例题4-4 Message Decoding UVA - 213
Some message encoding schemes require that an encoded message be sent in two parts. The first part,
called the header, contains the characters of the message. The second part contains a pattern that
represents the message. You must write a program that can decode messages under such a scheme.
The heart of the encoding scheme for your program is a sequence of “key” strings of 0’s and 1’s as
follows:
0, 00, 01, 10, 000, 001, 010, 011, 100, 101, 110, 0000, 0001, … , 1011, 1110, 00000, …
The first key in the sequence is of length 1, the next 3 are of length 2, the next 7 of length 3, the
next 15 of length 4, etc. If two adjacent keys have the same length, the second can be obtained from
the first by adding 1 (base 2). Notice that there are no keys in the sequence that consist only of 1’s.
The keys are mapped to the characters in the header in order. That is, the first key (0) is mapped
to the first character in the header, the second key (00) to the second character in the header, the kth
key is mapped to the kth character in the header. For example, suppose the header is:
AB#TANCnrtXc
Then 0 is mapped to A, 00 to B, 01 to #, 10 to T, 000 to A, …, 110 to X, and 0000 to c.
The encoded message contains only 0’s and 1’s and possibly carriage returns, which are to be ignored.
The message is divided into segments. The first 3 digits of a segment give the binary representation
of the length of the keys in the segment. For example, if the first 3 digits are 010, then the remainder
of the segment consists of keys of length 2 (00, 01, or 10). The end of the segment is a string of 1’s
which is the same length as the length of the keys in the segment. So a segment of keys of length 2 is
terminated by 11. The entire encoded message is terminated by 000 (which would signify a segment
in which the keys have length 0). The message is decoded by translating the keys in the segments
one-at-a-time into the header characters to which they have been mapped.
Input
The input file contains several data sets. Each data set consists of a header, which is on a single line
by itself, and a message, which may extend over several lines. The length of the header is limited
only by the fact that key strings have a maximum length of 7 (111 in binary). If there are multiple
copies of a character in a header, then several keys will map to that character. The encoded message
contains only 0’s and 1’s, and it is a legitimate encoding according to the described scheme. That is,
the message segments begin with the 3-digit length sequence and end with the appropriate sequence of
1’s. The keys in any given segment are all of the same length, and they all correspond to characters in
the header. The message is terminated by 000.
Carriage returns may appear anywhere within the message part. They are not to be considered as
part of the message.
Output
For each data set, your program must write its decoded message on a separate line. There should not
be blank lines between messages.
Sample input
TNM AEIOU
0010101100011
1010001001110110011
11000
$#**\
0100000101101100011100101000
Sample output
TAN ME
*$
参考刘汝佳《算法竞赛入门经典》(第二版)AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char code[10][1000];
char read_bin(){
char c;
while(1){
c=getchar();
if(c!='\r' && c!='\n') return c;
}
}
bool read_head(){
memset(code,0,sizeof(code));
for(int i=1;i<=7;i++){
for(int j=0;j<=(1<<i)-2;j++){
char c=getchar();
if(c=='\n' || c=='\r') return true;
if(c==EOF) return false;
code[i][j]=c;
}
}
return true;
}
int read_len(int len){
int n=0;
while(len--){
n=n*2+read_bin()-'0';
}
return n;
}
int main()
{
while(read_head()){
while(1){
int len=read_len(3);
if(len==0) break;
while(1){
int len2=read_len(len);
if(len2==(1<<len)-1) break;
putchar(code[len][len2]);
}
}
cout<<endl;
getchar();
}
return 0;
}
例题4-5 Spreadsheet Tracking UVA - 512
Input
The input consists of a sequence of spreadsheets, operations on those spreadsheets, and queries about
them. Each spreadsheet definition begins with a pair of integers specifying its initial number of rows
(r) and columns (c), followed by an integer specifying the number (n) of spreadsheet operations. Row
and column labeling begins with 1. The maximum number of rows or columns of each spreadsheet is
limited to 50. The following n lines specify the desired operations.
An operation to exchange the contents of cell (r1, c1) with the contents of cell (r2, c2) is given by:
EX r1 c1 r2 c2
The four insert and delete commands—DC (delete columns), DR (delete rows), IC (insert columns),
and IR (insert rows) are given by:
< command > A x1 x2 … xA
where < command > is one of the four commands; A is a positive integer less than 10, and x1, … , xA
are the labels of the columns or rows to be deleted or inserted before. For each insert and delete
command, the order of the rows or columns in the command has no significance. Within a single delete
or insert command, labels will be unique.
The operations are followed by an integer which is the number of queries for the spreadsheet. Each
query consists of positive integers r and c, representing the row and column number of a cell in the
original spreadsheet. For each query, your program must determine the current location of the data
that was originally in cell (r, c). The end of input is indicated by a row consisting of a pair of zeros for
the spreadsheet dimensions.
Output
For each spreadsheet, your program must output its sequence number (starting at 1). For each query,
your program must output the original cell location followed by the final location of the data or the
word ‘GONE’ if the contents of the original cell location were destroyed as a result of the operations.
Separate output from different spreadsheets with a blank line.
The data file will not contain a sequence of commands that will cause the spreadsheet to exceed the
maximum size.
Sample Input
7 9
5
DR 2 1 5
DC 4 3 6 7 9
IC 1 3
IR 2 2 4
EX 1 2 6 5
4
4 8
5 5
7 8
6 5
0 0
Sample Output
Spreadsheet #1
Cell data in (4,8) moved to (4,6)
Cell data in (5,5) GONE
Cell data in (7,8) moved to (7,6)
Cell data in (6,5) moved to (1,2)
参考刘汝佳《算法竞赛入门经典》(第二版)
AC代码:
#include<iostream>
using namespace std;
typedef struct{
char c[10];
int r1,c1,r2,c2;
int num;
int x[50];
}Cmd;
Cmd cmd[1000];
int row,col,n;
bool simulate(int &r0,int &c0){
for(int i=0;i<n;i++){
if(cmd[i].c[0]=='E'){
if(cmd[i].r1==r0 && cmd[i].c1==c0){
r0=cmd[i].r2;c0=cmd[i].c2;
}
else if(cmd[i].r2==r0 && cmd[i].c2==c0){
r0=cmd[i].r1;c0=cmd[i].c1;
}
}
else{
int dr=0,dc=0;
for(int j=0;j<cmd[i].num;j++){
int xx=cmd[i].x[j];
if(cmd[i].c[0]=='I'){
if(cmd[i].c[1]=='R' && xx<=r0) {dr++;}
if(cmd[i].c[1]=='C' && xx<=c0) {dc++;}
}
else{
if(cmd[i].c[1]=='R' && xx==r0) return 0;
if(cmd[i].c[1]=='C' && xx==c0) return 0;
if(cmd[i].c[1]=='R' && xx<r0) {dr--;}
if(cmd[i].c[1]=='C' && xx<c0) {dc--;}
}
}
r0+=dr;c0+=dc;
}
}
return 1;
}
int main()
{
int kase=0;
while(cin>>row>>col && row){
cin>>n;
for(int i=0;i<n;i++){
cin>>cmd[i].c;
if(cmd[i].c[0]=='E'){
cin>>cmd[i].r1>>cmd[i].c1>>cmd[i].r2>>cmd[i].c2;
}
else{
cin>>cmd[i].num;
for(int j=0;j<cmd[i].num;j++){
cin>>cmd[i].x[j];
}
}
}
if(kase!=0) cout<<endl;
cout<<"Spreadsheet #"<<++kase<<endl;
int query_num=0;
int r0,c0;
cin>>query_num;
while(query_num--){
cin>>r0>>c0;
cout<<"Cell data in ("<<r0<<","<<c0<<") ";
if(!simulate(r0,c0)){
cout<<"GONE"<<endl;
}
else{
cout<<"moved to ("<<r0<<","<<c0<<")"<<endl;
}
}
}
return 0;
}
例题4-6 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang) UVA - 12412
Hi, I am an undergraduate student in institute of foreign languages. As you know, C programming is a
required course in our university, even if his/her major is far from computer science. I don’t like this
course at all, as I am not good at computer and I don’t wanna even have a try of any programming!!
But I have to do the homework in order to pass :( Sh… Could you help me with it? Please keep secret!!
I know that you won’t say NO to a poor little girl, boy. :)
Task
Write a Student Performance Management System (SPMS).
Concepts
In the SPMS, there will be at most 100 students, each has an SID, a CID, a name and four scores
(Chinese, Mathematics, English and Programming).
• SID (student ID) is a 10-digit number
• CID (class ID) is a positive integer not greater than 20.
• Name is a string of no more than 10 letters and digits, beginning with a capital letter. Note that
a name cannot contain space characters inside.
• Each score is a non-negative integer not greater than 100.
Main Menu
When you enter the SPMS, the main menu should be shown like this:
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Adding a Student
If you choose 1 from the main menu, the following message should be printed on the screen:
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Then your program should wait for user input. The input lines are always valid (no invalid SID,
CID, or name, exactly four scores etc), but the SID may already exist. In that case, simply ignore this
line and print the following:
Duplicated SID.
On the other hand, multiple students can have the same name.
You should keep printing the message above until the user inputs a single zero. After that the main
menu is printed again.
Removing a Student
If you choose 2 from the main menu, the following message should be printed on the screen:
Please enter SID or name. Enter 0 to finish.
Then your program should wait for user input, and remove all the students matching the SID or
name in the database, and print the following message (it’s possible xx = 0):
xx student(s) removed.
You should keep printing the message above until the user inputs a single zero. After that the main
menu is printed again.
Querying Students
If you choose 3 from the main menu, the following message should be printed on the screen:
Please enter SID or name. Enter 0 to finish.
Then your program should wait for user input. If no student matches the SID or name, simply do
nothing, otherwise print out all the matching students, in the same order they’re added to the database.
The format is similar to the input format for “adding a student”, but 3 more columns are added: rank
(1st column), total score and average score (last two columns). The student with highest total score
(considering all classes) received rank − 1, and if there are two rank − 2 students, the next one would
be rank − 4.
You should keep printing the message above until the user inputs a single zero. After that the main
menu is printed again.
Showing the Ranklist
If you choose 4 from the main menu, the following message should be printed on the screen:
Showing the ranklist hurts students’ self-esteem. Don’t do that.
Then the main menu is printed again.
Showing Statistics
If you choose 5 from the main menu, show the statistics, in the following format:
Please enter class ID, 0 for the whole statistics.
When a class ID is entered, print the following statistics. Note that “passed” means to have a score
of at least 60.
Chinese
Average Score: xx.xx
Number of passed students: xx
Number of failed students: xx
Mathematics
Average Score: xx.xx
Number of passed students: xx
Number of failed students: xx
English
Average Score: xx.xx
Number of passed students: xx
Number of failed students: xx
Programming
Average Score: xx.xx
Number of passed students: xx
Number of failed students: xx
Overall:
Number of students who passed all subjects: xx
Number of students who passed 3 or more subjects: xx
Number of students who passed 2 or more subjects: xx
Number of students who passed 1 or more subjects: xx
Number of students who failed all subjects: xx
Then, the main menu is printed again.
Exiting SPMS
If you choose 0 from the main menu, the program should terminate.
Note that course scores and total score should be formatted as integers, but average scores should
be formatted as a real number with exactly two digits after the decimal point.
Input
There will be a single test case, ending with a zero entered in the main menu screen. The entire input
will be valid. The size of input does not exceed 10KB.
Output
Print out everything as stated in the problem description. You should be able to play around this little
program in your machine, with a keyboard and a screen. However, both the input and output may
look silly when they’re not mixed, as in the keyboard-screen case.
Hint:
When formatting a floating-point number such as Average Score, a good way to prevent floatingpoint errors is to add a small number (like 1e-5 in this problem). Otherwise, 80.315 would be printed
as 80.31 if the floating-point error makes it 80.31499999…
Sample Input
1
0011223344 1 John 79 98 91 100
0022334455 1 Tom 59 72 60 81
0011223344 2 Alice 100 100 100 100
2423475629 2 John 60 80 30 99
0
3
0022334455
John
0
5
1
2
0011223344
0
5
0
4
0
Sample Output
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Duplicated SID.
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Please enter the SID, CID, name and four scores. Enter 0 to finish.
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter SID or name. Enter 0 to finish.
2 0022334455 1 Tom 59 72 60 81 272 68.00
Please enter SID or name. Enter 0 to finish.
1 0011223344 1 John 79 98 91 100 368 92.00
3 2423475629 2 John 60 80 30 99 269 67.25
Please enter SID or name. Enter 0 to finish.
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter class ID, 0 for the whole statistics.
Chinese
Average Score: 69.00
Number of passed students: 1
Number of failed students: 1
Mathematics
Average Score: 85.00
Number of passed students: 2
Number of failed students: 0
English
Average Score: 75.50
Number of passed students: 2
Number of failed students: 0
Programming
Average Score: 90.50
Number of passed students: 2
Number of failed students: 0
Overall:
Number of students who passed all subjects: 1
Number of students who passed 3 or more subjects: 2
Number of students who passed 2 or more subjects: 2
Number of students who passed 1 or more subjects: 2
Number of students who failed all subjects: 0
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter SID or name. Enter 0 to finish.
1 student(s) removed.
Please enter SID or name. Enter 0 to finish.
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Please enter class ID, 0 for the whole statistics.
Chinese
Average Score: 59.50
Number of passed students: 1
Number of failed students: 1
Mathematics
Average Score: 76.00
Number of passed students: 2
Number of failed students: 0
English
Average Score: 45.00
Number of passed students: 1
Number of failed students: 1
Programming
Average Score: 90.00
Number of passed students: 2
Number of failed students: 0
Overall:
Number of students who passed all subjects: 0
Number of students who passed 3 or more subjects: 2
Number of students who passed 2 or more subjects: 2
Number of students who passed 1 or more subjects: 2
Number of students who failed all subjects: 0
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
Showing the ranklist hurts students’ self-esteem. Don’t do that.
Welcome to Student Performance Management System (SPMS).
1 - Add
2 - Remove
3 - Query
4 - Show ranking
5 - Show Statistics
0 - Exit
首先,贴出自己的代码,提交总是wa,我决定不再对我的代码进行调试,并贴在这里作为一个对自己的警示以及要求自己从此道题开始改变下思想,让自己的代码模块化,增加可读性,可调试性。
wrong answer代码(一):
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const double eps=1e-5;
typedef struct{
char sid[20];
int cid;
char name[20];
int a,b,c,d;
int sum(){
return a+b+c+d;
}
int a_pass,b_pass,c_pass,d_pass;
int pass(){
return a_pass+b_pass+c_pass+d_pass;
}
}Student;
Student stu[105];
int len=0;
bool cmp(Student s1,Student s2){
return s1.sum()>s2.sum();
}
void print_0(){
cout<<"Welcome to Student Performance Management System (SPMS)."<<endl<<endl;
cout<<"1 - Add"<<endl;
cout<<"2 - Remove"<<endl;
cout<<"3 - Query"<<endl;
cout<<"4 - Show ranking"<<endl;
cout<<"5 - Show Statistics"<<endl;
cout<<"0 - Exit"<<endl<<endl;
}
int main()
{
int n;
print_0();
while(cin>>n){
switch(n){
case 1:{
cout<<"Please enter the SID, CID, name and four scores. Enter 0 to finish."<<endl;
cin>>stu[len].sid;
if(strlen(stu[len].sid)==1) {print_0();break;}
else{
while(1){
cin>>stu[len].cid>>stu[len].name>>stu[len].a>>stu[len].b>>stu[len].c>>stu[len].d;
stu[len].a_pass=(stu[len].a>=60)?1:0;
stu[len].b_pass=(stu[len].b>=60)?1:0;
stu[len].c_pass=(stu[len].c>=60)?1:0;
stu[len].d_pass=(stu[len].d>=60)?1:0;
bool flag=0;
for(int i=0;i<len;i++){
if(strcmp(stu[len].sid,stu[i].sid)==0){
cout<<"Duplicated SID."<<endl;
flag=1;
break;
}
}
if(!flag) ++len;
cout<<"Please enter the SID, CID, name and four scores. Enter 0 to finish."<<endl;
cin>>stu[len].sid;
if(strcmp(stu[len].sid,"0")==0){print_0();break;}
}
}
break;
}
case 2:{
cout<<"Please enter SID or name. Enter 0 to finish."<<endl;
while(1){
char tmp[50];
int cnt=0;
cin>>tmp;
if(strlen(tmp)==1 && tmp[0]=='0'){print_0();break;}
for(int i=0;i<len;i++){
if(strcmp(tmp,stu[i].name)==0 || strcmp(tmp,stu[i].sid)==0){
for(int j=i;j<len;j++)
stu[j]=stu[j+1];
len--;
cnt++;
}
}
cout<<cnt<<" student(s) removed."<<endl;
cout<<"Please enter SID or name. Enter 0 to finish."<<endl;
}
break;
}
case 3:{
cout<<"Please enter SID or name. Enter 0 to finish."<<endl;
Student stu2[105];
for(int i=0;i<len;i++)
stu2[i]=stu[i];
sort(stu2,stu2+len,cmp);
char tmp[50];
while(1){
cin>>tmp;
int mark[105];
mark[0]=1;
if(strlen(tmp)==1 && tmp[0]=='0') {print_0();break;}
for(int i=1;i<len;i++){
if(stu2[i].sum()==stu2[i-1].sum()) mark[i]=mark[i-1];
else mark[i]=i+1;
}
for(int i=0;i<len;i++){
if(strcmp(tmp,stu[i].name)==0 || strcmp(tmp,stu[i].sid)==0){
int loc;
for(int j=0;j<len;j++){
if(strcmp(stu[i].sid,stu2[j].sid)==0){
loc=j;
break;
}
}
cout<<mark[loc]<<" "<<stu[i].sid<<" "<<stu[i].cid<<" "<<stu[i].name<<" ";
cout<<stu[i].a<<" "<<stu[i].b<<" "<<stu[i].c<<" "<<stu[i].d<<" ";
cout<<stu[i].sum()<<" ";printf("%.2lf\n",stu[i].sum()/4.0+eps);
}
}
}
break;
}
case 4:{
cout<<"Showing the ranklist hurts students¡¯ self-esteem. Don¡¯t do that."<<endl;
print_0();
break;
}
case 5:{
cout<<"Please enter class ID, 0 for the whole statistics."<<endl;
int choice;
cin>>choice;
bool flag=0;
int n1,n2,n3,n4;
int sum1,sum2,sum3,sum4;
int pass_0,pass_1,pass_2,pass_3,pass_4;
int num=0;
n1=n2=n3=n4=0;
sum1=sum2=sum3=sum4=0;
pass_0=pass_1=pass_2=pass_3=pass_4=0;
for(int i=0;i<len;i++){
if(!choice) flag=1;
else flag=(stu[i].cid==choice);
if(flag){
num++;
sum1+=stu[i].a;sum2+=stu[i].b;sum3+=stu[i].c;sum4+=stu[i].d;
if(stu[i].a_pass) n1++;
if(stu[i].b_pass) n2++;
if(stu[i].c_pass) n3++;
if(stu[i].d_pass) n4++;
if(stu[i].pass()==4) pass_4++;
if(stu[i].pass()>=3) pass_3++;
if(stu[i].pass()>=2) pass_2++;
if(stu[i].pass()>=1) pass_1++;
if(stu[i].pass()==0) pass_0++;
}
}
cout<<"Chinese"<<endl;
cout<<"Average Score: ";printf("%.2lf\n",sum1/(num+0.0)+eps);
cout<<"Number of passed students: "<<n1<<endl;
cout<<"Number of failed students: "<<num-n1<<endl;
cout<<endl;
cout<<"Mathematics"<<endl;
cout<<"Average Score: ";printf("%.2lf\n",sum2/(num+0.0)+eps);
cout<<"Number of passed students: "<<n2<<endl;
cout<<"Number of failed students: "<<num-n2<<endl;
cout<<endl;
cout<<"English"<<endl;
cout<<"Average Score: ";printf("%.2lf\n",sum3/(num+0.0)+eps);
cout<<"Number of passed students: "<<n3<<endl;
cout<<"Number of failed students: "<<num-n3<<endl;
cout<<endl;
cout<<"Programming"<<endl;
cout<<"Average Score: ";printf("%.2lf\n",sum4/(num+0.0)+eps);
cout<<"Number of passed students: "<<n4<<endl;
cout<<"Number of failed students: "<<num-n4<<endl;
cout<<endl;
cout<<"Overall:"<<endl;
cout<<"Number of students who passed all subjects: "<<pass_4<<endl;
cout<<"Number of students who passed 3 or more subjects: "<<pass_3<<endl;
cout<<"Number of students who passed 2 or more subjects: "<<pass_2<<endl;
cout<<"Number of students who passed 1 or more subjects: "<<pass_1<<endl;
cout<<"Number of students who failed all subjects: "<<pass_0<<endl;
cout<<endl;
print_0();
break;
}
case 0:{
return 0;
}
}
}
return 0;
}
wrong answer代码(二):
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-5;
typedef struct
{
char sid[15];
int cid;
char name[15];
int score[10];//四门成绩,score[1]-score[4]代表四门成绩
int pass[10];//是否及格,1代表及格,0代表不及格
int pass_sum;
int sum_score()
{
int sum=0;
for(int i=1;i<=4;i++)
sum+=score[i];
return sum;
}
double ave()
{
return 1.0*sum_score()/4.0+eps;
}
}student;
student stu[105];
int len=0;//学生人数
void add()
{
while(1)
{
cout << "Please enter the SID, CID, name and four scores. Enter 0 to finish." << endl;
cin>>stu[len].sid;
if(strcmp(stu[len].sid,"0")==0) break;
cin>>stu[len].cid>>stu[len].name>>stu[len].score[1]>>stu[len].score[2]>>stu[len].score[3]>>stu[len].score[4];
for(int i=1;i<=4;i++)
{
if(stu[len].score[i]>=60) stu[len].pass[i]=1;
else stu[len].pass[i]=0;
}
stu[len].pass_sum=0;
for(int i=1;i<=4;i++) stu[len].pass_sum+=stu[len].pass[i];
bool flag=0;
for(int i=0;i<len;i++)
{
if(strcmp(stu[i].sid,stu[len].sid)==0)
{
flag=1;
break;
}
}
if(flag) cout << "Duplicated SID." << endl;
else len++;
}
}
void del()
{
while(1)
{
cout << "Please enter SID or name. Enter 0 to finish." << endl;
char tmp[15];
int cnt=0;
cin>>tmp;
if(strcmp(tmp,"0")==0) break;
for(int i=0;i<len;i++)
{
if(strcmp(stu[i].sid,tmp)==0 || strcmp(stu[i].name,tmp)==0)
{
cnt++;
for(int j=i;j<len;j++)
{
stu[j]=stu[j+1];
}
len--;
}
}
cout<<cnt<<" student(s) removed."<<endl;
}
}
void person_rank()
{
while(1)
{
cout << "Please enter SID or name. Enter 0 to finish." << endl;
char tmp[15];
cin>>tmp;
if(strcmp(tmp,"0")==0) break;
for(int i=0;i<len;i++)
{
if(strcmp(stu[i].sid,tmp)==0 || strcmp(stu[i].name,tmp)==0)
{
int cnt=1;
for(int j=0;j<len;j++)
{
if(stu[i].sum_score()<stu[j].sum_score()) cnt++;
}
cout<<cnt<<" "<<stu[i].sid<<" "<<stu[i].cid<<" "<<stu[i].name<<" ";
for(int k=1;k<=3;k++) cout<<stu[i].score[k]<<" ";
cout<<stu[i].score[4]<<" "<<stu[i].sum_score()<<" ";
printf("%.2lf\n",stu[i].ave());
}
}
}
}
void info()
{
char subject[10][30]={" ","Chinese","Mathematics","English","Programming","Overall:"};
cout<<"Please enter class ID, 0 for the whole statistics."<<endl;
int class_num;
int stu_number=0;
bool flag;
int subject_sum[10],subject_passed[10],passed_sum[10];
memset(subject_sum,0,sizeof(subject_sum));
memset(subject_passed,0,sizeof(subject_passed));
memset(passed_sum,0,sizeof(passed_sum));
cin>>class_num;
for(int i=0;i<len;i++)
{
if(class_num==0) flag=1;
else flag=(stu[i].cid==class_num);
if(flag)
{
for(int j=1;j<=4;j++)
{
subject_passed[j]+=stu[i].pass[j];
subject_sum[j]+=stu[i].score[j];
}
stu_number++;
if(stu[i].pass_sum==3) passed_sum[3]++;
if(stu[i].pass_sum==2) passed_sum[2]++;
if(stu[i].pass_sum==1) passed_sum[1]++;
if(stu[i].pass_sum==0) passed_sum[0]++;
if(stu[i].pass_sum==4) passed_sum[4]++;
}
}
for(int i=1;i<=4;i++)
{
cout<<subject[i]<<endl;
cout<<"Average Score: ";printf("%.2lf\n",stu_number==0?0:(subject_sum[i]/(stu_number+0.0)+eps));
cout<<"Number of passed students: "<<subject_passed[i]<<endl;
cout<<"Number of failed students:"<<stu_number-subject_passed[i]<<endl;
cout<<endl;
}
cout << "Overall:" << endl;
printf("Number of students who passed all subjects: %d\n", passed_sum[4]);
printf("Number of students who passed 3 or more subjects: %d\n", passed_sum[3]+passed_sum[4]);
printf("Number of students who passed 2 or more subjects: %d\n", passed_sum[2]+passed_sum[3]+passed_sum[4]);
printf("Number of students who passed 1 or more subjects: %d\n", passed_sum[1]+passed_sum[2]+passed_sum[3]+passed_sum[4]);
printf("Number of students who failed all subjects: %d\n", passed_sum[0]);
cout<<endl;
}
void menu()
{
cout << "Welcome to Student Performance Management System (SPMS)." << endl << endl;
cout << "1 - Add" << endl;
cout << "2 - Remove" << endl;
cout << "3 - Query" << endl;
cout << "4 - Show ranking" << endl;
cout << "5 - Show Statistics" << endl;
cout << "0 - Exit" << endl;
cout << endl;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int choice;
while(1)
{
menu();
cin>>choice;
if(choice==1) add();
if(choice==2) del();
if(choice==3) person_rank();
if(choice==4) cout<<"Showing the ranklist hurts students’ self-esteem. Don’t do that."<<endl;
if(choice==5) info();
if(choice==0) break;
}
return 0;
}
坚持不懈调试了两三天,调试时间可以说长达10小时,功夫不负有心人,终于搞出来了,在贴AC代码之前,需要好好说一下本题的烦人之处。
首先本题最大的问题就是,在进行删除操作的时候,进行标记假删除可以顺利AC,但是,我一直进行真删除,结果AC不了,这个题目不知道到底怎么搞的。
另外,本题很坑人的点就是,人数为0的时候以及还有什么EPS精度的要求,这些东西太坑人了,最坑的就是很容易多一个空格少一个空格!!最后我进行重新写代码的过程中,依旧错误,又发现了本题目本身就有错误错误错误:题目中PDF格式文件中的这句话复制过去后,Showing the ranklist hurts students’ self-esteem. Don’t do that.这句话中的单引号竟然变成了中文格式的,真是坑我哎!!
这是刘汝佳《算法入门经典》(第2版)前90页最坑人的题目了。
下面是AC代码(都是心血啊):
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-5;
int len = 0;
typedef struct{
string SID, name;
int CID;
int score[5];
bool removed;
int pass[10];
int pass_sum;
}stu;
stu all[105];
void print_menu()
{
cout << "Welcome to Student Performance Management System (SPMS)." << endl << endl;
cout << "1 - Add" << endl;
cout << "2 - Remove" << endl;
cout << "3 - Query" << endl;
cout << "4 - Show ranking" << endl;
cout << "5 - Show Statistics" << endl;
cout << "0 - Exit" << endl;
cout << endl;
}
void add()
{
while(true){
cout << "Please enter the SID, CID, name and four scores. Enter 0 to finish." << endl;
stu in; cin >> in.SID;
if(in.SID =="0") break;
cin >> in.CID >> in.name >> in.score[1] >> in.score[2] >> in.score[3] >> in.score[4];
in.score[0]=0;
for(int i=1;i<=4;i++) in.score[0]+=in.score[i];
in.removed = false;
for(int i=1;i<=4;i++)
{
if(in.score[i]>=60) in.pass[i]=1;
else in.pass[i]=0;
}
in.pass_sum=0;
for(int i=1;i<=4;i++) in.pass_sum+=in.pass[i];
bool flag=0;
for(int i = 0; i < len; i++)
if(!all[i].removed && in.SID == all[i].SID){
cout << "Duplicated SID." << endl;
flag=1;
break;
}
if(!flag)
all[len++] = in;
}
}
void del()
{
while(true){
cout << "Please enter SID or name. Enter 0 to finish." << endl;
string str; cin >> str;
if(str == "0") break;
int rem_len = 0;
for(int i = 0; i < len; i++){
if(!all[i].removed && (str == all[i].SID || str == all[i].name)) {
{
all[i].removed = true;
rem_len++;
}
}
}
cout<<rem_len<<" student(s) removed."<<endl;
}
}
void person_rank()
{
while(true){
cout << "Please enter SID or name. Enter 0 to finish." << endl;
string str; cin >> str;
if(str == "0") break;
for(int i = 0; i < len; i++){
if(!all[i].removed && (str == all[i].SID || str == all[i].name)) {
int rankk=1;
for(int j=0;j<len;j++)
{
if(!all[j].removed && all[i].score[0]<all[j].score[0])
rankk++;
}
cout<<rankk<<" "<<all[i].SID<<" "<<all[i].CID<<" "<<all[i].name<<" ";
for(int k=1;k<=3;k++) cout<<all[i].score[k]<<" ";
cout<<all[i].score[4]<<" "<<all[i].score[0]<<" ";
printf("%.2f\n", all[i].score[0]/4.0+eps);
}
}
}
}
void info()
{
char subject[10][30]={" ","Chinese","Mathematics","English","Programming","Overall:"};
cout<<"Please enter class ID, 0 for the whole statistics."<<endl;
int class_num;
int stu_number=0;
bool flag;
int subject_sum[10],subject_passed[10],passed_sum[10];
memset(subject_sum,0,sizeof(subject_sum));
memset(subject_passed,0,sizeof(subject_passed));
memset(passed_sum,0,sizeof(passed_sum));
cin>>class_num;
for(int i=0;i<len;i++)
{
if(!all[i].removed && (class_num==0 || class_num==all[i].CID))
{
for(int j=1;j<=4;j++)
{
subject_passed[j]+=all[i].pass[j];
subject_sum[j]+=all[i].score[j];
}
stu_number++;
if(all[i].pass_sum==3) passed_sum[3]++;
if(all[i].pass_sum==2) passed_sum[2]++;
if(all[i].pass_sum==1) passed_sum[1]++;
if(all[i].pass_sum==0) passed_sum[0]++;
if(all[i].pass_sum==4) passed_sum[4]++;
}
}
for(int i=1;i<=4;i++)
{
cout<<subject[i]<<endl;
cout<<"Average Score: ";printf("%.2f\n",stu_number==0?0:(1.0*subject_sum[i]/stu_number+eps));
cout<<"Number of passed students: "<<subject_passed[i]<<endl;
cout<<"Number of failed students: "<<stu_number-subject_passed[i]<<endl;
cout<<endl;
}
cout << "Overall:" << endl;
printf("Number of students who passed all subjects: %d\n", passed_sum[4]);
printf("Number of students who passed 3 or more subjects: %d\n", passed_sum[3]+passed_sum[4]);
printf("Number of students who passed 2 or more subjects: %d\n", passed_sum[2]+passed_sum[3]+passed_sum[4]);
printf("Number of students who passed 1 or more subjects: %d\n", passed_sum[1]+passed_sum[2]+passed_sum[3]+passed_sum[4]);
printf("Number of students who failed all subjects: %d\n", passed_sum[0]);
cout<<endl;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int choice;
while(true){
print_menu();
cin >> choice;
if(choice == 1) add();
if(choice == 2) del();
if(choice == 3) person_rank();
if(choice == 4) cout << "Showing the ranklist hurts students' self-esteem. Don't do that." << endl;
if(choice == 5) info();
if(choice == 0) break;
}
return 0;
}
历经好几天,反复调试100多遍,终于写成最后版本的,如下AC代码:
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-5;
typedef struct
{
string sid,name;
int cid;
int pass[10];//pass[0]表示通过的科目总数,pass[1]-pass[4]表示各科通过与否,1表示通过,0未通过
int score[10];//类似pass的定义
bool removed;//1删除了,0未删除
}stu;
stu all[105];
int len=0;
void menu()
{
cout<<"Welcome to Student Performance Management System (SPMS)."<<endl<<endl;
cout<<"1 - Add"<<endl;
cout<<"2 - Remove"<<endl;
cout<<"3 - Query"<<endl;
cout<<"4 - Show ranking"<<endl;
cout<<"5 - Show Statistics"<<endl;
cout<<"0 - Exit"<<endl<<endl;
}
void add()
{
while(1)
{
cout<<"Please enter the SID, CID, name and four scores. Enter 0 to finish."<<endl;
stu in; cin>>in.sid; if(in.sid=="0") break;
cin>>in.cid>>in.name>>in.score[1]>>in.score[2]>>in.score[3]>>in.score[4];
in.score[0]=0;for(int i=1;i<=4;i++) in.score[0]+=in.score[i];
in.removed=false;
for(int i=1;i<=4;i++)
{
if(in.score[i]>=60) in.pass[i]=1;
else in.pass[i]=0;
}
in.pass[0]=0; for(int i=1;i<=4;i++) in.pass[0]+=in.pass[i];
bool flag=0;
for(int i=0;i<len;i++)
{
if(!all[i].removed && in.sid==all[i].sid){
cout<<"Duplicated SID."<<endl;
flag=1;
break;
}
}
if(!flag) all[len++]=in;
}
}
void del0_rank1(const int dr)
{
while(1)
{
cout<<"Please enter SID or name. Enter 0 to finish."<<endl;
string tmp; cin>>tmp; if(tmp=="0") break;
int cnt=0;
for(int i=0;i<len;i++)
{
if(!all[i].removed && (all[i].sid==tmp || all[i].name==tmp))
{
if(dr==0){
cnt++;
all[i].removed=true;
}
else if(dr==1){
int ranking=1;
for(int j=0;j<len;j++)
if(!all[j].removed && all[j].score[0]>all[i].score[0])
ranking++;
cout<<ranking<<" "<<all[i].sid<<" "<<all[i].cid<<" "<<all[i].name<<" ";
for(int k=1;k<=4;k++) cout<<all[i].score[k]<<" ";cout<<all[i].score[0]<<" ";
printf("%.2f\n",all[i].score[0]/4.0+eps);
}
}
}
if(dr==0) cout<<cnt<<" student(s) removed."<<endl;//student前多个空格,格式错误哎
}
}
void info()
{
cout<<"Please enter class ID, 0 for the whole statistics."<<endl;
char project[10][50]={" ","Chinese","Mathematics","English","Programming"};
int project_sum[10]={0},project_pass[10]={0},stu_number=0,pass_num[10]={0};
int class_id; cin>>class_id;
for(int i=0;i<len;i++)
{
if(!all[i].removed && (class_id==0 || class_id==all[i].cid)){
stu_number++;
for(int j=1;j<=4;j++)
{
project_sum[j]+=all[i].score[j];
project_pass[j]+=all[i].pass[j];
}
pass_num[all[i].pass[0]]++;
}
}
for(int i=1;i<=4;i++)
{
cout<<project[i]<<endl;
cout<<"Average Score: ";printf("%.2f\n",stu_number==0?0:project_sum[i]*1.0/stu_number+eps);
cout<<"Number of passed students: "<<project_pass[i]<<endl;
cout<<"Number of failed students: "<<stu_number-project_pass[i]<<endl<<endl;
}
cout<<"Overall:"<<endl;
cout<<"Number of students who passed all subjects: "<<pass_num[4]<<endl;
cout<<"Number of students who passed 3 or more subjects: "<<pass_num[3]+pass_num[4]<<endl;
cout<<"Number of students who passed 2 or more subjects: "<<pass_num[2]+pass_num[3]+pass_num[4]<<endl;
cout<<"Number of students who passed 1 or more subjects: "<<pass_num[1]+pass_num[2]+pass_num[3]+pass_num[4]<<endl;
cout<<"Number of students who failed all subjects: "<<pass_num[0]<<endl<<endl;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while(1)
{
menu();
int choice;
cin>>choice;
if(choice==0) break;
if(choice==1) add();
if(choice==2) del0_rank1(0);
if(choice==3) del0_rank1(1);
if(choice==4) cout<<"Showing the ranklist hurts students' self-esteem. Don't do that."<<endl;//本句子中的单引号复制过来竟然是中文型的,太坑我了
if(choice==5) info();
}
return 0;
}