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 行給出不超過 105 的正整數 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;
}