#27268: 找了好久找不出錯誤 WA (line:6)


s10914054@gm.cyut.edu.tw (Justin Tseng)

學校 : 不指定學校
編號 : 167107
來源 : [223.137.177.247]
最後登入時間 :
2022-08-30 17:42:14
a225. 明明愛排列 | From: [111.250.4.139] | 發表日期 : 2021-09-21 12:35

package zerojudge;

import java.math.*;

import java.util.*;

 

public class a225

{

public static void main(String[] args)

{

boolean change;

int position=0;

BigInteger compare,temporary;

 

Scanner sc=new Scanner(System.in);

while(sc.hasNextInt())

{

int number=sc.nextInt();

 

BigInteger numbers[]=new BigInteger[number];

for(int i=0;i<numbers.length;i++)

{

numbers[i]=sc.nextBigInteger();

}

 

for(int i=0;i<numbers.length;i++)

{

compare=numbers[i].mod(BigInteger.TEN);

change=false;

for(int j=i+1;j<numbers.length;j++)

{

if(compare.compareTo(numbers[j].mod(BigInteger.TEN))>0)

{

compare=numbers[j].mod(BigInteger.TEN);

position=j;

change=true;

}

else if(compare.compareTo(numbers[j].mod(BigInteger.TEN))==0)

{

if(numbers[i].compareTo(numbers[j])<0)

{

compare=numbers[j].mod(BigInteger.TEN);

position=j;

change=true;

}

}

}

if(change)

{

temporary=numbers[i];

numbers[i]=numbers[position];

numbers[position]=temporary;

}

}

 

for(BigInteger i:numbers)

{

System.out.print(i+" ");

}

System.out.println();

}

 

sc.close();

}

}

 

結果如下:

您的答案為: 1814543140 2134384050 1163484970 1438601320 1692624941 722027101 2107648762 1931992482 1025888862 611282933 270487683 1044902334 1598613074 436472365 1853284855 1344171365 1401532235 721903706 1826880957 1302988597 54003667 1055741897 2115385518 231511578 1922206729 2011091129 1951633349 1803868939 1699746019 509985669
正確答案為: 2134384050 1814543140 1438601320 1163484970 1692624941 722027101 2107648762 1931992482 1025888862 611282933 270487683 1598613074 1044902334 1853284855 1401532235 1344171365 436472365 721903706 1826880957 1302988597 1055741897 54003667 2115385518 231511578 2011091129 1951633349 1922206729 1803868939 1699746019 509985669
 
#27269: Re:找了好久找不出錯誤 WA (line:6)


s10914054@gm.cyut.edu.tw (Justin Tseng)

學校 : 不指定學校
編號 : 167107
來源 : [223.137.177.247]
最後登入時間 :
2022-08-30 17:42:14
a225. 明明愛排列 | From: [111.250.4.139] | 發表日期 : 2021-09-21 12:36

package zerojudge;

import java.math.*;

import java.util.*;

 

public class a225

{

public static void main(String[] args)

{

boolean change;

int position=0;

BigInteger compare,temporary;

 

Scanner sc=new Scanner(System.in);

while(sc.hasNextInt())

{

int number=sc.nextInt();

 

BigInteger numbers[]=new BigInteger[number];

for(int i=0;i<numbers.length;i++)

{

numbers[i]=sc.nextBigInteger();

}

 

for(int i=0;i<numbers.length;i++)

{

compare=numbers[i].mod(BigInteger.TEN);

change=false;

for(int j=i+1;j<numbers.length;j++)

{

if(compare.compareTo(numbers[j].mod(BigInteger.TEN))>0)

{

compare=numbers[j].mod(BigInteger.TEN);

position=j;

change=true;

}

else if(compare.compareTo(numbers[j].mod(BigInteger.TEN))==0)

{

if(numbers[i].compareTo(numbers[j])<0)

{

compare=numbers[j].mod(BigInteger.TEN);

position=j;

change=true;

}

}

}

if(change)

{

temporary=numbers[i];

numbers[i]=numbers[position];

numbers[position]=temporary;

}

}

 

for(BigInteger i:numbers)

{

System.out.print(i+" ");

}

System.out.println();

}

 

sc.close();

}

}

 

結果如下:

您的答案為: 1814543140 2134384050 1163484970 1438601320 1692624941 722027101 2107648762 1931992482 1025888862 611282933 270487683 1044902334 1598613074 436472365 1853284855 1344171365 1401532235 721903706 1826880957 1302988597 54003667 1055741897 2115385518 231511578 1922206729 2011091129 1951633349 1803868939 1699746019 509985669
正確答案為: 2134384050 1814543140 1438601320 1163484970 1692624941 722027101 2107648762 1931992482 1025888862 611282933 270487683 1598613074 1044902334 1853284855 1401532235 1344171365 436472365 721903706 1826880957 1302988597 1055741897 54003667 2115385518 231511578 2011091129 1951633349 1922206729 1803868939 1699746019 509985669


自己在compiler測試是沒問題的QQ

 
#27272: Re:找了好久找不出錯誤 WA (line:6)


jam930725@gmail.com (浮沉沉沉沉沉沉沉沉)

學校 : 國立臺中科技大學
編號 : 124762
來源 : [123.240.115.224]
最後登入時間 :
2022-08-27 13:56:53
a225. 明明愛排列 | From: [123.110.34.107] | 發表日期 : 2021-09-21 13:21

排序法裡面寫錯了

change = false;

以及

if(change){

    temporary=numbers[i];

    numbers[i]=numbers[position];

    numbers[position]=temporary;

}

應該放在第二層的迴圈裏面

具體的原因 可以好好想一下bubble sort是怎麼運作的

 

另外 這題可以直接用int來儲存數字,不需要用到java.math.BigInteger,寫起來會比較簡單

如果像我一樣比較懶的人 也可以用Integer來儲存數字 然後搭配java.util.Arrays.sort()和Comparator來寫

Arrays.sort(arr, new Comparator <Integer>(){

    @Override

    public int compare(Integer a, Integer b){

        if(a%10 == b%10)

            return b - a;

        return a%10 - b%10;

    }

});

/*

上面是lambda的寫法 也可以單獨寫一個class出來 大概像這樣子

public static class Cmp implements Comparator{

    public int compare(Object o1, Object o2){

        //code

    }

}

呼叫的時候

Arrays.sort(arr, new Cmp());

*/

 
#27273: Re:找了好久找不出錯誤 WA (line:6)


s10914054@gm.cyut.edu.tw (Justin Tseng)

學校 : 不指定學校
編號 : 167107
來源 : [223.137.177.247]
最後登入時間 :
2022-08-30 17:42:14
a225. 明明愛排列 | From: [111.250.4.139] | 發表日期 : 2021-09-21 13:55

排序法裡面寫錯了

change = false;

以及

if(change){

    temporary=numbers[i];

    numbers[i]=numbers[position];

    numbers[position]=temporary;

}

應該放在第二層的迴圈裏面

具體的原因 可以好好想一下bubble sort是怎麼運作的

 

另外 這題可以直接用int來儲存數字,不需要用到java.math.BigInteger,寫起來會比較簡單

如果像我一樣比較懶的人 也可以用Integer來儲存數字 然後搭配java.util.Arrays.sort()和Comparator來寫

Arrays.sort(arr, new Comparator (){

    @Override

    public int compare(Integer a, Integer b){

        if(a%10 == b%10)

            return b - a;

        return a%10 - b%10;

    }

});

/*

上面是lambda的寫法 也可以單獨寫一個class出來 大概像這樣子

public static class Cmp implements Comparator{

    public int compare(Object o1, Object o2){

        //code

    }

}

呼叫的時候

Arrays.sort(arr, new Cmp());

*/


非常感謝! AC了

 
ZeroJudge Forum