1012 数字分类 (20 分)
#include <bits/stdc++.h>
using namespace std;
int main()
{
int A1 = 0, A2 = 0, A3 = 0, A5 = 0;
double A4 = 0.0;
int a;
int k = 1;
int cntA2 = 0, cntA4 = 0;
int N;
cin >> N;
while (N--)
{
cin >> a;
if (a % 10 == 0)
A1 += a;
else if (a % 5 == 1)
{
A2 = A2 + k * a;
k = -k;
cntA2++;
}
else if (a % 5 == 2)
A3++;
else if (a % 5 == 3)
{
A4 += a;
cntA4 = cntA4 + 1;
}
else if (a % 5 == 4)
{
if (a > A5)
A5 = a;
}
}
if (cntA4 > 0)
A4 = A4 / cntA4;
if (A1 == 0)
cout << "N"
<< " ";
else if (A1 > 0)
cout << A1 << " ";
if (cntA2 == 0)
cout << "N"
<< " ";
else if (cntA2 > 0)
cout << A2 << " ";
if (A3 == 0)
cout << "N"
<< " ";
else if (A3 > 0)
cout << A3 << " ";
if (A4 == 0.0)
cout << "N"
<< " ";
else if (A4 > 0.0)
printf("%.1f ", A4);
if (A5 == 0)
cout << "N";
else if (A5 > 0)
cout << A5;
return 0;
}
1013 数素数 (20 分)
#include<bits/stdc++.h>
using namespace std;
const int maxn = 200010;
bool judge[maxn];
int pri[maxn], cnt = 0,ans=0;
void getprime(int n)
{
for (int i = 2; i <= n; i++)
{
if (!judge[i])
pri[cnt++] = i;
for (int j = 0; j < cnt; j++)
{
if (i * pri[j] > n)
break;
judge[i * pri[j]] = 1;
if (i % pri[j] == 0)
break;
}
}
}
int main(){
ios::sync_with_stdio(0);
int n, m;
cin >> n >> m;
int cnt = 0;
getprime(200010);
for (int i = n; i <= m;i++){
cnt++;
cout << pri[i - 1];
if(cnt%10==0||cnt==m-n+1)
cout << endl;
else
cout << " ";
}
return 0;
}
1014 福尔摩斯的约会 (20 分)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int flag = 0;
string s1, s2, s3, s4;
cin >> s1 >> s2 >> s3 >> s4;
string weight[7] = {
"MON","TUE","WED","THU","FRI","SAT","SUN" };
for (int i = 0; i < s1.size()&&i<s2.size(); i++)
{
if (flag)
{
if (s1[i]<='9'&&s1[i]>='0' && s1[i] == s2[i])
{
printf("%02d:", (s1[i] - '0'));
break;
}
else if (s1[i] >= 'A'&&s1[i] <= 'N'&&s1[i] == s2[i])
{
printf("%02d:", (s1[i] - 'A' + 10));
break;
}
}
else if (s1[i] >= 'A'&&s1[i] <= 'G'&&s1[i] && s1[i] == s2[i])
{
flag = 1;
cout << weight[s1[i] - 'A'] << " ";
}
}
for (int i = 0; i < s3.size()&&i<s4.size(); i++)
{
if(isalpha(s3[i])&&s3[i]==s4[i])
{
printf("%02d",i);
}
}
return 0;
}
1031 查验身份证 (15 分)
#include<bits/stdc++.h>
using namespace std;
const int weight[17] = {
7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
const char ch[11] = {
'1','0','X','9','8','7','6','5','4','3','2'};
int check(string s){
int cnt = 0;
for (int i = 0; i < 17;i++)
cnt += (s[i] - '0') * weight[i];
return cnt % 11;
}
int main(){
ios::sync_with_stdio(0);
int n, ans = 0, cnt = 0;
string id;
cin >> n;
for (int i = 0; i < n;i++){
cin >> id;
ans = check(id);
if(ch[ans]==id[17])
cnt++;
else
cout << id << endl;
}
if(cnt==n)
cout << "All passed";
return 0;
}
1017 A除以B (20 分)
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
string s;
int n, t = 0, temp = 0;
cin >> s >> n;
int len = s.size();
t = (s[0] - '0') / n;
if((t!=0&&len>1)||len==1)
cout << t;
temp = (s[0] - '0') % n;
for (int i = 1; i < len;i++){
t = (temp * 10 + s[i] - '0') / n;
cout << t;
temp = (temp * 10 + s[i] - '0') % n;
}
cout << " " << temp;
return 0;
}