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

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

01:判断数正负

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int n;
	cin>>n;
	if(n>0)
	{
		cout<<"positive"<<endl;
	}
	else if(n<0)
	{
		cout<<"negative"<<endl;
	}
	else
	{
		cout<<"zero";
	}
}

02:输出绝对值

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	float n;
	cin>>n;
    printf("%.2f",fabs(n));
}

03:奇偶数判断

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n;
	cin>>n;
	if(n%2)
	{
		cout<<"odd";
	}
	else
	{
		cout<<"even";
	}
}

04:奇偶ASCII值判断

#include<iostream>
using namespace std;
int main()
{
    char n;
	n=getchar();
	if((int)n%2)
	{
		cout<<"YES";
	}
	else
	{
		cout<<"NO";
	}
}
/*
WA了两发才反应过来cin不能读空格 
*/

05:整数大小比较

#include<iostream>
using namespace std;
int main()
{
   int x,y;
   cin>>x>>y;
   if(x<y)
   {
   	cout<<"<";
   }
   else if(x>y)
   {
   	cout<<">";
   }
   else
   {
   	cout<<"=";
   }
}

06:判断是否为两位数

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

07:收集瓶盖赢大奖

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

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

#include<iostream>
using namespace std;
int main()
{
	double a;
	cin>>a;
	if(!((long long)a%15))
	{
		cout<<"YES";
	}
	else
	{
		cout<<"NO";
	}
	 
}

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

#include<iostream>
using 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:有一门课不及格的学生

#include<iostream>
using 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:晶晶赴约会

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

12:骑车与走路

#include<iostream>
using namespace std;
int main()
{
	double a,b,c;
	cin>>a;
	b=27+23+a/3.0;
	c=a/1.2;
	if(b<c)
	{
		cout<<"Bike"; 
	}
	else if(b>c)
	{
		cout<<"Walk";
	}
	else
	{
		cout<<"All";
	}
	return 0;
}

13:分段函数

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	double x,y;
	cin>>x;
	if(0<x&&x<5||x<1e-14)
	{
		y=-1*x+2.5;
	}
	else if(5<x&&x<10||fabs(x-5)<1e-14) 
	{
		y=2-1.5*(x-3)*(x-3);
	}
	else
	{
		y=x/2-1.5;
	}
	printf("%.3f",y);
	return 0;
}

14:计算邮资

#include<iostream>
#include<cmath>
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<<s;
	return 0;
}

15:最大数输出

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

16:三角形判断

#include<iostream>
#include<cmath>
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=a<b?a:b;
	min=min<c? min:c;
	int mid;
	if(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-min<mid)
	{
		cout<<"yes";
	 } 
	 else
	 {
	 	cout<<"no";
	 }
	return 0;
}

17:判断闰年

#include<iostream>
#include<cmath>
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<iostream>
#include<cmath>
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<iostream>
#include<cmath>
using namespace std;
int main()
{
	int x,y,z;
	char opt;
	cin>>x>>y>>opt;
    switch(opt)
    {
    	case '+':
    		{
    			cout<<x+y;
    			break;
			}
    	
    	case '-':
    		{
    			cout<<x-y;
    			break;
			}
		case '*':
			{
				cout<<x*y;
    			break;
			}			
		case '/':
			{
				if(y==0)
				{
					cout<<"Divided by zero!";
				}
				else
				{
					cout<<x/y;
				}
	    		break;
			}
    	default:
    		cout<<"Invalid operator!";	
	}
	return 0;
}

20:求一元二次方程的根

#include<iostream>
#include<cmath>
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",sb,xb,sb,xb);
	   }
	   else
	   {
	   	  printf("x1=%.5lf+%.5lfi;x2=%.5lf-%.5lfi",sb,-xb,sb,-xb);
	   }
	   
	}
	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=x1<x2 ?x1:x2; 
		function(max);
	    function(min);
		printf("x1=%.5lf;x2=%.5lf",max,min);
	}
	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",x1);
	}
	return 0;
}

21:苹果和虫子2

#include<iostream>
using 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<<z;
	return 0;
}
posted @ 2020-01-27 13:25  东坡肉肉君  阅读(1013)  评论(0)    收藏  举报