辽宁师范大学 • 张大为@https://daweizh.github.io/noip/

1.4 编程基础之逻辑表达式与条件分支(21)

  1. 判断数正负,http://noi.openjudge.cn/ch0104/01/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n;
        cin >> n;
        string s = "positive";
        if (n ==0)	
            s = "zero";
        else if (n<0)
            s = "negative";
        cout << s << endl;
              
        return 0;
    }
    
  2. 输出绝对值,http://noi.openjudge.cn/ch0104/02/
    #include <iostream>
    using namespace std;
    
    int main(){
        double n;
        cin >> n;
        if (n<0)
            n = -n;
        printf("%.2f",n);
        
        return 0;
    }
    
  3. 奇偶数判断,http://noi.openjudge.cn/ch0104/03/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n;
        cin >> n;
        string s = "even";
        if (n%2==1)
            s = "odd";
        cout << s << endl;
        
        return 0;
    }
    
  4. 奇偶ASCII值判断,http://noi.openjudge.cn/ch0104/04/
    #include <iostream>
    using namespace std;
    
    int main(){
        char c=getchar();
        string s = "NO";
        if (c%2==1)
            s = "YES";
        cout << s << endl;
        
        return 0;
    }
    
  5. 整数大小比较,http://noi.openjudge.cn/ch0104/05/
    #include <iostream>
    using namespace std;
    
    int main(){
        int x,y;
    
        cin >> x >> y;
        string s = ">";
        if (x==y)
            s = "=";
        else if (x <y)
            s = "<"; 
        cout << s << endl;
        
        return 0;
    }
    
  6. 判断是否为两位数,http://noi.openjudge.cn/ch0104/06/
    #include <iostream>
    using namespace std;
    
    int main(){
        int i;
    
        cin >> i;
        if (i>=10 && i<=99)
            i = 1;
        else
            i = 0;		
        cout << i << endl;
        
        return 0;
    }
    
  7. 收集瓶盖赢大奖,http://noi.openjudge.cn/ch0104/07/
    #include <iostream>
    using namespace std;
    
    int main(){
        int x,y,z;
    
        cin >> x >> y;
        if (x>=10 || y >= 20)
            z = 1;
        else
            z = 0;		
        cout << z << endl;
        
        return 0;
    }
    
  8. 判断一个数能否同时被3和5整除,http://noi.openjudge.cn/ch0104/08/
    #include <iostream>
    using namespace std;
    
    int main(){
      int n;
    
      cin >> n;
      string s = "NO";
      if (n % 3==0 && n % 5==0)
          s = "YES";
      cout << s << endl;
      
      return 0;
    }
    
  9. 判断能否被3,5,7整除,http://noi.openjudge.cn/ch0104/09/
    #include <iostream>
    using namespace std;
    
    int main(){
        int a; 
        
        cin >> a ;
        if(a%3==0 && a%5==0 && a%7==0){
            cout << 3 << " " << 5 << " " << 7 << endl;
        }else if(a%3==0 && a%5==0){
            cout << 3 << " " << 5 << endl;
        }else if(a%5==0 && a%7==0){
            cout << 5 << " " << 7 << endl;
        }else if(a%3==0 && a%7==0){
            cout << 3 << " " << 7 << endl;
        }else if(a%3==0){
            cout << 3 << endl;
        }else if(a%5==0){
            cout << 5 << endl;
        }else if(a%7==0){
            cout << 7 << endl;
        }else{
            cout << "n" << endl;
        }
        
        return 0;
    } 
    
  10. 有一门课不及格的学生,http://noi.openjudge.cn/ch0104/10/
    #include <iostream>
    using namespace std;
    
    int main(){
        int c1,c2;
    
        cin >> c1 >> c2;
        string s = "0";
        if ((c1<60 && c2>=60 )|| (c1>=60 && c2 <60))
            s = "1";
        cout << s << endl;
        
        return 0;
    }
    
  11. 晶晶赴约会,http://noi.openjudge.cn/ch0104/11/
    #include <iostream>
    using namespace std;
    
    int main(){
        int d;
    
        cin >> d;
        string s = "YES";
        if (d==1 || d==3 || d == 5 )
            s = "NO";
        cout << s << endl;
        
        return 0;
    }
    
  12. 骑车与走路,http://noi.openjudge.cn/ch0104/12/
    #include <iostream>
    using namespace std;
    
    int main(){
        double m;
    
        cin >> m;
        string s = "All";
        double bikeTime = m / 3.0 + 50;
        double walkTime = m / 1.2;
        if ( bikeTime < walkTime )
            s = "Bike";
        else if (bikeTime > walkTime)
            s = "Walk";
        cout << s << endl;
        
        return 0;
    }
    
  13. 分段函数,http://noi.openjudge.cn/ch0104/13/
    #include <iostream>
    using namespace std;
    
    int main(){
        double n,f=0;
    
        cin >> n;
        if ( n>=0 && n < 5 )
            f = -n + 2.5;
        else if (n>=5 && n<10)
            f = 2 -1.5*(n-3)*(n-3);
        else if (n>=10 && n<20)
            f = n / 2 - 1.5;
        printf("%.3f",f);
        
        return 0;
    }
    
  14. 计算邮资,http://noi.openjudge.cn/ch0104/14/
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
        double w;
        char c;
        int f;
    
        cin >> w >> c;
        if (w <= 1000)
            f = 8;
        else
            f = ceil((w-1000)/500)*4 + 8;
        if (c=='y')
            f = f + 5;
        printf("%d",f);
        
        return 0;
    }
    
  15. 最大数输出,http://noi.openjudge.cn/ch0104/15/
    #include <iostream>
    using namespace std;
    
    int main(){
        int a,b,c;
    
        cin >> a >> b >> c;
        if(a>b && b>c){
            cout << a << endl;
        }else if(b>c){
            cout << b << endl;
        }else{
            cout << c << endl; 
        }
        
        return 0;
    }
    
  16. 三角形判断,http://noi.openjudge.cn/ch0104/16/
    #include <iostream>
    using namespace std;
    
    int main(){
        int x,y,z;
        
        cin >> x >> y >>z;
        string s = "yes";
        if (x + y <= z)
            s = "no";
        else if(x + z <= y)
            s = "no";
        else if(y + z <= x)				
            s = "no";
        cout << s << endl;
        
        return 0;
    }
    
  17. 判断闰年,http://noi.openjudge.cn/ch0104/17/
    #include <iostream>
    using namespace std;
    
    int main(){
        int y;
    
        cin >> y;
        string s = "N";
        if (y % 4 ==0 && !(y % 100==0 && y % 400 !=0) && !(y%3200==0))
            s = "Y";
        cout << s << endl;
        
        return 0;
    }
    
  18. 点和正方形的关系,http://noi.openjudge.cn/ch0104/18/
    #include <iostream>
    using namespace std;
    
    int main(){
        int x,y;
    
        cin >>x >> y;
        string s = "no";
        if (x<=1 && x>=-1 && y <=1 && y>=-1)
            s = "yes";
        cout << s << endl;
        
        return 0;
    }
    
  19. 简单计算器,http://noi.openjudge.cn/ch0104/19/
    #include <iostream>
    using namespace std;
    
    int main(){
        int x,y;
        char c;
        int result = 0;
        string s = "";
    
        cin >>x >> y >> c;
        switch(c){
            case '+':
                result = x + y;
                break;
            case '-':
                result = x - y;
                break;
            case '*':
                result = x * y;
                break;
            case '/':
                if (y==0)
                    s = "Divided by zero!";
                else
                    result = x / y;
                break;
            default:
                s = "Invalid operator!";		
        }	
        if (s=="")
            cout << result << endl;
        else
            cout << s << endl;
        
        return 0;
    }
    
  20. 求一元二次方程的根,http://noi.openjudge.cn/ch0104/20/
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
        double a,b,c;
    
        cin >> a >> b >> c;
        double b2 = b * b;
        double dt = 4 * a * c;
        double a2 = 2 * a;
        if (b2 < dt){
            double r = -b / a2;
            if (b==0){
                r = 0;
            }
            double v = sqrt(dt-b2)/a2;
            printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi",r,v,r,v);
        }else{
            double x1 = (-b + sqrt(b2-dt))/a2;
            double x2 = (-b - sqrt(b2-dt))/a2;
            if (b2>dt){
                printf("x1=%.5f;x2=%.5f",x1,x2);
            }else{
                printf("x1=x2=%.5f",x1);
            }
        }
    
        return 0;		
    }
    
  21. 苹果和虫子2,http://noi.openjudge.cn/ch0104/21/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n;  //苹果数量 
        int x;  //虫子吃一个苹果花的时间 
        int y; 	//虫子吃了多少时间的苹果
        
        cin >> n>> x >>y;  //分别输入苹果个数,虫子吃一个苹果的时间,虫子吃了多长时间的苹果 
        if(n>0){ 	//如果箱子里有苹果 
            if(x>0){
                if(y>0){
                    int m = y/x; //计算吃了几个整苹果
                    if(y%x!=0)
                        m = m + 1;
                    if(n-m>0)
                        cout << n-m << endl;
                    else
                        cout << "0" << endl;
                }else{
                  cout << n << endl;
                }
            }else{
              cout << n << endl;
            }
        }else{ 		//否则,箱子里没有苹果 
            cout << "0" << endl;
        }
        
        return 0;
    }
    
    

参考网址

  1. http://noi.openjudge.cn