【蓝桥杯】

知识框架

No.1 字符串

1.判断字符串中的字符是字母、数字、空格、等等

while ((c=getchar())!='\n'){
 if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z'){ letters++;}

 else if (c == ' '){space++;}

 else if (c >= '0'&&c <= '9'){digit++;}  }

2.比较字符串的大小:

vector<string> s;
string str;
int n=3;
while(n--){
    cin>>str;
    s.push_back(str);
}
//从小到大
sort(s.begin(),s.end());
for(int i=0;i<3;i++){
    cout<<s[i]<<endl;
}

No.2 递归枚举思想

题目:一个正整数n,判断其能否由2的幂相加得到

#include <stdio.h>
#include <math.h>
 
int hh(int n){ //判断这个数字是否是2的幂次方
    if((n&(n-1))==0){
        return 0;
    }else{
        return 1;
    }
}
 
int ff(int n){
    int i,j,z=0;
     
    if(hh(n)==0){//如果它本身是 2 的正整数次幂打印自己
        printf("%d",n);
        return 0;
    }
     
    for(i=1;pow(2,i)<n;i++);
    printf("%.0lf ",pow(2,i-1));
     
    if(hh(n-pow(2,i))==0){//判断剩下的是不是 2 的正整数次幂
        printf("%.0lf",n-pow(2,i-1));
        return 0;
    }else{//不是则进入递归
        ff(n-pow(2,i-1));
    }
}
 
int main() 
{
    int i,j;
    int n,m=0;
    scanf("%d",&n);
    if(n%2!=0){
        printf("-1");
    }else{
        ff(n);
    }
     
    return 0;
}

题目:求N以内的素数:

bool judge(int x)
{
    if(x==2)
        return true;
    if(x<2)
        return false;
    int i;//i用来循环
    for(i=2;i<=sqrt(x);i++)//从2开始循环至sqrt(x)
        if(x%i==0)//如果找到第三个因数
            return false;//返回假
    return true;//如果没有找到,就返回真
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值