本題用到的知識點是:結構體數組。
?上代碼:
#include<stdio.h>
#define max 10050
typedef struct PEOPLE {
int num;//編號
double income;//收入
int count;//搶到紅包個數
}peo;
int main()
{
peo s1[max] = { 0 };
int n; scanf("%d", &n);
int i, j;
for (i = 0; i < n; i++) {
s1[i].num = i + 1;
int m; scanf("%d", &m);//輸入發出紅包個數
for (j = 0; j < m; j++) {
int a, b;//編號、金額
scanf("%d %d", &a, &b);
s1[i].income -= b;
s1[a - 1].income += b;
s1[a - 1].count++;
}
}
peo temp = { 0 };
for (i = 0; i < n-1; i++) {
for (j = i + 1; j < n; j++) {
if (s1[i].income < s1[j].income) {
temp = s1[i];
s1[i] = s1[j];
s1[j] = temp;
}
}
}
for (i = 0; i < n; i++) {
if (s1[i].income == s1[i + 1].income) {
if (s1[i].count > s1[i+1].count) {
//printf("%d %.2lf\n", s1[i].num, s1[i].income / 100);
}
else if(s1[i].count < s1[i+1].count){
temp = s1[i];
s1[i] = s1[i + 1];
s1[i + 1] = temp;
//printf("%d %.2lf\n", s1[i].num, s1[i].income / 100);
}
else {
if (s1[i].num > s1[i + 1].num) {
temp = s1[i];
s1[i] = s1[i + 1];
s1[i + 1] = temp;
}
}
}
//else {
printf("%d %.2lf\n", s1[i].num, s1[i].income / 100);
//}
}
}
希望本篇對你有所幫助。
|