起因
在群里閑逛看到這張圖
不看廢話-代碼見文末[點擊跳轉]
我覺得這是個段子,但有群友覺得這是真實故事
然后我就寫了個腳本生成這88萬行代碼反駁他
發現這算是入門算法題;
- 1.計算數字位數
- 2.倒序輸出
代碼
跳轉到的地方
class Scratch {
public static void main(String[] args) {
for(int i=1;i<100000;i++){
int[] arr = transfer(i);
int lth=arr.length;
System.out.printf("case %d:",i);
System.out.printf("\n");
System.out.printf("count << \"是%d位數\"<<end1;",lth);
System.out.printf("\n");
System.out.printf("count << \"個位數是:%d\"<<end1;",arr[0]);
System.out.printf("\n");
if (lth>1){
System.out.printf("count << \"十位數是:%d\"<<end1;",arr[1]);
System.out.printf("\n");
if(lth>2){
System.out.printf("count << \"百位數是:%d\"<<end1;",arr[2]);
System.out.printf("\n");
if(lth>3){
System.out.printf("count << \"千位數是:%d\"<<end1;",arr[3]);
System.out.printf("\n");
if(lth>4){
System.out.printf("count << \"萬位數是:%d\"<<end1;",arr[4]);
System.out.printf("\n");
}
}
}
}
StringBuffer r=new StringBuffer();
for(int c=arr.length-1; c>=0; c--){
r.append(arr[c]);
}
System.out.printf("count << \"倒過來是:%s\"<<end1;",r.toString());
System.out.println();
System.out.println("break;");
}
}
public static int[] transfer(int a ){
String str =null;
str = Integer.toString(a);
int[] arr = new int[str.length()];
for(int i=0; i<arr.length; i++){
char c = str.charAt(i);
String s = String.valueOf(c);
int num = Integer.parseInt(s);
arr[i] = num;
}
return arr;
}
}