L1-016 查验身份证 (15 分)
#include <bits/stdc++.h>
using namespace std;
int a[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char c[] = {'1','0','X','9','8','7','6','5','4','3','2'};
int t;
bool res = true;
void solve()
{
string s;
cin >>s;
int ans = 0;
for(int i = 0; i < 17; i++)
ans += (s[i] - '0')*a[i];
ans %= 11;
if(c[ans] != s[17])
res = false, cout <<s <<endl;
}
int main(void)
{
cin >>t;
while(t--)
solve();
if(res) cout <<"All passed";
return 0;
}
L1-017 到底有多二 (15 分)
#include <bits/stdc++.h>
using namespace std;
string s;
//标记负数和偶数
bool ans5, ans2;
int main(void)
{
cin >>s;
ans2 = ans5 = false;
int len = s.length(), cnt = 0;
for(int i = 0; s[i]; i++){
if(s[i] == '-')
ans5 = true;
else
if(s[i] == '2') cnt++;
}
//偶数判断
if((s[len - 1] - '0')%2 == 0) ans2 = true;
//结果计算
double res;
if(ans5) res = cnt*1.0/(len - 1);
else res = cnt*1.0/len;
if(ans2) res *= 2.0;
if(ans5) res *= 1.5;
res *= 100;
printf("%.2lf%%", res);
return 0;
}
L1-018 大笨钟 (10 分)
Notice:
1.这个题用scanf / printf比较简单一点
2.Only %02d:%02d. Too early to Dang. 两句话中间是两个空格
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
int h, m;
scanf("%d:%d", &h, &m);
if((h >= 0 && h <= 11) || (h == 12 && m == 0))
printf("Only %02d:%02d. Too early to Dang.", h, m);
else{
int cnt;
h -= 12;
if(m == 0) cnt = h;
else
cnt = h + 1;
for(int i = 1; i <= cnt; i++)
cout <<"Dang";
}
return 0;
}
L1-019 谁先倒 (15 分)
Notice:
因为题上给出的是能喝多少杯 x ,所以判断倒下的条件应该是所喝杯数 cnt > x
#include <bits/stdc++.h>
using namespace std;
int a, b, n;
int a1[110], a2[110], b1[110], b2[110];
int main(void)
{
cin >>a >>b; cin >>n;
for(int i = 1; i <= n; i++)
cin >>a1[i] >>a2[i] >>b1[i] >>b2[i];
int cnta, cntb;
cnta = cntb = 0;
for(int i = 1; i <= n; i++){
int num = a1[i] + b1[i];
//同输同赢处理
if(num!=a2[i] && num!=b2[i]) continue;
if(num==a2[i] && num==b2[i]) continue;
//
if(num == a2[i]){
cnta++;
if(cnta == a + 1){
cout <<'A' <<endl;
cout <<cntb;
return 0;
}
}
if(num == b2[i]){
cntb++;
if(cntb == b + 1){
cout <<'B' <<endl;
cout <<cnta;
return 0;
}
}
}
}
L1-020 帅到没朋友 (20 分)
#include <bits/stdc++.h>
using namespace std;
int t, n, x;
bool cnt[100000];
vector<int>v;
int main(void)
{
memset(cnt,false,sizeof(cnt));
//读入并标记是否没朋友
cin >>t;
while(t--){
cin >>n;
if(n == 1)
cin>>x;
else{
while(n--){
cin >>x;
cnt[x] = true;
}
}
}
//读入检测数据并将没朋友的序号放进向量
bool ans = true;
cin >>n;
for(int i = 1; i <= n; i++){
cin >>x;
if(!cnt[x]){
v.push_back(x);
if(i != n)
cnt[x] = true; ans = false;
}
}
//输出
if(v.empty()) cout <<"No one is handsome";
else{
int num = v.size();
for(int i = 0; i < num; i++){
printf("%05d",v[i]);
if(i != num - 1) cout <<" ";
}
}
}