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

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

时间:2023-04-04 11:08:21浏览次数:40  
标签:1.4 std main NOI int namespace using include OpenJudge


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

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;
}


标签:1.4,std,main,NOI,int,namespace,using,include,OpenJudge
From: https://blog.51cto.com/gwj1314/6168189

相关文章

  • 【NOI OpenJudge】【1.2】编程基础之变量定义、赋值及转换
    01:整型数据类型存储空间大小#include<cstdio>intmain(){ inta;shortb; printf("%d%d",sizeof(a),sizeof(b)); return0;}02:浮点型数据类型存储空间大小#include<cstdio>intmain(){ floata;doubleb; printf("%d%d",sizeof(a),sizeof(b)); return......
  • 【初赛】NOIP2018程序模板
    这里没有代码,去相应的文章找。。。一、基础1、排序冒泡、选择、插入、快排、归并、堆、桶找k大数、排序+链表找最近值、2、高精度四则运算和高精四则运算和低精开根号3、模拟递推最大子段和矩阵找数4、二分5、贪心6、倍增二、动态规划最大字段和LIS字符串三、数学数论同余四、数据......
  • 【NOI OpenJudge1789】算24(搜索)
    problem给定4个数,加减乘除4种运算以及括号把这4个数连接起来得到一个表达式。问是否存在一种方式使得得到的表达式的结果等于24。solution正常的中缀表达式枚举和计算难度都约等于0,麻烦的是括号的枚举和处理。这里只要求满足的结果,所以换一种方式拿掉括号——打乱顺序即可(括号的用......
  • NOIST2023 + HEOI2023 游记
    好像是被打破防了。春季赛春季赛忘的差不多了,但是总而言之打假赛。day0去的是叫英庄李家的酒店,下午去看海了。手机在看到海并拍摄一张照片后残忍关机了。......
  • Day 21 21.4 数据库之Python操作MongoDB
    PyMongo在这里我们来看一下Python3下MongoDB的存储操作,在本节开始之前请确保你已经安装好了MongoDB并启动了其服务,另外安装好了Python的PyMongo库。安装:pipinstallpymongo添加文档importpymongoclient=pymongo.MongoClient(host='localhost',port=27017)"""......
  • NOI 1.8编程基础之多维数组
    02:同行列对角线的格子1.描述输入三个自然数N,i,j (1<=i<=N,1<=j<=N),输出在一个N*N格的棋盘中(行列均从1开始编号),与格子(i,j)同行、同列、同一对角线的所有格子的位置。如:n=4,i=2,j=3表示了棋盘中的第二行第三列的格子,如下图:第一列第二列第三列第四列     ......
  • HNOI2023 游记
    Day???去中山集训回来了。Day0没啥心情做题,上午随便写了点板子,然后扫描线写了一个小时调不出来,感觉不是很好。下午听了下动员,听完之后心态确实好些了,虽然很久没做什么题,但是在考场上写满暴力不挂分似乎并不是很难达到的目标。晚上到考场旁边订了个酒店,稍微思考了一下明天的......
  • LNOI2023游记
    前传:2023ST游记Day-n春测以倒数第三的成绩获得了省选体验卡Day12023.4.1一上来三道题前两道粗略看看不懂T3题面巨长不禁感叹这就是省选的压迫感吗(大概看了下三道题之后去做T1结合样例终于看懂了想了个每次跳最远点的贪心证完正确性大概半小时敲完并过了样124跑3的时......
  • NOI2023 联合省选游记
    Day-4~Day-1把【全真模拟】都给摆了,随机看一些知识点,决定到时候慢慢敲暴力。Day0颓了一天,随机睡觉和颓废。Day1放个CSPT1难度的T1,笑死。T2来推一推!30min后感觉自己会了!T3,不是模拟费用流板子吗?也不是很难写!感觉赢麻了。自信即癫疯!草,T1为啥要线段树优化建图啊,开写。......
  • P4688 [Ynoi2016] 掉进兔子洞
    RE了大约12次以后,SoN3ri告诉我是bitset开小了。那你为什么全RE了啊(?题意是给你一个长度为\(n\)的序列,一共\(m\)次询问,每次询问包含三个区间,求三个区间内相同的数去掉后剩下的数的个数。做完了小清新人渣的本愿,看啥都是bitset+莫队,这题我也是一开始打了一个莫队+bitset,但是......