09.指针内部选拔竞赛

1.吃瓜群众

问题:语文能力太差读不明白题目。。。。。

#include<iostream>
using  namespace std;

int main() {
    int weight;cin>>weight;
    if(weight%2==0&&weight!=2)
        cout<<"YES, you can divide the watermelon into two even parts.";
    else
        cout<<"NO, you can't divide the watermelon into two even parts.";
    return 0;
}

2.判断闰年

口诀:四年一润并且百年不润,或者400年一润

2000年时闰年,但是1900年不是闰年

#include <iostream>
using namespace std;

int main(){
    int year;cin>>year;
    if(year%4==0&&year%100!=0||yaer%400==0)
        cout<<"yes";
    else 
        cout<<"no";
    return 0;
}

3.统计单词计数

算法思想:

1。它统计的是单词的出现不是字符串的出现,单词的俩边都会有空格,所以真正要查询的是空格sub空格,而不是空格就比如要查询to那么你toto并不会被计数加加,因为不单词匹配

2.里面while(大串查小串(俩个参数))循环查询,cnt++,知道查不到位置

#include<iostream>
#include<string>
using namespace std;
string s,subs;
void tolower(string& s1){
    for(auto& c:s1){
        if(c>='A'&&c<='Z') c+=32;
    }
}
int main(){
    getline(cin,subs);
    getline(cin,s);
    tolower(subs);
    tolower(s);
    s=" "+s+" ";// to
    subs=" "+subs+" ";// to be or be is a question
    int pos=0,cnt=0,x,ans=-1;
    while((pos=s.find(subs,pos))!=-1){
        cnt++;
        if(ans==-1) ans=pos;
        pos++;
    }
    if(cnt) cout<<cnt<<" "<<ans;
    else cout<<ans;
}

4.跑毒

算法思想:贪心算法,血量少且足够打完急救包时的时候用急救包最为合适

#include <iostream>

using namespace std;

int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        int hp=100,a,b,c;
        cin>>a>>b>>c;
        while(hp>0){
            hp-=a;
            b--;
            if(hp<=7*a&&c>0){
                hp=80;
                c--;
                //打急救包不走b不变
            }
        }
        if(b>0) cout<<"NO"<<endl;
        else    cout<<"YES"<<endl;
    }

    //a 扣血速度 b是距离安全区距离 c是急救包

    return 0;
}

5.最大公约数与最小公倍数的递归算法

#include<iostream>
#include<string>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
int gcd(int a,int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int main(){
    int a,b;cin>>a>>b;
    cout<<"最大公约数为:"<<
    gcd(a,b)<<endl;
    cout<<"最小公倍数:"<<a/gcd(a,b)*b<<endl;
    return 0;
}

整常而言,lcm=ab/gcd;但是ab很又可能越界所以可以lcm=a/gcd*b

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值