package tw.LanYao.myproject.HomeWork;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.LinkedList;
public class LinkedListb809 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sca = new Scanner(System.in);
while (sca.hasNext()) {
String word = sca.nextLine();
String[] wordArray = word.split("");
int num = sca.nextInt();// 幾層
LinkedList intArray = new LinkedList();// 階層
ArrayList abc = new ArrayList();// 存放千百十個...
for (int i = 1; i <= num; i++) {// 0階
int u = String.valueOf(i).length();// 判斷i為幾位數
if (u >= 2) {
for (int t = 0; t < u; t++) {
abc.add((i / (int) Math.pow(10, t) % 10));// 把該數依個位數、十位數...拆開放進集合
}
for (int n = u - 1; n >= 0; n--) {
intArray.add(abc.get(n));
}
abc.clear();
} else {
intArray.add(i);
}
}
while (true) {// 1~n階
for (int n = 0; n < intArray.size() - 1; n++) {
int a = (int) intArray.get(n) + (int) intArray.get(n + 1);
int k = String.valueOf(a).length();// 該數為幾位數
if (k >= 2) {
for (int i = 0; i < k; i++) {// 把該數依個位數、十位數...拆開放進集合
abc.add((a / (int) Math.pow(10, i)) % 10);
}
int h = 0;
for (int i = 0; i < abc.size(); i++) {
h = h + (int) abc.get(i);
}
intArray.set(n, h);
abc.clear();
} else {
intArray.set(n, a);
}
if (n == (intArray.size() - 2)) {// 移除最後一個數
intArray.remove(intArray.size() - 1);
}
}
if (intArray.size() == 1) {
break;
}
}
System.out.println("範例輸出" + wordArray[4]);
System.out.print(intArray.get(0));
}
sca.close();
}
}
跑1000的話要等等,開頭的字串偷懶...
不過為啥最後Scanner.close() 還是會被程式中斷?
package tw.LanYao.myproject.HomeWork;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.LinkedList;public class LinkedListb809 {
public static void main(String[] args) {
// TODO Auto-generated method stubScanner sca = new Scanner(System.in);
while (sca.hasNext()) {String word = sca.nextLine();
String[] wordArray = word.split("");
int num = sca.nextInt();// 幾層LinkedList intArray = new LinkedList();// 階層
ArrayList abc = new ArrayList();// 存放千百十個...for (int i = 1; i <= num; i++) {// 0階
int u = String.valueOf(i).length();// 判斷i為幾位數
if (u >= 2) {
for (int t = 0; t < u; t++) {
abc.add((i / (int) Math.pow(10, t) % 10));// 把該數依個位數、十位數...拆開放進集合
}
for (int n = u - 1; n >= 0; n--) {
intArray.add(abc.get(n));}
abc.clear();
} else {
intArray.add(i);
}
}while (true) {// 1~n階
for (int n = 0; n < intArray.size() - 1; n++) {
int a = (int) intArray.get(n) + (int) intArray.get(n + 1);
int k = String.valueOf(a).length();// 該數為幾位數
if (k >= 2) {for (int i = 0; i < k; i++) {// 把該數依個位數、十位數...拆開放進集合
abc.add((a / (int) Math.pow(10, i)) % 10);
}
int h = 0;
for (int i = 0; i < abc.size(); i++) {
h = h + (int) abc.get(i);
}intArray.set(n, h);
abc.clear();} else {
intArray.set(n, a);
}
if (n == (intArray.size() - 2)) {// 移除最後一個數
intArray.remove(intArray.size() - 1);
}
}if (intArray.size() == 1) {
break;
}}
System.out.println("範例輸出" + wordArray[4]);
System.out.print(intArray.get(0));}
sca.close();
}}
跑1000的話要等等,開頭的字串偷懶...
不過為啥最後Scanner.close() 還是會被程式中斷?