fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define USERNUM 3 // number of rows
  5. #define COLNUM 6 // number of columns
  6. #define ECOLNUM (COLNUM/2) // number of effective columns
  7.  
  8. struct sum_with_index {
  9. int sum;
  10. int index;
  11. } users[USERNUM], rows[USERNUM][ECOLNUM];
  12.  
  13. int answer[USERNUM];
  14.  
  15. int a[USERNUM][COLNUM] =
  16. {{10,20,32,40,45,35},
  17. {20,10,25,42,47,40},
  18. {30,35,15,10,20,25}};
  19.  
  20. int cmp_sumorder_ascending(const void *v1, const void *v2);
  21. int cmp_sumorder_descending(const void *v1, const void *v2);
  22. void init();
  23. void cal_user_order();
  24. void cal_row_order();
  25. void cal_answer();
  26. void out_answer();
  27.  
  28. int main() {
  29. int i, j;
  30.  
  31. // initialize some data structure, users and rows literally
  32. init();
  33.  
  34. // calculate the order of users
  35. cal_user_order();
  36. /* DEBUG
  37. * for(i = 0; i < USERNUM; i++)
  38. printf("%d\t%d\n", users[i].sum, users[i].index);*/
  39.  
  40. // calculate the order of each row
  41. cal_row_order();
  42. /* DEBUG
  43. * for(i = 0; i < USERNUM; i++) {
  44. for(j = 0; j < ECOLNUM; j++)
  45. printf("%d,%d\t", rows[i][j].sum, rows[i][j].index);
  46. printf("\n");
  47. }*/
  48.  
  49. // according to the order of users, iterate each row and calculate the answer
  50. cal_answer();
  51.  
  52. // print out the answers
  53. out_answer(a, answer);
  54.  
  55. return 0;
  56. }
  57.  
  58. int cmp_sumorder_ascending(const void *v1, const void *v2) {
  59. if(((struct sum_with_index const *)v1)->sum > ((struct sum_with_index const *)v2)->sum) return 1;
  60. else if(((struct sum_with_index const *)v1)->sum == ((struct sum_with_index const *)v2)->sum) return 0;
  61. else return -1;
  62. }
  63.  
  64. int cmp_sumorder_descending(const void *v1, const void *v2) {
  65. if(((struct sum_with_index const *)v1)->sum > ((struct sum_with_index const *)v2)->sum) return -1;
  66. else if(((struct sum_with_index const *)v1)->sum == ((struct sum_with_index const *)v2)->sum) return 0;
  67. else return 1;
  68. }
  69.  
  70. void init() {
  71. int i, j;
  72. // calculate the sum of each user, and the sum of each pair in each row
  73. for(i = 0; i < USERNUM; i++) {
  74. users[i].sum = 0;
  75. users[i].index = i;
  76. for(j = 0; j < COLNUM; j++) {
  77. users[i].sum += a[i][j];
  78. if(j%2) {
  79. rows[i][(j-1)/2].sum = a[i][j-1] + a[i][j];
  80. rows[i][(j-1)/2].index = (j-1)/2;
  81. }
  82. }
  83. }
  84. }
  85.  
  86. void cal_user_order() {
  87. qsort(users, USERNUM, sizeof(struct sum_with_index), cmp_sumorder_ascending);
  88. }
  89.  
  90. void cal_row_order() {
  91. int i;
  92. for(i = 0; i < USERNUM; i++)
  93. qsort(rows[i], ECOLNUM, sizeof(struct sum_with_index), cmp_sumorder_descending);
  94. }
  95.  
  96. void cal_answer() {
  97. int chosen[ECOLNUM] = {0};
  98. int i, j;
  99.  
  100. for(i = 0; i < USERNUM; i++)
  101. for(j = 0; j < ECOLNUM; j++)
  102. if(0 == chosen[rows[users[i].index][j].index]) {
  103. chosen[rows[users[i].index][j].index] = 1;
  104. answer[users[i].index] = rows[users[i].index][j].index;
  105. break;
  106. }
  107. }
  108.  
  109. void out_answer() {
  110. int i, j;
  111. for(i = 0; i < USERNUM; i++)
  112. printf("User%d: %d %d\n", i+1, a[i][answer[i]*2], a[i][answer[i]*2+1]);
  113. }
  114.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
User1: 45 35
User2: 25 42
User3: 30 35