UVa 12412 - a Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

是道例题,编写个管理系统,类似于课本十二章的那个。自己写的代码运行样例正常,但提交WA。而且给了一半的代码,就又上网搜了例题原版代码。对比了输入与输出,一直
没找到错,目测是有个地方格式错了或者精度控制出现了问题。。 照着重敲一遍Ac了。。

PS:发现问题了,原本写的代码缓冲区出现了问题导致WA。。

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<stdio.h>
#include<string.h>
#define maxn 1000
#define maxl 100
#define EPS 1e-5
int n;
char sid[maxn][maxl];
int cid[maxn];
char name[maxn][maxl];
int score[maxn][5];
int removed[maxn];
const char* course_name[]={"Chinese","Mathematics","English","Programming"};
int valid(int k)
{
for (int i=0;i<k;i++)
if(!removed[i])
if(!strcmp(sid[i],sid[k]))
return 0;
return 1;
}
void add()
{
while(1)
{
printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
scanf("%s",sid[n]);
if(!strcmp(sid[n],"0"))
break;
scanf("%d%s%d%d%d%d",&cid[n],name[n],&score[n][0],&score[n][1],&score[n][2],&score[n][3]);
if(valid(n))
{
score[n][4]=score[n][0]+score[n][1]+score[n][2]+score[n][3];
n++;
}
else
printf("Duplicated SID.\n");
}
}
int rank(int k)
{
int r=0;
for(int i=0;i<n;i++)
if(!removed[i]&&score[i][4]>score[k][4])
r++;
return r+1;
}
void DQ (int isq)
{
char s[maxl];
while(1)
{
printf("Please enter SID or name. Enter 0 to finish.\n");
scanf("%s",s);
if(!strcmp (s, "0"))
break;
int r=0;
for(int i=0;i<n;i++)
if(!removed[i])
{
if(!strcmp(sid[i],s)||!strcmp(name[i],s))
{
if(isq)
printf("%d %s %d %s %d %d %d %d %d %.2f\n",rank(i),sid[i],cid[i],name[i],score[i][0],score[i][1],score[i][2],score[i][3],score[i][4],score[i][4]/4.0+EPS);
else
{
removed[i]=1;
r++;
}
}
}
if(!isq)
printf("%d student(s) removed.\n",r);
}
}
double get_course_stat(int c,int s,int* passed,int* failed)
{
int tot=0;
*passed=*failed=0;
for(int i=0;i<n;i++)
if(!removed[i]&&(!c||cid[i]==c))
{
tot+=score[i][s];
if(score[i][s]>=60)
(*passed)++;
else
(*failed)++;
}
return (double)tot/(double)(*passed+*failed);
}
void get_overall_stat(int c,int* cnt)
{
cnt[0]=cnt[1]=cnt[2]=cnt[3]=cnt[4]=0;
for(int i=0;i<n;i++)
if(!removed[i]&&(!c||cid[i]==c))
{
int k=0;
for(int j=0;j<4;j++)
if(score[i][j]>=60)
k++;
cnt[k]++;
}
}
void stat()
{
int c;
printf("Please enter class ID, 0 for the whole statistics.\n");
scanf("%d", &c);
for(int i=0;i<4;i++)
{
int passed,failed;
double avg=get_course_stat(c,i,&passed,&failed);
printf("%s\n",course_name[i]);
printf("Average Score: %.2f\n",avg+EPS);
printf("Number of passed students: %d\n",passed);
printf("Number of failed students: %d\n",failed);
printf("\n");
}
int cnt[5];
get_overall_stat(c,cnt);
printf("Overall:\n");
printf("Number of students who passed all subjects: %d\n",cnt[4]);
printf("Number of students who passed 3 or more subjects: %d\n",cnt[4]+cnt[3]);
printf("Number of students who passed 2 or more subjects: %d\n",cnt[4]+cnt[3]+cnt[2]);
printf("Number of students who passed 1 or more subjects: %d\n",cnt[4]+cnt[3]+cnt[2]+cnt[1]);
printf("Number of students who failed all subjects: %d\n",cnt[0]);
printf("\n");
}
int main()
{
while(1)
{
int choice;
printf("Welcome to Student Performance Management System (SPMS).\n");
printf("\n");
printf("1 - Add\n");
printf("2 - Remove\n");
printf("3 - Query\n");
printf("4 - Show ranking\n");
printf("5 - Show Statistics\n");
printf("0 - Exit\n");
printf("\n");
scanf("%d",&choice);
if(choice==0)
break;
if(choice==1)
add();
if(choice==2)
DQ(0);
if(choice==3)
DQ(1);
if(choice==4)
printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
if(choice==5)
stat();
}
return 0;
}

本文迁移自我的 CSDN博客 ,格式可能有所偏差。