注释掉的部分使用Arrays.sort()排序都正常,但是arr2排序完后都成0 了,在Arrays.sort()前面输出arr2也是正常的,排序完输出后为什么会成0?
代码:
import java.util.Arrays; import java.util.Scanner; public class pat_1064 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr1 = new int[n]; int[] arr2 = new int[n]; for(int i = 0; i < arr1.length;i++){ arr1[i] = in.nextInt(); int temp = arr1[i]; arr1[i] = mod(temp); //至此全部转换成各位数和 System.out.print(arr1[i] + " "); } //这里不造怎么处理 /* * 从第一个开始找,第一次遇到一样的时候,标志置为false, * 然后从这个数再往下找,遇到一样的数组不再增加,而是都置为-1 * */ System.out.println(); int count = 0; for(int i = 0; i < n; i++){ for(int j = i + 1; j < n - 1; j++){ if(arr1[i] == arr1[j] && arr1[i] != -1 && arr1[j] != -1){ arr2[count] = arr1[i]; // System.out.println(arr2[count]); count++; arr1[j] = -1; for(int x = j + 1; x < n-1; x++){ if(arr1[i] == arr1[x]) arr1[x] = -1; } break; } } } System.out.println(arr2[0]); System.out.println(arr2[1]); System.out.println(arr2[2]); Arrays.sort(arr2); // int[] arr3 = new int[]{2,4,1,7,3,5}; // Arrays.sort(arr3); // for(int i = 0; i < 6; i++) System.out.print(arr3[i] + " "); for(int i = 0; i < count; i++) System.out.print(arr2[i] + " "); } public static int mod(int n){ int yu = 0; while(n != 0){ yu = yu + n % 10; n = n / 10; } return yu; } }
测试数据:
8 123 899 51 998 27 33 36 12
第一次遇见这种情况
The size of
arr2 is 8 and count is 3. Of course it will print
0
,因为Arrays.sort()
After execution it becomes arr2:{0,0,0,0,0,6,9,26}You can use System.out.println(Arrays.toString(arr2)); to view the contents of the array