牛客网C++刷题记录过程part2【C++语法题】

写在前面:因为这一块比较简单,就快速写了,一个题有哪些注意的地方提两句,没有就不疼了。一共12道,都比较简单;后面难度应该会慢慢上升。

#include <iostream>
using namespace std;

int main() {
    // write your code here......
    int a,b;
    while (cin>>a>>b) {

    if (a<b) {
        int temp=a;
        a=b;
        b=temp;
    }
    cout<<a+b<<" "<<a-b<<" "<<a*b<<" "<<a/b<<" "<<a%b<<endl;

    }
    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int a = 0;
    int b = 0;
    cin >> a;
    cin >> b;
    //write your code here.......
    int temp=a;
    a=b;
    b=temp;
    
    cout << a << " " << b << endl;
    
    return 0;
}


 

#include <iostream>
using namespace std;

int main() {
    
    int a, b, c;
    cin >> a;
    cin >> b;
    cin >> c;
    // write your code here......
    int max=a>b?a:b;
    max=max>c?max:c;
    
    cout<<max<<endl;

    return 0;
}


#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    
    double price;
    cin >> price;

    double cost = 0.0;

    // write your code here.......
    if (price>0 && price < 100) {
        cost =price;
    }else if (price>= 100 && price < 500) {
        cost=price * 0.9;
    }else if (price >=500 && price <2000 ) {
        cost=price * 0.8;
    }else if (price >=2000 && price <5000) {
        cost=price * 0.7;
    }else if (price >=5000) {
        cost=price * 0.6;
    }else {
        cout<<"input error"<<endl;
    }

    cout << setiosflags(ios::fixed) << setprecision(1) << cost << endl;

    return 0;
}

        PS:这个题初看可以switch case 做,但其实不行,因为price是个double型,switch 一般要使用整型变量;


#include <iostream>
using namespace std;

int main() {
   
    double weight;
    double height;

    cin >> weight;
    cin >> height;

    // write your code here......
    double BMI = weight / (height *height) ;
    if (BMI<18.5) {
        cout<<"偏瘦"<<endl;
    }else if (BMI >= 18.5 && BMI <20.9) {
        cout<<"苗条"<<endl;
    }else if (BMI >=20.9 && BMI <24.9){
        cout<<"适中"<<endl;
    }else {
        cout<<"偏胖"<<endl;
    }
    

    return 0;
}


#include <iostream>
using namespace std;

int main() {
    
    int score;
    cin >> score;

    // write your code here......
    if (score>=0 &&score <=59 ) {
        cout<<"差"<<endl;
    }else if (score >= 60 && score <70) {
        cout<<"及格"<<endl;
    }else if (score >=70 && score <80) {
        cout<<"中"<<endl;
    }else if (score >=80 && score <90) {
        cout<<"良"<<endl;
    }else if (score>=90 && score<=100) {
        cout<<"优秀"<<endl;
    }else {
        cout<<"成绩不合法"<<endl;
    }


    return 0;
}


 


#include <iostream>
using namespace std;

int main() {
    
    int month;
    cin >> month;

    // write your code here......
    if (month>=3&&month<=5) {
        cout<<"春季"<<endl;
    }else if (month>=6&&month<=8) {
        cout<<"夏季"<<endl;
    }else if (month>=9&&month<=11) {
        cout<<"秋季"<<endl;
    }else if (month==12||month==1||month==2) {
        cout<<"冬季"<<endl;
    }else {
        cout<<"不合法"<<endl;
    }

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    
    int n;
    cin >> n;
    int sum = 0;
    
    // write your code here......
    for(int i = 0; i <= n ; ++i){
        if(i%2==0){
            sum+=i;
        }
    }

    cout << sum << endl;
    
    return 0;
}


 

#include <iostream>
using namespace std;

int main() {
    
    int n;
    cin >> n;
    long long factorial = 1;
    
    // write your code here......
    while (n!=0) {
        factorial*=n;
        --n;
    }

    cout << factorial << endl;
    
    return 0;
}


#include <iostream>
using namespace std;

int main() {
    
    // write your code here......
    for(int i=100 ; i<1000 ;++i){
        int hundred=i/100;
        int ten=(i%100)/10;
        int bit=i%10;
        int sum=hundred*hundred*hundred+ten*ten*ten+bit*bit*bit;
        if(sum==i){
            cout<<i<<endl;
        }
    }
    

    return 0;
}


#include <iostream>
using namespace std;

int main() {
    
    int n;
    cin >> n;

    // write your code here......
    for(int i=1 ; i <= n ; ++i){
        for(int j =1 ; j<=i ; ++j){
            cout<<j<<" "<<"*"<<" "<<i<<" "<<"="<<" "<<i*j<<"    ";
            if(j==i){
                cout<<endl;
            }
        }
    }
    

    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {

    // write your code here......
    long sum=0;
    for(int i=0;i<10;++i){
        long temp=0;
        for(int j=0;j<=i;++j){
            temp+=9*pow(10,j);
        }
        sum+=temp;
    }
    cout<<sum<<endl;

    return 0;
}

这道题有2个点,一个是10位9超过了int上限,所有结果要用long类型保存;

另一个是每个数字的计算,其实明显有更简单的,即10的n次方-1,我这里没用,是因为第一时间没想到。 


#include <ios>
#include <iostream>
#include <iomanip>
using namespace std;

int main() {

    // 下落的高度和落地的次数
    double h;
    int n;

    cin >> h;
    cin >> n;

    // write your code here......
    // 总路径长度
    double sum=0;
    for(int i=1;i<=n;++i){
        if (i==1) {
            sum+=h;
            h/=2.0;
        }else {
            sum+=h*2.0;
            h/=2.0;
        }

    }
    cout<<fixed<<setprecision(1)<<sum<<" "<<setprecision(1)<<h<<endl;
    

    return 0;
}

iosmanip的使用方法贴在这

(10条消息) C++ 头文件 iomanip_iomanip头文件包含什么_mz's的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值