1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| #include <stdio.h> #include <stdlib.h> #include <string.h>
struct Person { int id; char name[30]; double income; };
double SumIncomeByLastname(struct Person *people, int n, const char *lastname); double SumIncomeByIDList(struct Person *people, int n, int *id_list, int id_count);
void Test(struct Person *people, int n);
int main(void) { int n; scanf("%d", &n); getchar();
struct Person *people = (struct Person *)malloc(n * sizeof(struct Person)); for (int i = 0; i < n; i++) { char line[100]; if (!fgets(line, sizeof(line), stdin)) break;
int id; char lastname[20], firstname[20]; double income;
sscanf(line, "%d %s %s %lf", &id, lastname, firstname, &income);
people[i].id = id; people[i].income = income; snprintf(people[i].name, sizeof(people[i].name),"%s %s", lastname, firstname);
}
Test(people, n); free(people); return 0; }
void Test(struct Person *people, int n) { for(int i = 0; i < n; i++) printf("%d %s %f\n", people[i].id, people[i].name, people[i].income);
printf("=== 测试: 姓氏 \"Zhang\" ===\n"); printf("%.2f\n", SumIncomeByLastname(people, n, "Zhang"));
printf("=== 测试: 姓氏 \"Li\" ===\n"); printf("%.2f\n", SumIncomeByLastname(people, n, "Li"));
printf("=== 测试: 姓氏 \"Torres\" ===\n"); printf("%.2f\n", SumIncomeByLastname(people, n, "Torres"));
printf("=== 测试: 姓氏 \"Minese\" (不存在) ===\n"); printf("%.2f\n", SumIncomeByLastname(people, n, "Minese"));
printf("=== 测试: 姓氏 \"Zhang San\" (完整名) ===\n"); printf("%.2f\n", SumIncomeByLastname(people, n, "Zhang San"));
printf("=== 测试: ID 1001~1100 全部 ===\n"); int all_ids[100]; for (int i = 0; i < 100; i++) { all_ids[i] = 1001 + i; } printf("%.2f\n", SumIncomeByIDList(people, n, all_ids, 100));
printf("=== 测试: ID 1001~1100 奇数 ===\n"); int odd_ids[50]; int count = 0; for (int i = 1001; i <= 1100; i += 2) { odd_ids[count++] = i; } printf("%.2f\n", SumIncomeByIDList(people, n, odd_ids, count));
printf("=== 测试: ID 1001~1100 偶数 ===\n"); int even_ids[50]; count = 0; for (int i = 1002; i <= 1100; i += 2) { even_ids[count++] = i; } printf("%.2f\n", SumIncomeByIDList(people, n, even_ids, count)); }
double SumIncomeByLastname(struct Person *people, int n, const char *lastname) { if (n <= 0) return -1; if (!people) return -1; if (!lastname) return -1;
double sum = 0.0; int len = strlen(lastname);
for (int i = 0; i < n; i++) { if (strncmp(people[i].name, lastname, len) == 0 && people[i].name[len] == ' ') { sum += people[i].income; } } return sum; }
double SumIncomeByIDList(struct Person *people, int n, int *id_list, int id_count) { double sum = 0.0; if ( people == NULL ) return -1; if ( n <= 0 ) return -1; for (int i = 0; i < n; i++) { for (int j = 0; j < id_count; j++) { if ( people[i].id == id_list[j] ) { sum += people[i].income; break; } } } return sum; }
|