python && C++ NOI编程题解1.4

01:判断数正负

python编程

n = int(input())
if n > 0:
    print("positive")
elif n == 0:
    print("zero")
else:
    print("negative")

C++编程

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

 

02:输出绝对值

python编程

n = float(input())
if n < 0:
    n = -n
print('%.2f' % n)

C++编程

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

 

03:奇偶数判断

python编程

n = int(input())
if n % 2:
    print("odd")
else:
    print("even")

C++编程

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

 

04:奇偶ASCII码值判断

python编程

ch = input()
n = ord(ch)
if n % 2:
    print("YES")
else:
    print("NO")

runtime error

C++编程

#include<iostream>
using namespace std;
int main()
{
	char a;
	cin.get(a);
	if(a%2==0)
		printf("%s","NO");
	if(a%2==1)
		printf("%s","YES");
}

 

05:整数大小比较

python编程

x, y = map(int, input().split())
if x > y:
    print(">")
elif x == y:
    print("=")
else:
    print("<")

C++编程

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

 

06:判断是否为两位数

python编程

n = int(input())
if n < 100 and n > 9:
    print("1")
else:
    print("0")

C++编程

#include<iostream>
using namespace std;
int main()
{
	int x;
	cin>>x;
	if(x/10>0 && x/100<1)
		cout<<"1";
	else
		cout<<"0";
}

 

07:收集瓶盖赢大奖

python编程

x, y = map(int, input().split())
if x >= 10 or y >= 20:
    print("1")
else:
    print("0")

C++编程

#include<iostream>
using namespace std;
int main()
{
	int x,y;
	cin>>x>>y;
	if(x>9 || y>19)
		cout<<"1";
	else
		cout<<"0";
}

 

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

python编程

n = int(input())
if n % 3 == 0 and n % 5 == 0:
    print("YES")
else:
    print("NO")

C++编程

#include<iostream>
using namespace std;
int main()
{
	int x;
	cin>>x;
	if(x==(x/3)*3 && x==(x/5)*5)
		cout<<"YES";
	else
		cout<<"NO";
}

 

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

python编程

n = int(input())
if n % 3 == 0 and n % 5 == 0 and n % 7 == 0:
    print(3, 5, 7, sep=' ')
elif n % 3 == 0 and n % 5 == 0:
    print(3, 5, sep=' ')
elif n % 3 == 0 and n % 7 == 0:
    print(3, 7,sep=' ')
elif n % 5 == 0 and n % 7 == 0:
    print(5, 7,sep=' ')
elif n % 3 == 0:
    print(3)
elif n % 5 == 0:
    print(5)
elif n % 7 == 0:
    print(7)
else:
    print('n')

C++编程

#include<iostream>
using namespace std;
int main()
{
	int a;
	cin>>a;
	if(a==(a/3)*3 && a==(a/5)*5 && a==(a/7)*7)
		cout<<"3"<<" "<<"5"<<" "<<"7";
	else if(a==(a/3)*3 && a==(a/5)*5)
		cout<<"3"<<" "<<"5";
	else if(a==(a/3)*3 && a==(a/7)*7)
		cout<<"3"<<" "<<"7";
	else if(a==(a/5)*5 && a==(a/7)*7)
		cout<<"5"<<" "<<"7";
	else if(a==(a/3)*3)
		cout<<"3";
	else if(a==(a/5)*5)
		cout<<"5";
	else if(a==(a/7)*7)
		cout<<"7";
	else
		cout<<"n";
}

 

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

python编程

x, y = map(int, input().split())
if (x < 60 and y > 59) or (x > 59 and y < 60):
    print("1")
else:
    print("0")

C++编程

#include<iostream>
using namespace std;
int main()
{
	int x,y;
	cin>>x>>y;
	if((x>59 && y<60)||(x<60 && y>59))
		cout<<"1";
	else
		cout<<"0";
}

 

11:晶晶赴约会

python编程

n = int(input())
if n == 1 or n == 3 or n == 5:
    print("NO")
else:
    print("YES")

C++编程

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

 

12:骑车与走路

python编程

n = int(input())
walk = n/1.2
bike = n/3 + 50
if walk > bike:
    print("Bike")
elif walk == bike:
    print("All")
else:
    print("Walk")

C++编程

#include<iostream>
using namespace std;
int main()
{
	int x;
	cin>>x;
	double b=50+x/3.0,w=x/1.2;
	if(b>w)
		cout<<"Walk"<<endl;
	else if(w>b)
		cout<<"Bike"<<endl;
	else
		cout<<"All"<<endl;
}

 

13:分段函数

python编程

n = float(input())
if 0 <= n < 5:
    print('%.3f' % (2.5 - n))
elif n < 10:
    print('%.3f' % (2 - 1.5*(n - 3)*(n - 3)))
elif n < 20:
    print('%.3f' % (n/2 - 1.5))

C++编程

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

 

14:计算邮资

python编程

import math
x, y = map(str, input().split())
if y == "y":
    if int(x) <= 1000:
        print(13)
    else:
        print(8 + math.ceil((int(x)-1000)/500)*4 + 5)
if y == "n":
    if int(x) <= 1000:
        print(8)
    else:
        print(8 + math.ceil((int(x)-1000)/500)*4)

C++编程

#include<iostream>
using namespace std;
int main()
{
	int x,y;
	char ch;
	cin>>x>>ch;
	if(x<=1000)
		y=8;
	else
	{
		if(x==(x/500)*500)
			y=((x-1000)/500)*4+8;
		else
			y=(((x-1000)/500)+1)*4+8;
	}
	if(ch=='y')
		cout<<y+5;
	if(ch=='n')
		cout<<y;
}

 

15:最大数输出

python编程

x, y, z = map(int, input().split())
max = x
if y > max:
    max = y
if z > max:
    max = z
print(max)

C++编程

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

 

16:三角形判断

python编程

x, y, z = map(int, input().split())
if x+y > z and x+z > y and y+z > x:
    print("yes")
else:
    print("no")

C++编程

#include<iostream>
using namespace std;
int main()
{
	unsigned int a,b,c;
	cin>>a>>b>>c;
	if(a+b>c && a+c>b && b+c>a)
		printf("yes");
	else
		printf("no");
}

 

17:判断闰年

python编程

n = int(input())
if n % 4 == 0:
    if (n % 100 == 0 and n % 400 != 0) or n % 3200 == 0:
        print("N")
    else:
        print("Y")
else:
    print("N")

C++编程

 

#include<iostream>
using namespace std;
int main()
{
	int a;
	cin>>a;
	if(a==(a/4)*4)
	{
		if((a!=(a/400)*400 && a==(a/100)*100) || (a==(a/3200)*3200))
			cout<<"N";
		else
			cout<<"Y";
	}
	else
		cout<<"N";
}

 

18:点和正方形的关系

python编程

x, y = map(int, input().split())
if -1 <= x <= 1 and -1 <= y <= 1:
    print("yes")
else:
    print("no")

C++编程

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

 

19:简单计算器

python编程

import math
x, y, op = map(str, input().split())
if op == "+":
    print(int(x)+int(y))
elif op == "-":
    print(int(x)-int(y))
elif op == "*":
    print(int(x)*int(y))
elif op == "/":
    if y == 0:
        print("Divided by zero!")
    else:
        result = int(x)/int(y)
        if result >= 0:
            result = math.floor(result)
        else:
            result = math.ceil(result)
        print(result)
else:
    print("Invalid operator!")

runtime error

C++编程

#include<iostream>
using namespace std;
int main()
{
	int x,y;
	char c;
	cin>>x>>y>>c;
	switch(c)
	{
		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!";
	}
}

 

20:求一元二次方程的根

python编程

import math
a, b, c = map(float, input().split())
dt = b*b - 4*a*c
if dt == 0:
    x = -b / (2 * a)
    print('x1=x2=%.5f' % x)
elif dt > 0:
    x1 = (-b + math.sqrt(dt))/(2*a)
    x2 = (-b - math.sqrt(dt))/(2*a)
    print('x1=%.5f;x2=%.5f' % (x1, x2))
else:
    real = (0 - b) / (2 * a);
    image = math.sqrt(0 - dt) / (2 * a);
    print('x1=%.5f+%.5fi;x2=%.5f-%.5fi' % (real, image, real, image))

C++编程

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

 

21:苹果和虫子2

python编程

import math
n, x, y = map(int, input().split())
if y/x > n:
    print("0")
else:
    residue = n - math.ceil(y/x)
    print(residue)

C++编程

#include<iostream>
using namespace std;
int main()
{
	unsigned int n,x,y;
	cin>>n>>x>>y;
	if(n*x<=y)
		cout<<"0";
	else
	{
		if(y%x==0)
			cout<<n-y/x;
		else
			cout<<n-y/x-1;
	}
}
  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值