fork download
  1. /*
  2.  * プログラミングのお題スレ Part11
  3.  * https://m...content-available-to-author-only...h.net/test/read.cgi/tech/1524570314/599
  4.  * 599 名前:デフォルトの名無しさん[sage] 投稿日:2018/07/14(土) 15:13:06.98 ID:QznZ4U7X
  5.  * 1から100までの数字から4つを取り出したそれぞれに番号を振る速い方法ありますか?
  6.  * メモリもあまり使わずに
  7.  *
  8.  * 1 2 3 4 -> 1
  9.  * 1 2 3 5 -> 2
  10.  * 1 2 3 6 -> 3
  11.  *
  12.  * 96 97 98 99 -> 3921221
  13.  * 96 97 98 100 -> 3921222
  14.  * 96 97 99 100 -> 3921223
  15.  * 96 98 99 100 -> 3921224
  16.  * 97 98 99 100 -> 3921225
  17.  **/
  18. class Ideone
  19. {
  20. public static void main(String[] args)
  21. {
  22. int i = 1;
  23. for (int a = 1; a <= 100; a++)
  24. {
  25. for (int b = a + 1; b <= 100; b++)
  26. {
  27. for (int c = b + 1; c <= 100; c++)
  28. {
  29. for (int d = c + 1; d <= 100; d++)
  30. {
  31. int j = h(a, b, c, d);
  32. if (j <= 6 || j >= 3921220) System.out.printf("%d %d %d %d: %d%n", a, b, c, d, j);
  33. if (j != i++) System.out.println("error");
  34. }
  35. }
  36. }
  37. }
  38. }
  39.  
  40. static int h(int a, int b, int c, int d)
  41. {
  42. assert a >= 1 & d <= 100 & a < b & b < c & c < d;
  43. a = (100 - a) * (99 - a) * (98 - a) * (97 - a);
  44. b = (100 - b) * (99 - b) * (98 - b) * 4;
  45. c = (100 - c) * (99 - c) * 12;
  46. d = (100 - d) * 24;
  47. return 3921225 - (a + b + c + d) / 24;
  48. }
  49. }
  50.  
Success #stdin #stdout 0.14s 29184KB
stdin
Standard input is empty
stdout
1 2 3 4: 1
1 2 3 5: 2
1 2 3 6: 3
1 2 3 7: 4
1 2 3 8: 5
1 2 3 9: 6
95 98 99 100: 3921220
96 97 98 99: 3921221
96 97 98 100: 3921222
96 97 99 100: 3921223
96 98 99 100: 3921224
97 98 99 100: 3921225