PAT 1153 C++ 版

版权声明:如若转载,请联系作者。 https://blog.csdn.net/liu16659/article/details/86772496

PAT 1153 C++版【存在问题】

1.题意

给出一串参加pat考试的人的名单。接着给出一系列查询的名单,让你输出其查询结果。

2.分析

题目比较繁琐。我的主要思想如下:

  • 针对不同的输入type,进行不同的测试程序,得到不同的执行结果。

3.代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<iostream>

using namespace std;

struct student{
	char regId[15];
	int score;
};

struct result3{
	int site;
	int num;
};

student stu[10005];
student res[10005];
result3 res3[10005];



int cmp(student s1,student s2){
	if(s1.score == s2.score) return strcmp(s1.regId,s2.regId) < 0;
 	return s1.score > s2.score;
}

int cmp2(result3 r1,result3 r2){
	if(r1.num == r2.num) return r1.site< r2.site;
	return r1.num > r2.num;
}

int main(){
	int N,M;
	cin >> N>> M;
	int i ,j;
	for(i = 0;i< N;i++){
		cin >> stu[i].regId >> stu[i].score;
	}
	
	int type;
	char level;//如果type是1的时候,则输入level 
	int term;//如果tupe是2/3 ,则输入term 
	int flag ;
	for(i = 0;i< M;i++){
		flag = 0;
		cin >> type;
		if(type == 1){
			cin >> level;//开始处理 
			int index = 0;
			cout << "Case "<< i+1 <<": "<<type << " " << level<< "\n";						
			for(j = 0;j< N;j++){
				if(stu[j].regId[0] == level){
					flag = 1;//说明有 
					strcpy(res[index].regId,stu[j].regId);
					res[index].score = stu[j].score;
					index ++;					
				}
			}
			if(flag == 0){
				cout << "NA" << "\n";
			}else{
				sort(res,res+index,cmp);
				for(int k = 0;k< index;k++){
					cout << res[k].regId << " "<< res[k].score<<"\n";
				}
			}
		}
		else if(type == 2){
			cin >> term;
			int testees = 0;
			int sum = 0;
			int site = 0;
			for(j = 1;j < N;j++){
				site = (stu[j].regId[1]-'0') * 100 
				+ (stu[j].regId[2] -'0') * 10 
				+ (stu[j].regId[3] -'0');
				if(site == term){
					flag = 1;
					testees ++;
					sum += stu[j].score;					
				}
			}
			cout << "Case "<< i+1 <<": "<<type << " "<<term<<"\n";
			if(flag == 1) cout << testees << " "<< sum << "\n";
			else cout << "NA" << "\n";
		}
		else if(type == 3){
			cin >> term;
			int array[1000];
			memset(array,0,sizeof(array));
			int date = 0;
			int site= 0;
			int index = 0;
			for(j = 0;j< N;j++){
				date = (stu[j].regId[4]-'0') * 100000 
				+ (stu[j].regId[5] -'0') * 10000
				+ (stu[j].regId[6] -'0') * 1000
				+ (stu[j].regId[7] -'0') * 100
				+ (stu[j].regId[8] -'0') * 10
				+ (stu[j].regId[9] -'0');
								
				if(date == term){
					flag = 1;
					site = (stu[j].regId[1]-'0') * 100 
					+ (stu[j].regId[2] -'0') * 10 
					+ (stu[j].regId[3] -'0');
					array[site]++;
				}
			}
			
			cout << "Case "<< i+1 <<": "<<type << " "<< setfill('0')<<setw(6)<<term<<"\n";			
			if(flag == 1){			
				for(j = 0;j< 1000;j++){
					if(array[j]!=0){
						res3[index].num = array[j];			
						res3[index].site = j;
						index++; 						
					}
				}
				sort(res3,res3+index,cmp2);
				for(j = 0;j < index;j++){
					cout << res3[j].site  <<" "<< res3[j].num << "\n";
				}
			}
			else cout << "NA" << "\n";			
		}
	}
}

4.测试用例

5.执行结果

  • 代码未完全AC。不大清楚哪里出了问题。
  • 因为嫌弃代码写的太丑,我将如下的代码
date = (stu[j].regId[4]-'0') * 100000 
+ (stu[j].regId[5] -'0') * 10000
+ (stu[j].regId[6] -'0') * 1000
+ (stu[j].regId[7] -'0') * 100
+ (stu[j].regId[8] -'0') * 10
+ (stu[j].regId[9] -'0');

换成了

temp = stu[j].regId;				
date = stoi(temp.substr(4,6));

代码倒是简洁了,但是却没有过一个点。【运行超时】看来还是数组的运行效率高 (╯﹏╰)b

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/86772496
今日推荐