提出詳細
ソースコード
import java.util.*;
public class Capreka {
public static int max(int number) {
List<Integer> numlist = new ArrayList<>();
Integer ichi,ju,hyaku,sen;
sen = number/1000;
hyaku = (number-sen*1000)/100;
ju = (number-sen*1000-hyaku*100)/10;
ichi = (number-sen*1000-hyaku*100-ju*10);
numlist.add(sen);
numlist.add(hyaku);
numlist.add(ju);
numlist.add(ichi);
for(int i=0;i<numlist.size();i++) {
for(int j=numlist.size()-1;j>i;j--) {
if(numlist.get(j-1) < numlist.get(j)) {
Integer tmp = numlist.get(j-1);
numlist.set(j-1,numlist.get(j));
numlist.set(j,tmp);
}
}
}
return numlist.get(0)*1000+numlist.get(1)*100+numlist.get(2)*10+numlist.get(3);
}
public static int min(int number) {
List<Integer> numlist = new ArrayList<>();
Integer ichi,ju,hyaku,sen;
sen = number/1000;
hyaku = (number-sen*1000)/100;
ju = (number-sen*1000-hyaku*100)/10;
ichi = (number-sen*1000-hyaku*100-ju*10);
numlist.add(sen);
numlist.add(hyaku);
numlist.add(ju);
numlist.add(ichi);
for(int i=0;i<numlist.size();i++) {
for(int j=numlist.size()-1;j>i;j--) {
if(numlist.get(j-1) > numlist.get(j)) {
Integer tmp = numlist.get(j-1);
numlist.set(j-1,numlist.get(j));
numlist.set(j,tmp);
}
}
}
return numlist.get(0)*1000+numlist.get(1)*100+numlist.get(2)*10+numlist.get(3);
}
public static void main(String[] args ) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt(); //テストケース数
for (int i = 0; i < T; i++) {
int count = 0; //収束するまでの回数
int N = sc.nextInt();
for(;;) {
if(N == 6174) break;
N = max(N)-min(N);
count++;
if(N == 6174) break;
if(N==0) break;
}
System.out.printf("Case #%d:\n",i+1);
System.out.println(count);
}
}
}
提出情報
提出出力結果
テストケース情報