fork download
  1. #include <stdio.h>
  2.  
  3. struct GDATA {
  4. int bango;
  5. char sei[11];
  6. char mei[11];
  7. int tokuten;
  8. };
  9.  
  10. int main(void)
  11. {
  12. FILE *fp;
  13. int ret;
  14. int count;
  15. struct GDATA a[100];
  16. int bango;
  17. int i;
  18. int j;
  19. int k;
  20. int found;
  21. int searchCount;
  22.  
  23. /* read */
  24. fp = fopen("data-1.dat", "r");
  25. if (fp == NULL) {
  26. printf("%s\n", "data-1.dat OPEN ERROR!");
  27. return 1;
  28. }
  29. i = 0;
  30. while (1) {
  31. ret = fscanf(fp, "%4d%10s%10s%3d", &a[i].bango, a[i].sei, a[i].mei, &a[i].tokuten);
  32. if (ret == EOF) {
  33. break;
  34. }
  35. i++;
  36. }
  37. fclose(fp);
  38.  
  39. count = i;
  40.  
  41. /* sort */
  42. {
  43. struct GDATA t;
  44. for(i=0; i<count; i++) {
  45. for(j=i+1; j<count; j++) {
  46. if(a[i].bango>a[j].bango) {
  47. t=a[i];
  48. a[i]=a[j];
  49. a[j]=t;
  50. }
  51. }
  52. }
  53. }
  54.  
  55. while (1) {
  56. /* input */
  57. ret = fscanf(stdin, "%d", &bango);
  58. if (ret == EOF) {
  59. break;
  60. }
  61.  
  62. /* search */
  63. i = 0;
  64. j = count - 1;
  65. searchCount = 0;
  66. while (1) {
  67. if (i > j) {
  68. found = 0;
  69. break;
  70. }
  71. k = ((j - i) >> 1) + i;
  72. searchCount++;
  73. if (bango < a[k].bango) {
  74. j = k - 1;
  75. } else {
  76. if (bango > a[k].bango) {
  77. i = k + 1;
  78. } else {
  79. found = 1;
  80. break;
  81. }
  82. }
  83. }
  84.  
  85. /* message */
  86. if (found) {
  87. printf("%d番%s %s 得点= %d点 探索回数= %d回\n", a[k].bango, a[k].sei, a[k].mei, a[k].tokuten, searchCount);
  88. } else {
  89. printf("%d番なし 探索回数= %d回\n", bango, searchCount);
  90. }
  91. }
  92.  
  93. return 0;
  94. }
Runtime error #stdin #stdout 0.01s 1808KB
stdin
Standard input is empty
stdout
data-1.dat OPEN ERROR!