首页 » 入门组内容 » 正文

程序的控制结构

1039.判断数正负

#include<bits/stdc++.h>

using namespace std;  

int main(){
    int n;  
    cin >> n;  
    if(n > 0){
        cout << "positive";  
    }
    else if(n == 0){
        cout <<"zero";  
    }
    else cout <<"negative";  
    cout << endl;  
    return 0;  
} 

  1. 整数大小比较
#include<bits/stdc++.h>

using namespace std;  

int main(){
    int a, b;  
    cin >> a >> b;  
    if(a > b) cout << '>'<< endl;  
    else if(a == b) cout << '=' << endl;  
    else cout << '<' << endl;  
    return 0;  
} 
  1. 判断能否被3、5、7整除
#include<bits/stdc++.h>

using namespace std;  

int main(){
    int n;  
    cin >> n;  
    bool flag = false;  
    if( n % 3 == 0){
        cout << 3;  
        flag = true;  
    }
    if(n % 5 == 0){
        if(flag){
            cout << ' ';  
        }
        cout << 5;  
        flag = true;  
    }
    if(n % 7 == 0){
        if(flag) cout << ' ';  
        cout << 7 ;  
        flag = true;  
    }
    if(!flag) cout << 'n';  
    cout << endl;  
    return 0;  
} 

1049.晶晶赴约会

#include<bits/stdc++.h>

using namespace std;  

int main(){
    int n;  
    cin >> n;  
    switch(n){
        case 1 :
        case 3 :
        case 5 :
            cout << "NO"<<endl;  
            break;  //注意break 
        case 2 :
        case 4 :
        case 6 :
        case 7 :
            cout << "YES"<<endl;  //可以采用default来写
            break;   

    }
    return 0;  
} 

  1. 三角形判断
#include<bits/stdc++.h>

using namespace std;  

int main(){
    int a, b, c;  
    cin >> a >> b >> c;  
    if(a >= b && a >= c){
        if( b+c >a){
            cout << "yes"<< endl;  
        }
        else cout << "no" << endl;  
    }
    else if( b >= a && b >= c){
        if(a + c > b){
            cout << "yes"<<endl;  
        }
        else cout << "no"<<endl;  
    }
    else {
        if(a + b > c){
            cout << "yes"<<endl;  
        }
        else {
            cout << "no"<<endl;  
        }
    }
    return 0;  
} 

赞 (0)

发表评论