首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求大家帮小弟我看看这道题~哪里写错了

2013-03-12 
求大家帮我看看这道题~~~哪里写错了题目描述:今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完

求大家帮我看看这道题~~~哪里写错了


题目描述:
    今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑每题的分值,所以并不是最后的排名。给定录取分数线,请你写程序找出最后通过分数线的考生,并将他们的成绩按降序打印。
输入:
    测试输入包含若干场考试的信息。每场考试信息的第1行给出考生人数N ( 0 < N < 1000 )、考题数M ( 0 < M < = 10 )、分数线(正整数)G;第2行排序给出第1题至第M题的正整数分值;以下N行,每行给出一名考生的准考证号(长度不超过20的字符串)、该生解决的题目总数m、以及这m道题的题号(题目号由1到M)。 
    当读入的考生人数为0时,输入结束,该场考试不予处理。
输出:
    对每场考试,首先在第1行输出不低于分数线的考生人数n,随后n行按分数从高到低输出上线考生的考号与分数,其间用1空格分隔。若有多名考生分数相同,则按他们考号的升序输出。
样例输入:
4 5 25
10 10 12 13 15
CS004 3 5 1 3
CS003 5 2 4 1 3 5
CS002 2 1 2
CS001 3 2 3 5
1 2 40
10 30
CS001 1 2
2 3 20
10 10 10
CS000000000000000001 0
CS000000000000000002 2 1 2
0
样例输出:
3
CS003 60
CS001 37
CS004 37
0
1
CS000000000000000002 20

前面几个都通过了。就是最后一个测试出了问题。我感觉是因为那个0的问题,但是还是没改过来。大家帮我看一下吧。以下是代码。
#include<iostream>
#include<string>
#include<algorithm>
#include<stdio.h>
using namespace std;
typedef struct
{
        string id;
        int score;
}Person;
bool cmp(Person p1,Person p2)
{
        if (p1.score!=p2.score)
        {
                return p1.score>p2.score;
        }
        else
        {
                return p1.id<p2.id;
        }
}
int main()
{
    Person p[1000];
    int fen[11];
    int n,m,jige;
    while(scanf("%d",&m)!=EOF)
    {
        if(m==0)
        {
            break;
        }
        scanf("%d%d",&n,&jige);
        int renshu=0;
        for(int i=0;i<n;i++)//输入每个分数的分值;
        {
            scanf("%d",&fen[i+1]);
        }
        for(int j=0;j<m;j++)//m个人,每次循环
        {
            int cnt=0;
            cin>>p[j].id;//输入每个人的id
            scanf("%d",&cnt);//记录每个人做对了多少题
            //printf("%d",cnt);

                for(int k=0;k<cnt;k++)//计算每个人的得分
            {


                int tmp=0;
                scanf("%d",&tmp);//输入做对的题号
                p[j].score+=fen[tmp];//每次把做对的题加到得分中
                //printf("%d\n",p[j].score);
            }



        }
        sort(p,p+m,cmp);//按照自己设定的规则进行排序

         for(int q=0;q<m;q++)
         {
             if(p[q].score>=jige)
               renshu++;
         }
         printf("%d\n",renshu);
        for(int d=0;d<m;d++)
        {
            if(p[d].score>=jige)
            {
                 cout<<p[d].id<<" "<<p[d].score<<endl;
                 p[d].score=0;
            }
        }
    }

    return 0;
}


[解决办法]
你在“scanf("%d",&cnt);//记录每个人做对了多少题”之后加一个判断:
if( 0 == cnt ) p[ j ].score = 0;
[解决办法]
我用C写的源代码:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 考生结构体
typedef struct {
char cstrName[ 100 ];
int iSolved;
int iScore;
} examinee;

// 比较函数
int cmp( const void *vl, const void *vr)
{
if( ( * ( examinee * ) vl ).iScore != ( * ( examinee * ) vr ).iScore )
return ( * ( examinee * ) vl ).iScore < ( * ( examinee * ) vr ).iScore;
else
return strcmp( ( * ( examinee * ) vl ).cstrName, ( * ( examinee * ) vr ).cstrName );
}

int main( int argc, char *argv[ ] )
{
// 成员变量
examinee seCandidate[ 1000 ]; // 假设有一个1000个考生
int iTopicPoint[ 1000 ] = { 0 }; // 每道题目的分数
int iPersonCount = 0; // 实际考生个数
int iTopicCount = 0; // 考试题目个数
int iPassLine = 0; // 考试及格线
int i = 0, j = 0;
int iTmp = 0;
int iPassNumber = 0; // 多少个人及格了



while( scanf( "%d", &iPersonCount ) && ( 0 != iPersonCount ) ) // 考生数目大于零
{
iPassNumber = 0;

scanf("%d %d", &iTopicCount, &iPassLine);

// 输入每道题目的分数
for( i = 0; i < iTopicCount; ++i )
scanf( "%d", &iTopicPoint[ i ]);

// 输入每个考生的考试情况
for ( i = 0; i < iPersonCount; ++i )
{
scanf( "%s %d", seCandidate[ i ].cstrName, &seCandidate[ i ].iSolved );

if( 0 == seCandidate[ i ].iSolved ) // 这个考生一题都没答出来
seCandidate[ i ].iScore = 0;
else
{
seCandidate[ i ].iScore = 0;

for( j = 0; j < seCandidate[ i ].iSolved; ++j ) // 算总分
{
scanf( "%d", &iTmp);
seCandidate[ i ].iScore += iTopicPoint[ iTmp - 1];
}
}

if( seCandidate[ i ].iScore >= iPassLine )
++iPassNumber; // 及格人数加1
}

qsort( seCandidate, iPersonCount, sizeof(examinee), cmp);

printf( "%d\n", iPassNumber );

for( i = 0; i < iPersonCount; ++i ) // 输出及格考生的考试总成绩
if( seCandidate[ i ].iScore >= iPassLine )
printf( "%s %d\n", seCandidate[ i ].cstrName, seCandidate[ i ].iScore );
}

system( "pause" );
return 0;
}



运行结果:
求大家帮小弟我看看这道题~哪里写错了

希望我的回答能解决你的问题。

热点排行