【NOI OpenJudge】【1.4】编程基础之逻辑表达式与条件分支

在这里插入图片描述

01:判断数正负

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
    int n;  cin>>n;
    if(n > 0){
        printf("positive\n");
    }else if(n == 0){
        printf("zero\n");
    }else{
        printf("negative\n");
    }
    return 0;
}

02:输出绝对值

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
    float x;
    cin>>x;
    if(x>=0)printf("%.2f",x);
    else printf("%.2f",-x);
    return 0;
}

03:奇偶数判断

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

04:奇偶ASCII值判断

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
    char c;
    scanf("%c",&c);
    if((int)c%2==1)cout<<"YES";
    else cout<<"NO";
    return 0;
}

05:整数大小比较

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

06:判断是否为两位数

#include<iostream>
using namespace std;
int main(){
    int n; 
    cin>>n;
    if(n >= 10 && n <= 99){
        cout<<1;
    }else {
        cout<<0<<'\n';
    }
    return 0;
}

07:收集瓶盖赢大奖

#include<iostream>
using namespace std;
int main(){
    int a, b;
    cin>>a>>b;
    if(a >= 10 || b >= 20){
        cout<<1<<'\n';
    }else cout<<0;
    return 0;
}

08:判断一个数能否同时被3和5整除

#include<iostream>
using namespace std;
int main(){
    int n;  cin>>n;
    if(n%3==0 && n%5==0)cout<<"YES";
    else cout<<"NO";
    return 0;
}

09:判断能否被3,5,7整除

#include<iostream>
using namespace std;
int main(){
    int n;  cin>>n;
    int ok = 0;
    if(n%3==0){
        cout<<3<<' '; ok = 1;
    }
    if(n%5==0){
        cout<<5<<' '; ok = 1;
    }
    if(n%7==0){
        cout<<7<<' '; ok = 1;
    }
    if(!ok)cout<<"n";
    return 0;
}

10:有一门课不及格的学生

#include<iostream>
using namespace std;
int main(){
    int a, b;
    cin>>a>>b;
    if(a<60 && b>=60 || b<60&&a>=60){
        cout<<1;
    }else cout<<0;
    return 0;
}

11:晶晶赴约会

#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    if(n==1 || n==3 || n==5){
        cout<<"NO";
    }else cout<<"YES";
    return 0;
}

12:骑车与走路

#include<iostream>
using namespace std;
int main(){
    int d;  cin>>d;
    int bt = d/3+27+23;
    int wt = d/1.2;
    if(bt < wt){cout<<"Bike";return 0;}
    if(bt > wt){cout<<"Walk";return 0;}
    cout<<"All";
    return 0;
}

13:分段函数

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
    float x, y;  cin>>x;
    if(x < 5)y = -x+2.5;
    else if(x < 10)y = 2-1.5*(x-3)*(x-3);
    else if(x < 20)y = y=x/2-1.5;
    printf("%.3f",y);
    return 0;
}

14:计算邮资

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
    int w; char s;
    cin>>w>>s;
    int ans = 8;
    if(s=='y')ans += 5;
    if(w<=1000){
        cout<<ans<<'\n';
        return 0;
    }else{
        w -= 1000;
        ans += ((w-1)/500+1)*4;
        cout<<ans;
    }
    return 0;
}

15:最大数输出

#include<iostream>
#include<cmath>
using namespace std;
int main(){
    int a, b, c;
    cin>>a>>b>>c;
    cout<<(max(a,max(b,c)))<<'\n';
    return 0;
}

16:三角形判断

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a[3];
    cin>>a[0]>>a[1]>>a[2];
    sort(a,a+3);
    if(a[2]<a[1]+a[0]&&a[0]>a[2]-a[1]){
        cout<<"yes";
    }
    else cout<<"no";
    return 0;
}

17:判断闰年

#include<iostream>
#include<cmath>
using namespace std;
int main(){
    int y;  cin>>y;
    if(y%4==0&&y%100!=0 || y%100==0&&y%400==0)cout<<"Y";
    else cout<<"N";
    return 0;
}

18:点和正方形的关系

#include<iostream>
#include<cmath>
using namespace std;
int main(){
    int x, y;
    cin>>x>>y;
    if(x<=1&&x>=-1&&y<=1&&y>=-1)cout<<"yes";
    else cout<<"no";
    return 0;
}

19:简单计算器

#include<iostream>
#include<cmath>
using namespace std;
int main(){
    int a, b; char op;
    cin>>a>>b>>op;
    if(op == '+')cout<<(a+b);
    else if(op=='-')cout<<(a-b);
    else if(op=='*')cout<<(a*b);
    else if(op=='/'){
        if(b==0)cout<<"Divided by zero!";
        else cout<<(a/b);
    }else cout<<"Invalid operator!";
    return 0;
}

20:求一元二次方程的根

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int main(){
    float a, b, c, x;
    cin>>a>>b>>c;
    if(b*b==4*a*c){
        printf("x1=x2=%.5f",(-b+sqrt(b*b-4*a*c))/(2*a));
    }else if(b*b>4*a*c){
        printf("x1=%.5f;x2=%.5f",(-b+sqrt(b*b-4*a*c))/(2*a), (-b-sqrt(b*b-4*a*c))/(2*a));
    }else{
        x = (-b/(2*a));
        if(x==-0.00000)x=0;
        printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi",x,(sqrt(4*a*c-b*b)/(2*a)),x,(sqrt(4*a*c-b*b)/(2*a)));
    }
    return 0;
}

21:苹果和虫子2

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int main(){
    int n, x, y;
    cin>>n>>x>>y;
    if(y == 0){
        cout<<n<<'\n';
        return 0;
    }
    int ans = n-((y-1)/x+1);
    if(ans < 0)ans = 0;
    cout<< ans <<'\n';
    return 0;
}
posted @ 2018-12-18 12:27  gwj1139177410  阅读(...)  评论(...编辑  收藏
选择