计蒜客—加一

最后更新于:2022-04-01 09:41:07

这个程序一开始没考虑到全9的特殊情况,导致提交一直不通过,程序写的有点多,不太符合题目的本意要求,后续找到优秀的算法,再改正 ~~~ import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { List<Integer> bList = new ArrayList<Integer>(); Scanner input = new Scanner(System.in); int num,temp; num = input.nextInt(); for (int i = 0; i < num; i++) { temp = input.nextInt(); bList.add(temp); } temp = judge(bList, num); if((num-1-temp>-1)&&(bList.get(num-1-temp)!=9)){ bList.set(num-1-temp, bList.get(num-1-temp)+1); }else { bList.set(num - temp, 0); } for (int i = (num-1); i >num - temp - 1 ; i--) { bList.set(i, 0); } if (judgezero(bList) == 0) { bList.add(0,1); } for (int i : bList) { System.out.print(i + " "); } } public static int judge(List<Integer> bList,int num) { int temp = 0; for (int i = (num-1); i >-1; i--) { if (bList.get(i) == 9) { temp++; }else { break; } } return temp; } public static int judgezero(List<Integer> bList) { int sum = 0; for (int i = 0; i < bList.size(); i++) { sum = sum + bList.get(i); } return sum; } } ~~~
';