(初学者)自学C++之蓝桥杯题库1004,1005,1009,1011,1012

1004:母牛的故事(这个本人写的有点偏差,不完全准确,请各位指正!!!)

#include<iostream>
using namespace std;
int f(int n){
    int N[2]={0},sum;
     N[0]=1;
     N[1]=2;
    if(n==1||n==2){
       sum=N[n-1];
    }
    else if(n==3){
        sum=N[n-2]+N[n-3];
    }
    else{
        for(int i=2;i<n;i++){
            sum=f(i)+f(i-1)-1;
        }
    }
    return sum;
}
int main()
{
    int n[4]={0};
    for(int j=0;j<4;j++){
        cin>>n[j];
    if(n[j]>0&&n[j]<55){
    cout<<f(n[j])<<endl;
    }
    }
  return 0;
}

1005:温度转换

#include<iostream>
#include <iomanip>   //精度函数头文件
using namespace std;
int main()
{
    float F,c;
    cin>>F;
    c=5.0*(F-32.0)/9.0;
    cout.setf(ios::fixed);
    cout<<"c="<<fixed<<setprecision(2) <<c<<endl;
    return 0;
}

题目 1009: 数字的处理与判断

#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
    //输入的为字符串
    string x;int len=0,m;
    cin>>x;
    len=x.length();
    cout<<len<<endl;
    for(int i=0;i<len;i++){
        cout<<x[i];
        if(m!=(len-1)){
            cout<<" ";
        }
    }
    cout<<endl;
    for(int j=len-1;j>=0;j--){
        cout<<x[j];
    }
    cout<<endl;
    return 0;
} 

1011:最大公约数,最小公倍数

#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
int main()
{
   int m,n,max=0,min=0;
   cin>>m>>n;
   int Mn=m;
   if(m<n){
       Mn=n;
   }
  for(int i=Mn,j=1;i>0,j<=Mn;i--,j++){
      if(m%i==0&&n%i==0&&max==0){
          max=i;
      }
      if(m*j%n==0&&min==0){
          min=m*j;
      }
  }
   cout<<max<<" "<<min<<endl;
    return 0;
}

1012:字符串分类统计

#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
int main()
{
   int e=0,s=0,k=0,q=0;
   string str;
   getline(cin,str);//why?
   int len=str.length();
   for(int i=0;i<len;i++){
       if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')){
           e++;
       }else if(str[i]>='0'&&str[i]<='9'){
           s++;
       }else if(str[i]==' '){
           k++;
       }else{
           q++;
       }
   }
   cout<<e<<" "<<s<<" "<<k<<" "<<q<<endl;
    return 0;
}

请各位看官,批评指正!应该有许多可以优化的地方!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值