洛谷分支结构题单前一半 深基例2到例9 P5710 数的性质 P5711 闰年判断 P5712 Apples P5713 洛谷团队系统 P5714 肥胖问题 P5715 三位数排序 P5716月份天数

继续活动生疏的小手。
比起刚开始写的代码,更加的简洁生动。

P5710 数的性质
熟练使用逻辑运算符,正确辨别逻辑关系。

#include<bits/stdc++.h>
using namespace std;
bool fun1(int x)
{
	if(x%2==0)
		return 1;
	return 0;
}
bool fun2(int x)
{
	if(x>4&&x<=12)
		return 1;
	return 0;
}
int main()
{
	int x;
	cin >> x;
	if(fun1(x)&&fun2(x))
	cout << 1 << " ";
	else
	cout << 0 << " "; 
	if(fun1(x)||fun2(x))
	cout << 1 << " ";
	else
	cout << 0 << " "; 
	if((fun1(x)&&!fun2(x))||(!fun1(x)&&fun2(x)))
	cout << 1 << " ";
	else
	cout << 0 << " "; 
	if(!fun1(x)&&!fun2(x))
	cout << 1;
	else
	cout << 0; 
	return 0;
}

P5711 闰年判断
闰年的判断表达式。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x;
	cin >> x;
	if((x%4==0&&x%100!=0)||x%400==0)
	cout << 1;
	else
	cout << 0; 
	return 0;
}

P5712 Apples
if else 分支结构

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x;
	cin >> x;
	if(x<2)
	printf("Today, I ate %d apple.",x);
	else
	printf("Today, I ate %d apples.",x);
	return 0;
}

P5713 洛谷团队系统
if else 分支结构

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x;
	cin >> x;
	if(5*x>3*x+11)
	cout << "Luogu";
	else
	cout << "Local"; 
	return 0;
}

P5714 肥胖问题
简单计算加分支结构

#include<bits/stdc++.h>
using namespace std;
int main()
{
	double m,n;
	cin >> m >> n;
	double BMI = m/n/n;
	if(BMI<18.5)
	cout << "Underweight";
	else if(BMI>=24)
	{
		cout << BMI << endl;
		cout << "Overweight";
	}
	else
	cout << "Normal";
	return 0;
}

P5715 三位数排序
注意先排ab,再排ac,最后排bc,要有顺序排序。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c;
	cin >> a >> b >> c;
	if(a>b)
	{
		int temp = a;
		a = b;
		b = temp;
	}
	if(a>c)
	{
		int temp = a;
		a = c;
		c = temp;
	}
	if(b>c)
	{
		int temp = b;
		b = c;
		c = temp;
	}
	
	cout << a <<" "<< b << " " << c; 
	return 0;
}

P5716 月份天数
使用数组输出更加方便。

#include<bits/stdc++.h>
using namespace std;
int normal_year[13]={31,28,31,30,31,30,31,31,30,31,30,31}; 
int special_year[13]={31,29,31,30,31,30,31,31,30,31,30,31};
int main()
{
	int y;
	cin >> y;
	int m;
	if((y%4==0&&y%100!=0)||y%400==0)
	{
		cin >> m;
		cout << special_year[m-1];
	} 
	else
	{
		cin >> m;
		cout << normal_year[m-1];
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三元湖有大锦鲤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值