fork download
  1. #include <stdio.h>
  2.  
  3. struct student {
  4. int id; /* 学籍番号 */
  5. int eng; /* 英語 */
  6. int math; /* 数学 */
  7. int sci; /* 理科 */
  8. };
  9.  
  10. #define N 3
  11.  
  12. int main() {
  13.  
  14. int i, j;
  15.  
  16. struct student s[N] = {
  17. {17001, 60, 100, 20},
  18. {17002, 90, 40, 80},
  19. {17003, 20, 30, 50}
  20. };
  21.  
  22. struct student tmp;
  23.  
  24. int total1, total2;
  25.  
  26. /* 合計点で昇順ソート */
  27. for(i = 1; i < N; i++) {
  28. for(j = 0; j < N - i; j++) {
  29.  
  30. total1 = s[j].eng + s[j].math + s[j].sci;
  31. total2 = s[j+1].eng + s[j+1].math + s[j+1].sci;
  32.  
  33. if(total1 > total2) {
  34. tmp = s[j];
  35. s[j] = s[j+1];
  36. s[j+1] = tmp;
  37. }
  38. }
  39. }
  40.  
  41. /* 表示 */
  42. for(i = 0; i < N; i++) {
  43.  
  44. int total = s[i].eng + s[i].math + s[i].sci;
  45.  
  46. printf("学籍番号:%d 英語:%d 数学:%d 理科:%d 合計:%d\n",
  47. s[i].id, s[i].eng, s[i].math, s[i].sci, total);
  48. }
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
学籍番号:17003 英語:20 数学:30 理科:50 合計:100
学籍番号:17001 英語:60 数学:100 理科:20 合計:180
学籍番号:17002 英語:90 数学:40 理科:80 合計:210