【NOI OpenJudge】【1.4】编程基础之逻辑表达式与条件分支
【摘要】
01:判断数正负
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int n...
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
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;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
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
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
文章来源: gwj1314.blog.csdn.net,作者:小哈里,版权归原作者所有,如需转载,请联系作者。
原文链接:gwj1314.blog.csdn.net/article/details/85063098
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)