叨叨游戏网
您的当前位置:首页noi寒假刷题之旅_1.4编程基础之逻辑表达式与条件分支(21题)

noi寒假刷题之旅_1.4编程基础之逻辑表达式与条件分支(21题)

来源:叨叨游戏网
noi寒假刷题之旅_1.4编程基础之逻辑表达式与条件分⽀(21题)

(21题)

01:判断数正负

#include#include

using namespace std;int main(){

int n; cin>>n; if(n>0) {

cout<<\"positive\"<else if(n<0) {

cout<<\"negative\"<cout<<\"zero\"; }}

02:输出绝对值

#include#include

using namespace std;int main(){

float n; cin>>n;

printf(\"%.2f\}

03:奇偶数判断

#include#include

using namespace std;int main(){

int n; cin>>n; if(n%2) {

cout<<\"odd\"; } else {

cout<<\"even\"; }}

04:奇偶ASCII值判断

#includeusing namespace std;int main(){

char n; n=getchar(); if((int)n%2) {

cout<<\"YES\"; } else {

cout<<\"NO\"; }}/*

WA了两发才反应过来cin不能读空格 */

05:整数⼤⼩⽐较

#includeusing namespace std;int main(){

int x,y;

cin>>x>>y; if(xcout<<\"<\"; }

else if(x>y) {

cout<<\">\"; } else {

cout<<\"=\"; }}

06:判断是否为两位数

#includeusing namespace std;int main(){

int a; cin>>a;

if(10<=a&&a<=99) {

cout<<1; } else {

cout<<0; }}

07:收集瓶盖赢⼤奖

#includeusing namespace std;int main(){

int a,b;

cin>>a>>b;

if(a>=10||b>=20) {

cout<<1; } else {

cout<<0; } }

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

#includeusing namespace std;int main(){

double a; cin>>a;

if(!((long long)a%15)) {

cout<<\"YES\"; } else {

cout<<\"NO\"; } }

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

#includeusing namespace std;int main(){

double a; cin>>a;

if(!((long long)a%105)) {

cout<<\"3 5 7\"; }

else if(!((long long)a%15)) {

cout<<\"3 5\"; }

else if(!((long long)a%21)) {

cout<<\"3 7\"; }

else if(!((long long)a%35)) {

cout<<\"5 7\";

}else if(!((long long)a%3)) {

cout<<3; }

else if(!((long long)a%5)) {

cout<<5; }

else if(!((long long)a%7)) {

cout<<7; } else

{

cout<<\"n\"; }}

10:有⼀门课不及格的学⽣

#includeusing namespace std;int main(){

int a,b;

cin>>a>>b;

if((a<60||b<60)&&!(a<60&&b<60)) {

cout<<1; } else {

cout<<0; }}

11:晶晶赴约会

#includeusing namespace std;int main(){

int a; cin>>a;

if(a==1||a==3||a==5) {

cout<<\"NO\"; } else {

cout<<\"YES\"; }}

12:骑车与⾛路

#includeusing namespace std;int main(){

double a,b,c; cin>>a;

b=27+23+a/3.0; c=a/1.2; if(bcout<<\"Bike\"; }

else if(b>c) {

cout<<\"Walk\"; } else {

cout<<\"All\"; }

return 0;}

13:分段函数

#include#include

using namespace std;int main(){

double x,y; cin>>x;

if(0y=-1*x+2.5; }

else if(5y=2-1.5*(x-3)*(x-3); } else {

y=x/2-1.5; }

printf(\"%.3f\ return 0;}

14:计算邮资

#include#include

using namespace std;int main(){

int x,s=8; char y; cin>>x>>y; if(x>1000) {

s+=(x-1000)/500*4; if((x-1000)%500) {

s+=4; } }

if(y=='y')s+=5; cout<15:最⼤数输出

#include#include

using namespace std;int main(){

int a,b,c;

cin>>a>>b>>c; if(a>=b&&a>=c) {

cout<if(b>=a&&b>=c) {

cout<if(c>=b&&c>=a) {

cout<return 0;}

16:三⾓形判断

#include#include

using namespace std;int main(){

int a,b,c;

cin>>a>>b>>c; int max=a>b? a:b; max=max>c ? max:c; int min=aif(max==a&&min==c||max==c&&min==a)mid=b; if(max==b&&min==a||max==a&&min==b)mid=c; if(max==b&&min==c||max==c&&min==b)mid=a; if(min+mid>max&&max-mincout<<\"yes\"; } else {

cout<<\"no\"; }

return 0;}

17:判断闰年

#include#include

using namespace std;int main(){

int a; cin>>a;

if((a%4==0)&&(a%100)) {

cout<<\"Y\"; }

else if(a%400==0) {

cout<<\"Y\"; } else {

cout<<\"N\"; }

return 0;}

/*数据范围没到3200,所以不⽤考虑这个*/

18:点和正⽅形的关系

#include

#include

using namespace std;int main(){

double x,y; cin>>x>>y;

if(fabs(x)>1||fabs(y)>1) {

cout<<\"no\"; } else {

cout<<\"yes\"; }

return 0;}

19:简单计算器

#include#include

using namespace std;int main(){

int x,y,z; char opt;

cin>>x>>y>>opt; switch(opt) {

case '+': {

cout<case '-': {

cout<case '*': {

cout<if(y==0) {

cout<<\"Divided by zero!\"; } else {

cout<break; }

default:

cout<<\"Invalid operator!\"; }

return 0;}

20:求⼀元⼆次⽅程的根

#include#include

using namespace std;void function(double &a){

if(fabs(a)<1e-4)a=0;}

int main(){

double a,b,c; cin>>a>>b>>c;

double temp=b*b-4*a*c; if(temp<0) {

double sb = -b / (2*a), xb = sqrt(4*a*c-b*b) / (2*a); function(sb); function(xb); if(xb>0) {

printf(\"x1=%.5lf+%.5lfi;x2=%.5lf-%.5lfi\ } else {

printf(\"x1=%.5lf+%.5lfi;x2=%.5lf-%.5lfi\ } }

else if(temp>0)

{

double x1 = (-b + sqrt(b*b-4*a*c))/(2*a), x2 = (-b-sqrt(b*b-4*a*c))/(2*a); double max=x1>x2 ?x1:x2; double min=x1printf(\"x1=%.5lf;x2=%.5lf\ } else {

double x1 = (-b + sqrt(b*b-4*a*c))/(2*a), x2 = (-b-sqrt(b*b-4*a*c))/(2*a); function(x1);

printf(\"x1=x2=%.5lf\ }

return 0;}

21:苹果和⾍⼦2

#includeusing namespace std;int main(){

long long n,x,y,z; cin>>n>>x>>y; if(x) {

if(y%x) {

z=n-1-y/x; } else {

z=n-y/x; } } else { z=0; }

if(z<0)z=0; cout<

因篇幅问题不能全部显示,请点此查看更多更全内容