PAT (Basic Level) Practice (中文)1031-1032

1031 查验身份证 (15 分)

一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:

首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后将计算的和对11取模得到值Z;最后按照以下关系对应Z值与校验码M的值:

Z:0 1 2 3 4 5 6 7 8 9 10
M:1 0 X 9 8 7 6 5 4 3 2

现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。

输入格式:

输入第一行给出正整数N(≤100)是输入的身份证号码的个数。随后N行,每行给出1个18位身份证号码。

输出格式:

按照输入的顺序每行输出1个有问题的身份证号码。这里并不检验前17位是否合理,只检查前17位是否全为数字且最后1位校验码计算准确。如果所有号码都正常,则输出All passed

输入样例1:

4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X

输出样例1:

12010X198901011234
110108196711301866
37070419881216001X

输入样例2:

2
320124198808240056
110108196711301862

输出样例2:

All passed
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
using namespace std;
int main()
{
	int N;
	cin>>N;
	string s;
	queue<string> q;
	int quan[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
	int z[]={1,0,10,9,8,7,6,5,4,3,2};
	while(N--)
	{
		cin>>s;
		int sum=0;
		int i;
		for(i=0;i<s.length()-1;i++)
		{
			if(s[i]=='X')
			  sum+=10*quan[i];
			else
			  sum+=(s[i]-'0')*quan[i];
		}
		int index=sum%11;
		if(z[index]==10)
		{
			if(s[i]!='X')
			   q.push(s);
		}
		else
		{
			if((s[i]-'0')!=z[index])
			  q.push(s);
		}
	}
	if(q.empty())
	   cout<<"All passed"<<endl;
	else
	{
		while(q.size())
		{
			cout<<q.front()<<endl;
			q.pop();
		}
	}
	return 0;
}

1032 挖掘机技术哪家强 (20 分)

为了用事实说明挖掘机技术到底哪家强,PAT 组织了一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。

输入格式:

输入在第 1 行给出不超过 10​5​​ 的正整数 N,即参赛人数。随后 N 行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从 1 开始连续编号)、及其比赛成绩(百分制),中间以空格分隔。

输出格式:

在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。

输入样例:

6
3 65
2 80
1 100
2 70
3 40
3 0

输出样例:

2 150
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
typedef struct school
{
	int sh;
	int score;
}school;
struct Rule
{
	bool operator()(const school&s1,const school&s2)
	{
		return s1.score>s2.score;
	}
};
int main()
{
	map<int,int>mp;
	int N;
	cin>>N;
	set<school,Rule>st;
	while(N--)
	{
		int n,m;
		cin>>n>>m;
		mp[n]+=m;
	}
	for(map<int,int>::iterator i=mp.begin();i!=mp.end();i++)
	{
		school s;
		s.sh=i->first;
		s.score=i->second;
		st.insert(s);
	}
	set<school,Rule>::iterator i=st.begin();
	school s=*i;
	cout<<s.sh<<" "<<s.score;
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_41066584/article/details/94594014