第四章:if else switch使用

/*
 * @Issue: 输入整数a和b,若a²+b²大于100,则输出a²+b²之和的百位以上的数字,否则直接输出a²+b²的和
 * @Author: 一届书生
 * @LastEditTime : 2020-01-17 09:35:32
 */

#include<iostream>
using namespace std;

int main(){
    int a,b,t;
    cin>>a>>b;
    t=a*a+b*b;
    if(t>100)cout<<t/100<<endl;
    else cout<<t<<endl;
    return 0;
}

  

/*
 * @Issue: 题目:输入的正整数是否既是5的倍数,也是7的倍数,是就输出yes,否就输出no
 * @Author: 一届书生
 * @LastEditTime : 2020-01-17 09:34:23
 */
#include<iostream>
using namespace std;

int main(){
    int t;
    while((cin>>t)&&t>0){
        if(t%5==0&&t%7==0)cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
    return 0;
}

  

/*
 * @Issue:根据x取不同范围内的值,计算y对应的结果
 *          x取值       y取值
 *          x<0          0
 *          0<x<=10      x
 *          10<x<=20     10
 *          20<x<40      -0.5x+20
 * @Author: 一届书生
 * @LastEditTime : 2020-01-17 09:40:09
 */
#include<iostream>
using namespace std;


int main(){
    double x,y;
    while(cin>>x){
        if(x<0)cout<<0<<endl;
        else if(x>0&&x<=10)cout<<x<<endl;
        else if(x>10&&x<=20)cout<<10<<endl;
        else if(x>20&&x<40)cout<<-0.5*x+20<<endl;
    }
    return 0;
}

  

/*
 * @Issue: 判断一个数能否分别被3,5,7整除
 * @Author: 一届书生
 * @LastEditTime : 2020-01-17 09:47:33
 */
#include<iostream>
using namespace std;

int main(){
    int t;
    while(cin>>t){
        int a=t%3,b=t%5,c=t%7;
        if(a==0&&b==0&&c==0)cout<<"能同时被3,5,7整除"<<endl;
        else if(a==0&&b==0&&c!=0)cout<<"能被3,5整除,但不能被7整除"<<endl;
        else if(a==0&&b!=0&&c==0)cout<<"能被3,7整除,但不能被5整除"<<endl;
        else if(a!=0&&b==0&&c==0)cout<<"能被5,7整除,但不能被3整除"<<endl;
        else if(a==0&&b!=0&&c!=0)cout<<"能被3整除,但不能被5,7整除"<<endl;
        else if(a!=0&&b==0&&c!=0)cout<<"能被5整除,但不能被3,7整除"<<endl;
        else if(a!=0&&b!=0&&c==0)cout<<"能被7整除,但不能被3,5整除"<<endl;
        else cout<<"都不能被整除"<<endl;
    }
    return 0;
}

  

/*
 * @Issue:用switch语句实现函数关系 
 * @Author: 一届书生
 * @LastEditTime : 2020-01-17 09:52:16
 */
#include<iostream>
using namespace std;

int main(){
    int t;
    while(cin>>t){
        switch(t<0){
            case 1:cout<<-1<<endl;break;
            case 0:switch (t==0)
            {
            case 1:cout<<0<<endl; break;
            default:cout<<1<<endl; break;
            }
        }
    }
    return 0;
}

  

/*
 * @Issue: 两个整数a,b,和一个字符c,c可以代表加减乘除任意一个字符,求(a c b)的值
 * @Author: 一届书生
 * @LastEditTime : 2020-01-17 09:58:23
 */
#include<iostream>
using namespace std;

int main(){
    int a,b;
    char c;
    cin>>a>>b;
    cin>>c;
    switch(c){
        case '+':cout<<a+b<<endl;break;
        case '-':cout<<a-b<<endl;break;
        case '*':cout<<a*b<<endl;break;
        case '/':cout<<a/b<<endl;break;
    }
    return 0;
}

  

/*
 * @Issue: 输入一个复数事,输出其共轭复数,例如输入2+3i,则输出2-3i
 * @Author: 一届书生
 * @LastEditTime : 2020-01-17 10:11:16
 */
#include<iostream>
using namespace std;

int main(){
    int a,b=3;
    char c,d='i';
    cin>>a;
    cin>>c;
    cin>>b;
        if(c=='-')c=='+';
        else c='-';
        cout<<a<<c<<b<<"i"<<endl;
    return 0;
}

  

原文地址:https://www.cnblogs.com/52dxer/p/12204490.html