四、分支语句的简单应用-习题

三十七、判断奇偶性

image

#include <iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    if(n%2==0){
        cout<<"even";
    }
    else{
        cout<<"odd";
    }
    return 0;
}

三十八、购买电池

image

#include <iostream>
using namespace std;
int main(){
    int sl;
    cin>>sl;
    if(sl<=10){
        cout<<sl*10;
    }
    else{
        cout<<sl*10*0.8;
    }
    return 0;
}

三十九、Maoge的游戏

image

#include <iostream>
using namespace std;
int main(){
    int x,y;
    cin>>x>>y;
    if(x!=y){
        cout<<"maoge";
    }
    else{
        cout<<"maoge233";
    }
    return 0;
}

四十、逻辑运算运用1

image

#include <iostream>
using namespace std;
int main(){
    int a,b,c;
    cin>>a>>b>>c;
    if(a>b){
        if(b>c){
            cout<<"YES";
        }
        else{
            cout<<"NO";
        }
    }
    else{
        cout<<"NO";
    }
    return 0;
}

四十一、美丽数

image

#include <iostream>
using namespace std;
int main(){
    int a;
    cin>>a;
    if(a>50){
        if(a%2==0){
            cout<<"yes";
        }
        else{
            cout<<"no";
        }
    }
    else{
        cout<<"no";
    }
    return 0;
}

四十二、求绝对值

image

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    int a;
    cin>>a;
    cout<<abs(a);
    return 0;
}

四十三、买东西

image

#include <iostream>
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    if(a>b){
        cout<<"change "<<a-b<<" dollar.";
    }
    else{
        cout<<"owe "<<abs(a-b)<<" dollar.";
    }
    return 0;
}

四十四、某年某月有几天

image

#include <iostream>
using namespace std;
int main(){
    int year,mon;
    cin>>year>>mon;
    if(mon!=2){
        if(mon==1){
            cout<<31;
        }
        if(mon==3){
            cout<<31;
        }
        if(mon==4){
            cout<<30;
        }
        if(mon==5){
            cout<<31;
        }
        if(mon==6){
            cout<<30;
        }
        if(mon==7){
            cout<<31;
        }
        if(mon==8){
            cout<<31;
        }
        if(mon==9){
            cout<<30;
        }
        if(mon==10){
            cout<<31;
        }
        if(mon==11){
            cout<<30;
        }
        if(mon==12){
            cout<<31;
        }
    }
    else{
        if(year%4==0){
          if(year%100!=0){
            cout<<29;
          }
          else{
            cout<<28;
          }
        }
      else{
        cout<<28;
      }
    }
    return 0;
}

四十五、三数中最大和最小

image

#include <iostream>
#include <cmath>
using namespace std;
int main(){
  int a,b,c;
  cin>>a>>b>>c;
  cout<<"The maximum number is : "<<max(max(a,b),c)<<endl;
  cout<<"The minimum number is : "<<min(min(a,b),c);
  return 0;
}

四十六、分西瓜

image

#include <iostream>
using namespace std;
int main(){
  int w;
  cin>>w;
  if (w%2==0) {
    if(w>=4){
      cout<<"YES, you can divide the watermelon into two even parts.";
    }
    else{
      cout <<"NO, you can't divide the watermelon into two even parts.";
    }
  }
  else{
    cout <<"NO, you can't divide the watermelon into two even parts.";
  }
  return 0;
}

四十七、买房子

image

#include <iostream>
using namespace std;
int main(){
  int n,k,min_good,max_good;
  cin>>n>>k;
  if(k==0){
    min_good=0;
  }else{
  if(k==n){
    min_good=0;
  }else{
    min_good=1;
  }
  }
  if(k==0){
    max_good=0;
  }else{
    if(2*k<=n-k){
      max_good=2*k;
    }else{
      max_good=n-k;
    }
  }
  cout<<min_good<<" "<<max_good<<endl;
  return 0;
}

四十八、约会

image


四十九、可能的位置

image


五十、小帅打电话

image


五十一、判断直角三角形

image

#include <iostream>
#include <cmath>
using namespace std;
int main(){
  int a,b,c;
  cin>>a>>b>>c;
  if(c<a){
    swap(a,c);
  }
  if(c<b){
    swap(b,c);
  }
  if((a*a)+(b*b)==(c*c)){
    cout<<"yes";
  }else{
    cout<<"no";
  }
  return 0;
}
posted @ 2025-09-05 18:19  jch123456  阅读(17)  评论(0)    收藏  举报