这是一道打表题,但是有很多细节需要注意;上题:
首先,我认为题目没有说明白,补充几点
1.3×5点阵,其实是五行三列
2.每两个表示中间需要一个.作为分隔符
题解(两种处理字符串思路)
1.手动将矩阵赋值给对应的数,像洛谷题解那样
2.直接复制整段,在输出时进行加工(像我这样)
另外,顺便复习几个Java常识:
1.字符串的处理(substring方法的使用)、读入(next和nextLine方法)
2.字符与数字的转换
3.数组常量只能在初始化操作中使用
(不说了,抽胡桃去)
下面附上AC代码:
package P5730;
import java.util.*;
//每个数所代表的符号是一个类
public class Main {
public static void main(String[] args) {
//创建表
String house[] = {
"XXX...X.XXX.XXX.X.X.XXX.XXX.XXX.XXX.XXX",
"X.X...X...X...X.X.X.X...X.....X.X.X.X.X",
"X.X...X.XXX.XXX.XXX.XXX.XXX...X.XXX.XXX",
"X.X...X.X.....X...X...X.X.X...X.X.X...X",
"XXX...X.XXX.XXX...X.XXX.XXX...X.XXX.XXX",
};
//录入需要显示的信息
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String str = in.next();
char temp;
int arr[] = new int[n];//各个数字存放在arr数组里
for(int i=0;i<n;i++) {//按字符形式提取每个数字
arr[i] = str.charAt(i) - '0';
}
//开始输出
for(int k=0;k<5;k++) {//行数
for(int j=0;j<n;j++) {
if(arr[j] == 0) {
System.out.print(house[k].substring(0,3));
}
else if(arr[j] == 1) {
System.out.print(house[k].substring(4,7));
}
else if(arr[j] == 2) {
System.out.print(house[k].substring(8,11));
}
else if(arr[j] == 3) {
System.out.print(house[k].substring(12,15));
}
else if(arr[j] == 4) {
System.out.print(house[k].substring(16,19));
}
else if(arr[j] == 5) {
System.out.print(house[k].substring(20,23));
}
else if(arr[j] == 6) {
System.out.print(house[k].substring(24,27));
}
else if(arr[j] == 7) {
System.out.print(house[k].substring(28,31));
}
else if(arr[j] == 8) {
System.out.print(house[k].substring(32,35));
}
else if(arr[j] == 9) {
System.out.print(house[k].substring(36,39));
}
if(j!=n-1)System.out.print('.');
}
if(k != 4)System.out.print('\n');
}
}
}