团体程序设计天梯赛练习题L1_part 11(56~60)

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UmVfX1BvaW50

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值