C++的分支结构练习题记录【代码】

转载至:https://zhuanlan.zhihu.com/p/609399111

分支结构

一、成绩转换

#include <iostream>
using  namespace std;
int main()
{
    int n;
    cin >> n;
    if ( n >= 90 )
       cout << "A";
       else 
           if ( n >= 80 )
              cout << "B";
           else
               if ( n >= 70 )
                  cout << "C";
               else
                   if ( n>= 60 )
                      cout << "D";
                   else 
                      cout << "F";
    return 0;
}

二、字母判断

#include <iostream>
using  namespace std;
int main()
{
    char letter;
    cin >> letter;

    if ( letter == 'a' || letter == 'e' || letter == 'u' || letter == 'i' || letter == 'o' )
       cout << "vowel" ;
    else
       cout << "consonant" ;

    return 0;
}

三、三个整型数排序

#include <iostream>
using  namespace std;
int main()
{
    int a,b,c;
    cin >> a >> b >> c;
    int transf;
    if ( a <= b ) 
    {
       transf = b;
       b = a;
       a = transf;
    }
    if ( b <= c )
    {
        transf = c;
        c = b;
        b = transf;
    }
    if ( a <= c )
    {
        transf = c;
        c = a;
        a = transf;
     }

    cout << a << ' ' << b << ' ' << c ;
    return 0;
}

四、判断回文数

#include <iostream>
using  namespace std;
int main()
{
    int num ;
    cin >> num ;

    int x1,x2,x4,x5;
    x1 = num / 10000;
    x2 = num / 1000 % 10;
 
    x4 = num % 100 / 10;
    x5 = num % 10;

    if ( x1 == x5 && x2 == x4 )
       cout << "Yes";
    else 
       cout << "No";
       
    return 0;
}

五、字符转换

#include <iostream>
using  namespace std;
int main()
{
    char ch;
    cin >> ch;

    if ( ch >= 'a' && ch <= 'z' )
       ch = 'A' + ch - 'a';
    else 
       if ( ch >= 'A' && ch <= 'Z' )
          ch = 'a' + ch - 'A';
    
    cout << ch ;
    
    return 0;
}

六、停车收费系统

#include <iostream>
using  namespace std;
int main()
{
    char vehicle_type ;
    int in_time, out_time, parking_time, pay ;

    cin >> vehicle_type >> in_time >> out_time;

    parking_time = out_time - in_time;

    if ( vehicle_type == 'c' )
       if ( parking_time <= 3 )
           pay = parking_time * 5 ;
        else 
           pay = parking_time * 10 - 15 ;

    if ( vehicle_type == 'b' )
       if ( parking_time <= 2 )
           pay = parking_time * 10 ;
        else
           pay = parking_time * 15 - 10 ;

    if ( vehicle_type == 't' )
       if ( parking_time <= 1 )
           pay = parking_time * 10 ;
        else
           pay = parking_time * 15 - 5 ;

    cout << pay ;
    return 0;
}

七、第几天

#include <iostream>
using  namespace std;
int main()
{
    int year, month, day;
    cin >> year >> month >> day ;

    int  leapyear = 0 ;
    if ( ( year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 ) )
       leapyear = 1;
    
    int dayNum ;
    dayNum = 31 * ( month - 1 ) + day ;

    if ( month > 2 )
    {  dayNum -= ( 4 * month + 23 ) / 10 ;
       if ( leapyear )
          dayNum++ ;
    }
    
    cout << dayNum ;

    return 0;
}

八、高度是多少

#include <iostream>
using  namespace std;
int main()
{
    double x, y;
    cin >> x >> y;

    double dx, dy, distance_sqrt;  // 距离最近圆心横坐标距离dx,距离最近圆心纵坐标距离dy,距离最近圆心的距离的平方distance_sqrt
    int sign_x, sign_y;            // x,y的符号,负数记为-1,非负数记为1
    int height = 0;

    if ( x >= 0 )
       sign_x = 1;
    else 
       sign_x = -1;
    
    if ( y >= 0 )
       sign_y = 1;
    else
       sign_y = -1;
    
    dx = x - sign_x * 2;
    dy = y - sign_y * 2;
    distance_sqrt = dx * dx + dy * dy ;

    if ( distance_sqrt <= 1 )
       height = 10;
    
    cout << height;
    return 0;
}

九、乒乓球比赛

#include <iostream>
using  namespace std;
int main()
{
    int a, b;
    cin >> a >> b ;

    if ( ( a == 11 && b <= 9 ) || ( a > 11 && b > 9 && ( a - b == 2 ) ) )
       cout << "A win";
    else 
        if ( ( b == 11 && a <= 9 ) || ( b > 11 && a > 9 && ( b - a == 2 ) ) )
           cout << "B win";
        else
           if ( ( a < 11 && b < 11 ) || ( a > 9 && b > 9 && ( a == b || a - b == 1 || b - a == 1) ) )
               cout << "In progress";
            else
               cout << "Illegal";

    return 0;
}

十、三角形判别

#include <iostream>
using  namespace std;
int main()
{
    int a, b, c;
    cin >> a >> b >> c;

    //a,b,c由大到小排序
    int transf;
    if ( a < b )
    {
        transf = b;
        b = a;
        a = transf;
    }
    if ( a < c )
    {
        transf = c;
        c = a;
        a = transf;
    }
    if ( b < c )
    {
        transf = c;
        c = b;
        b = transf;
    }

    // 判断是否为三角形以及是否为直角三角形
    if ( b + c <= a )
       cout << "no" ; 
    else 
    {
        cout << "yes" << endl;
        if ( b * b + c * c == ( a * a ) )
            cout << "yes" ;
        else
            cout << "no" ;
    }

    return 0;
}

十一、鸡兔同笼

#include <iostream>
using  namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    
    int chicken_num,rabbit_num;

    if ( m <= n * 2 || m >= n * 4 || m %2 == 1 )
       cout << "no solution";
    else 
    {
        rabbit_num = m/2 - n;
        chicken_num = n - rabbit_num;
        cout << chicken_num << " " << rabbit_num;
    }
    return 0;
}

十二、第几天(判断日期是否合法)

#include <iostream>
using  namespace std;
int main()
{
    int year, month, day ;
    cin >> year >> month >> day ;
    
    //合法性判断
    bool flag = 1, leapyear = 0 ;                       // 日期是否合法flag,是否为闰年leapyear
    if ( ( year % 4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 ) )
       leapyear = 1;
    if ( month > 12 )
        flag = 0;
    else
        switch ( month )
        {
            case 2:
                   if ( leapyear )
                    {
                        if ( day > 29 )
                           flag = 0;
                    }  
                    else
                    {
                        if ( day > 28 )
                            flag = 0;
                    }
                    break;
            case 4:
            case 6:
            case 9:
            case 11:
                   if ( day > 30 )
                      flag = 0;
                    break;
            default :
                   if ( day > 31 )   
                      flag = 0;        
        }
   
    //计算天数或输出非法
    if ( flag )
    {
        int dayNum ;
        dayNum = 31 * ( month - 1 ) + day ;

        if ( month > 2 )
           {  
               dayNum -= ( 4 * month + 23 ) / 10 ;
               if ( leapyear )
                   dayNum++ ;
           }
        cout << dayNum ;
    }
    else
        cout << "Illegal" ;

    return 0;
}

十三、加法(考虑溢出)

#include <iostream>
using  namespace std;
int main()
{
    int a, b;
    cin >> a >> b;

    long long a_long_long, b_long_long, sum_long_long;
    a_long_long = a;
    b_long_long = b;
    sum_long_long = a_long_long + b_long_long;

    if ( sum_long_long >= -2147483648 && sum_long_long <= 2147483647 )
        cout << sum_long_long ;
    else
        cout << "error" ;
    
    return 0;
}

十四、输出数字

#include <iostream>
using  namespace std;
int main()
{
    int n, m, transf;
    char ch;
    cin >> n >> m >> ch;
    switch ( ch ){
        case '>':
                 if ( n < m ){
                     transf = m;
                     m = n;
                     n = transf;
                 }
                 cout << n << " " << m ;
                 break;
        case '<':
                 if ( n > m ){
                     transf = m;
                     m = n;
                     n = transf;
                 }
                 cout << n << " " << m ;
                 break;
        default:
                cout << "error" ;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值