L1-056 猜数字 (20 分)
#include <bits/stdc++.h>
using namespace std;
string s[10010], mins;
double num[10010], n, ave = 0, minn = 110;
int main(void)
{
//读入
cin >>n;
for(int i = 1; i <= n; i++){
cin >>s[i] >>num[i];
ave += num[i];
}
//求解最接近的人
ave /= (n * 2);
for(int i = 1; i <= n; i++){
if(abs(num[i] - ave) < minn){
minn = abs(num[i] - ave);
mins = s[i];
}
}
cout <<(int)ave <<" " <<mins;
return 0;
}
L1-057 PTA使我精神焕发 (5 分)
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
cout <<"PTA shi3 wo3 jing1 shen2 huan4 fa1 !";
return 0;
}
L1-058 6翻了 (15 分)
Notice:
注意需要特判一下以6结尾的字符串,因为含有空格,读入数据用getline,其他的按题意来就行
#include <bits/stdc++.h>
using namespace std;
string s;
int main(void)
{
int cnt = 0;
getline(cin, s);
for(int i = 0; s[i]; i++){
if(s[i] == '6') cnt++;
if(s[i] != '6'){
if(cnt != 0){
if(cnt > 9) cout <<"27";
else if(cnt > 3) cout <<"9";
else{
for(int i = 1; i <= cnt; i++) cout <<"6";
}
cnt = 0;
}
cout <<s[i];
}
}
//对末尾含有6的情况进行处理
if(cnt > 0){
if(cnt > 9) cout <<"27";
else if(cnt > 3) cout <<"9";
else{
for(int i = 1; i <= cnt; i++) cout <<"6";
}
}
return 0;
}
L1-059 敲笨钟 (20 分)
Notice:
这道题我主要是用了一下字符串的查找功能find,通过对句号和逗号的定位来判断押韵
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int main(void)
{
//输入,注意读掉换行符号
scanf("%d", &n);
getchar();
for(int i = 1; i <= n; i++){
getline(cin,s);
//找到句号和逗号,判断句子是否以ong结尾
int pos1 = s.find(',');
int pos2 = s.find('.');
if(s[pos1-1] == 'g' && s[pos2-1] == 'g'
&& s[pos1-2] == 'n' && s[pos2-2] == 'n'
&& s[pos1-3] == 'o' && s[pos2-3] == 'o'){
//成立时定义结果字符串
string res = "qiao ben zhong.";
int cnt = 0;
//从后向前构造结果字符串
for(int i = s.length()-1; i >= 0; i--){
if(s[i] == ' ') cnt++;
if(cnt > 2){
res = s[i] + res;
}
}
//输出
cout <<res;
}
else
cout <<"Skipped";
//输出换行
if(i != n) cout <<endl;
}
return 0;
}
L1-060 心理阴影面积 (5 分)
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
double x, y;
cin >>x >>y;
double ans1 = x*y/2;
double ans2 = (100-x) * y;
double ans3 = (100-y) * (100-x) / 2;
cout <<5000-ans1-ans2-ans3;
return 0;
}