原题描述:
批改多选题是比较麻烦的事情,本题就请你写个程序帮助老师批改多选题,并且指出哪道题错的人最多。
输入格式:
输入在第一行给出两个正整数N(<=1000)和M(<=100),分别是学生人数和多选题的个数。随后M行,每行顺次给出一道题的满分值(不超过5的正整数)、选项个数(不少于2且不超过5的正整数)、正确选项个数(不超过选项个数的正整数)、所有正确选项。注意每题的选项从小写英文字母a开始顺次排列。各项间以1个空格分隔。最后N行,每行给出一个学生的答题情况,其每题答案格式为“(选中的选项个数 选项1 ……)”,按题目顺序给出。注意:题目保证学生的答题情况是合法的,即不存在选中的选项数超过实际选项数的情况。
输出格式:
按照输入的顺序给出每个学生的得分,每个分数占一行。注意判题时只有选择全部正确才能得到该题的分数。最后一行输出错得最多的题目的错误次数和编号(题目按照输入的顺序从1开始编号)。如果有并列,则按编号递增顺序输出。数字间用空格分隔,行首尾不得有多余空格。如果所有题目都没有人错,则在最后一行输出“Too simple”。
输入样例:3 4 3 4 2 a c 2 5 1 b 5 3 2 b c 1 5 4 a b d e (2 a c) (2 b d) (2 a c) (3 a b e) (2 a c) (1 b) (2 a b) (4 a b d e) (2 b d) (1 e) (2 b c) (4 a b c d)输出样例:
3 6 5 2 2 3 4
1.用结构体来存储这道题的信息,包括满分,选项个数,正确选项个数,和正确答案。可以将正确答案里面的字符换成数字,也可以换成字符串(这里换成了字符串),但是要记得在最后加上\0表示字符串结束。
2.由于输入的格式不是单纯的数字,所以还有用getchar()来读入括号和空格。
3.因为要输出错误的最多的题目数,所以还有用数组记录每道题错误的次数。
4.变量比较多,之前把正确答案和错误答案都存在了结构体里,导致变量引用混乱。。。。所以单独分出来了。
#include <stdio.h>
#include <string.h>
typedef struct PROBLEM{
int score;
int choice ;
int choice_right;
char c[10] ;
}Problem;
Problem pro[ 1001 ] ;
int main ( )
{
int N , M ;
int right[ 1001 ] = { 0 } ;
int wrong[ 1001 ] = { 0 } ;
scanf("%d %d",&N , &M );
int i , num , j , k ;
for ( i = 0 ; i < M ; i ++ )
{
scanf("%d %d %d",&pro[ i ].score, &pro[ i ].choice, &pro[ i ].choice_right );
num = 0 ;
for ( j = 0 ; j < pro[ i ].choice_right ; j ++ )
{
getchar ( ) ; //读入空格
scanf("%c",&pro[ i ].c[ num++ ] );
}
}
char a [ 10 ] ;
getchar( );
int temp; //输入的答案个数
for ( i = 0 ; i < N ; i ++ )
{
for ( j = 0 ; j < M ; j ++ )
{
getchar( ) ; //读入左括号
scanf("%d",&temp );
for ( k = 0 ; k < temp ; k ++ )
{
getchar( ); //读入空格
scanf("%c",&a [ k ] );
}
a [ temp ] = '\0' ;
getchar ( ) ; //读入右括号
getchar ( ) ; //读入空格
if ( !strcmp ( a , pro[ j ].c ) )
right [ i ] += pro [ j ].score ;
else
wrong[ j ] ++;
}
}
for ( i = 0 ; i < N ; i ++ )
printf("%d\n", right [ i ] ) ;
int max = 0 ;
for ( i = 0 ; i < M ; i ++ )
{
if ( max < wrong [ i ] )
max = wrong [ i ] ;
}
if ( max == 0 )
{
printf("Too simple");
return 0;
}
printf("%d",max);
for ( i = 0 ; i < M ; i++ )
{
if ( wrong [ i ] == max )
printf(" %d",i+1 );
}
return 0;
}
还看到了一种将字符转化为二进制的。神奇。
#include <stdio.h>
#include <stdlib.h>
typedef struct prob{
int score;
int answer; /* bitwise storage for at most 5 options */
int wrong;
} Prob;
/* read 'count option1 ...' format */
int readanswer()
{
char c;
int count, answer = 0;
scanf("%d", &count);
for(int k = 0; k < count; k++)
{
while((c = getchar()) == ' ');
answer |= 1 << (c - 'a');
}
return answer;
}
int main()
{
int N, M, max = 0, useless;
Prob probs[100];
/* read the answers for each problem */
scanf("%d %d", &N, &M);
for(int i = 0; i < M; i++)
{
scanf("%d %d", &probs[i].score, &useless);
probs[i].wrong = 0;
probs[i].answer = readanswer();
}
/* read every student's answer */
for(int i = 0; i < N; i++)
{
int score = 0;
for(int j = 0; j < M; j++)
{
/* read answer for one problem */
while(getchar() != '(');
if(readanswer() == probs[j].answer) /* If it is right */
score += probs[j].score;
else if(max < ++probs[j].wrong) /* If most students got it wrong */
max = probs[j].wrong;
while(getchar() != ')');
}
printf("%d\n", score);
}
if(max == 0)
printf("Too simple");
else
{
printf("%d", max);
for(int i = 0; i < M; i++) if(probs[i].wrong == max)
printf(" %d", i + 1);
}
return 0;
}
原文见链接:http://www.jianshu.com/p/4a8a570e41c3