UVA 11321 Java

  1. import java.util.*;
  2. import static java.lang.System.*;
  3.  
  4. public class main{
  5. public static void main(String[] args) {
  6. Scanner sc=new Scanner(System.in);
  7. while(sc.hasNextInt())
  8. {
  9. int num = sc.nextInt();
  10. int m = sc.nextInt();
  11. System.out.printf("%d %d\r\n", num, m);
  12. if(num == 0&& m == 0)
  13. break;
  14. Data[] datas = new Data[num];
  15. for(int i = 0; i < num; i++)
  16. {
  17. int n = sc.nextInt();
  18. datas[i] = new Data(n, n % m, n % 2 == 0);
  19. }
  20. Arrays.sort(datas);
  21. for(int i = 0; i < num; i++)
  22. {
  23. System.out.println(datas[i].toString());
  24. }
  25. }
  26. }
  27.  
  28. public static class Data implements Comparable
  29. {
  30. int n;
  31. int r;
  32. boolean even;
  33. Data(int n, int r, boolean even)
  34. {
  35. this.n = n;
  36. this.r = r;
  37. this.even = even;
  38. }
  39. public String toString()
  40. {
  41. return Integer.toString(this.n);
  42. }
  43. @Override
  44. public int compareTo(Data other)
  45. {
  46. if(this.r != other.r)
  47. return this.r < other.r ? -1 : 1;
  48. else
  49. {
  50. if(this.even != other.even)
  51. return !this.even ? -1 : 1;
  52. else if(this.even && other.even)
  53. return this.n < other.n ? -1 : 1;
  54. else
  55. return this. n < other.n ? 1 : -1;
  56. }
  57. }
  58. }
  59. };

留言

這個網誌中的熱門文章

C# 井字遊戲