打印模式 - Coding Ninjas Career Camp Fresher
Print Pattern - Coding Ninjas Career Camp Fresher
为给定的 N 行数打印以下模式。
Pattern for N = 4
1
##
234
####
输入格式:
Integer N (Total no. of rows)
输出格式:
Pattern in N lines
约束条件:
N lies in the range: [1,20]
示例输入:
5
示例输出:
1
##
234
####
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int N = scn.nextInt();
int val = 1;
for (int row = 1; row <= N; row++) {
if (row % 2 == 0) {
// Print special character, row number of times
for (int col = 1; col <= row; col++) {
System.out.print("#");
}
} else {
// Print val, row number it times, increment it continuously
for (int col = 1; col <= row; col++) {
System.out.print(val);
val++;
}
}
System.out.println();
}
scn.close();
}
}
为给定的 N 行数打印以下模式。
Pattern for N = 4
1
##
234
####
输入格式:
Integer N (Total no. of rows)
输出格式:
Pattern in N lines
约束条件:
N lies in the range: [1,20]
示例输入:
5
示例输出:
1
##
234
####
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int N = scn.nextInt();
int val = 1;
for (int row = 1; row <= N; row++) {
if (row % 2 == 0) {
// Print special character, row number of times
for (int col = 1; col <= row; col++) {
System.out.print("#");
}
} else {
// Print val, row number it times, increment it continuously
for (int col = 1; col <= row; col++) {
System.out.print(val);
val++;
}
}
System.out.println();
}
scn.close();
}
}