【洛谷】入门2 分支结构

原题传送门

点我

P5710 【深基3.例2】数的性质

题目描述
一些数字可能拥有以下的性质:

性质 1:是偶数;
性质 2:大于 4 且不大于 12。
小A 喜欢这两个性质同时成立的数字;Uim 喜欢这至少符合其中一种性质的数字;八尾勇喜欢刚好有符合其中一个性质的数字;正妹喜欢不符合这两个性质的数字。

输入格式
输入一个数字 x(0≤x≤1000)

输出格式
输出这 4 个人是否喜欢这个数字,如果喜欢则输出1,否则输出0,用空格分隔。

输入输出样例
输入
12
输出
1 1 0 0

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

P5711 【深基3.例3】闰年判断

题目描述
输入一个年份(大于 1582 的整数 ),判断这一年是否是闰年,如果是输出 1,否则输出 0。

ps:
if后要有一整个括号。

#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 【深基3.例4】Apples

题目描述
八尾勇喜欢吃苹果。她今天吃掉了 x(0≤x≤100) 个苹果。英语课上学到了 apple 这个词语,想用它来造句。如果她吃了 1 个苹果,就输出 Today, I ate 1 apple.;如果她没有吃,那么就把 1 换成 0;如果她吃了不止一个苹果,别忘了 apple 这个单词后面要加上代表复数的 s。你能帮她完成这个句子吗?

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	if(n==1||n==0)  cout<<"Today, I ate "<<n<<" apple.";
	else  cout<<"Today, I ate "<<n<<" apples.";
	
	return 0;
}

P5713 【深基3.例5】洛谷团队系统

题目描述
在洛谷上使用团队系统非常方便的添加自己的题目。如果在自己的电脑上配置题目和测试数据,每题需要花费时间 5 分钟;而在洛谷团队中上传私有题目,每题只需要花费 3 分钟,但是上传题目之前还需要一次性花费 11 分钟创建与配置团队。现在要配置 n(n≤100) 道题目,如果本地配置花费的总时间短,请输出 Local,否则输出 Luogu。

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

P5714 【深基3.例7】肥胖问题

题目描述
BMI 指数是国际上常用的衡量人体胖瘦程度的一个标准,其算法是 m/h^2 (40≤m≤120,1.4≤h≤2.0),其中 m 是指体重(千克),h 是指身高(米)。不同体型范围与判定结果如下:

小于 18.5:体重过轻,输出 Underweight;
大于等于 18.5 且小于 24:正常体重,输出 Normal;
大于等于 24:肥胖,不仅要输出 BMI 值(使用 cout 的默认精度),然后换行,还要输出 Overweight;

现在给出体重和身高数据,需要根据 BMI 指数判断体型状态并输出对应的判断。

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

P5715 【深基3.例8】三位数排序

题目描述
给出三个整数 a,b,c(0≤a,b,c≤100),要求把这三位整数从小到大排序。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[3];
	for(int i=0;i<3;i++)
	{
		cin>>a[i];
	}
	sort(a,a+3);
	
	for(int i=0;i<3;i++)
	{
		cout<<a[i];
		if(i!=2)
		{
			cout<<" ";
		}
	}
	
	return 0;
}

P5716 【深基3.例9】月份天数

题目描述
输入年份和月份,输出这一年的这一月有多少天。需要考虑闰年。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int year,month;
	cin>>year>>month;
	if(month!=2)
	{
		if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))  cout<<31;
		else if((month==4)||(month==6)||(month==9)||(month==11))  cout<<30;
	 } 
	 
	else
	{
		if((year%4==0&&year%100!=0)||(year%400==0))  cout<<29;
		else cout<<28;
	}
	return 0;
}

P1085 [NOIP2004 普及组] 不高兴的津津

题目描述

津津上初中了。妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班。另外每周妈妈还会送她去学习朗诵、舞蹈和钢琴。但是津津如果一天上课超过八个小时就会不高兴,而且上得越久就会越不高兴。假设津津不会因为其它事不高兴,并且她的不高兴不会持续到第二天。请你帮忙检查一下津津下周的日程安排,看看下周她会不会不高兴;如果会的话,哪天最不高兴。

输入格式

输入包括77行数据,分别表示周一到周日的日程安排。每行包括两个小于1010的非负整数,用空格隔开,分别表示津津在学校上课的时间和妈妈安排她上课的时间。

输出格式

一个数字。如果不会不高兴则输出00,如果会则输出最不高兴的是周几(用1, 2, 3, 4, 5, 6, 71,2,3,4,5,6,7分别表示周一,周二,周三,周四,周五,周六,周日)。如果有两天或两天以上不高兴的程度相当,则输出时间最靠前的一天。

输入输出样例
输入
5 3
6 2
7 2
5 3
5 4
0 4
0 6
输出
3

#include<bits/stdc++.h>
using namespace std;
int cmp(int a,int b)
{
	return a>b;
}
int main()
{
	int a[7],b[7],c[7],c1[7],day;
	for(int i=0;i<7;i++)
	{
		cin>>a[i]>>b[i];
		c[i]=a[i]+b[i];
		c1[i]=c[i];
	}
	
	sort(c1,c1+7,cmp);//排c1
	
	for(int i=0;i<7;i++)
	{
		if(c1[0]==c[i])
		{
			day=i;
			break;
		}
	}
	
	if(c1[0]<=8)  cout<<0;
	else cout<<day+1;
	
	return 0;
}

P1909 [NOIP2016 普及组] 买铅笔

在这里插入图片描述
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,a[2],b[2],c[2],aa,bb,cc;//后三个表示最少画的钱
	int m[3]; 
	cin>>n>>a[0]>>a[1]>>b[0]>>b[1]>>c[0]>>c[1];
	
	int i=1;
	while(i*a[0]<n)
	{
		i++;
	}
	aa=i;
	m[0]=aa*a[1];
	
	i=1;
	while(i*b[0]<n)
	{
		i++;
	}
	bb=i;
	m[1]=bb*b[1];
	
	i=1;
	while(i*c[0]<n)
	{
		i++;
	}
	cc=i;
	m[2]=cc*c[1];
	
	sort(m,m+3);
	cout<<m[0];
	return 0;
}

P1055 [NOIP2008 普及组] ISBN 号码

在这里插入图片描述
在这里插入图片描述
第一次做的时候卡在两个测试点上,于是删了重写,string真好用。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string a;
	cin>>a;
	int sum=0,num=1;
	
	for(int i=0;i<12;i++)
	{
		if(a[i]=='-')  continue;
		else sum=sum+(a[i]-'0')*num;num++;
	}
	
	sum=sum%11;
	
	if(sum==10)
	{
		if(a[12]=='X')  cout<<"Right";
		else  
		{
			for(int i=0;i<12;i++)
			{
				cout<<a[i];
			}
			cout<<"X";
		}
	}
	
	else
	{
		if(a[12]-'0'==sum) cout<<"Right";
		else  
		{
			for(int i=0;i<12;i++)
			{
				cout<<a[i];
			}
			cout<<sum;
		}
	}
	return 0;
}

P1422 小玉家的电费

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	double money=0;
	cin>>n;
	if(n<=150)  money=n*0.4463;
	else if(n>150&&n<=400)  money=150*0.4463+(n-150)*0.4663;
	else if(n>400)  money=150*0.4463+250*0.4663+(n-400)*0.5663;
	
	cout.precision(1);
	cout<<fixed<<money;
	return 0;
}

P1424 小鱼的航程(改进版)

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x,n,sum=0;
	cin>>x>>n;
	while(n--)
	{
		if(x!=6&&x!=7)
		{
			sum=sum+250;
			
		}
		x++;
		if(x==8) x=x-7;
	}
	
	cout<<sum;
	return 0;
}

P1888 三角函数

在这里插入图片描述
ps:
分数约分其实就是分子分母同时除以最大公约数。
求最大公约数用辗转相除法
其函数如下(递归版):

//求最大公约数函数 
int gcd(int a,int b)
{
	return b==0?a:gcd(b,a%b);
}

总体代码:

#include<bits/stdc++.h>
using namespace std;

//求最大公约数函数 
int gcd(int a,int b)
{
	return b==0?a:gcd(b,a%b);
}

int main()
{
	int a[3],x;
	for(int i=0;i<3;i++)
	{
		cin>>a[i];
	}
	sort(a,a+3);
	
	//判断是否有公约数
	int yue=gcd(a[2],a[0]);
	
	a[2]=a[2]/yue;
	a[0]=a[0]/yue;
	
	cout<<a[0]<<"/"<<a[2];
	return 0;
}

P5717 【深基3.习8】三角形分类

点我

P4414 [COCI2006-2007#2] ABC

题目描述
You will be given three integers A, B and C. The numbers will not be given in that exact order, but we do know that A is less than B and B less than C. In order to make for a more pleasant viewing, we want to rearrange them in the given order.

输入格式
The first line contains three positive integers A, B and C, not necessarily in that order. All three numbers will be less than or equal to 100. The second line contains three uppercase letters ‘A’, ‘B’ and ‘C’ (with no spaces between them) representing the desired order.

输出格式
Output the A, B and C in the desired order on a single line, separated by single spaces.

题意翻译
【题目描述】

三个整数分别为 A,B,C. 这三个数字不会按照这样的顺序给你,但它们始终满足条件:A<B<C . 为了看起来更加简洁明了,我们希望你可以按照给定的顺序重新排列它们。

【输入格式】

第一行包含三个正整数 A,B,C,不一定是按这个顺序。这三个数字都小于或等于 100 。第二行包含三个大写字母 A 、 B 和 C(它们之间没有空格)表示所需的顺序.

【输出格式】

在一行中输出 A ,B 和 C ,用一个 (空格)隔开.

感谢@smartzzh 提供的翻译

输入输出样例
输入
1 5 3
ABC
输出
1 3 5
输入
6 4 2
CAB
输出
6 2 4

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[3];
	char b[3];
	cin>>a[0]>>a[1]>>a[2]>>b[0]>>b[1]>>b[2];
	
	sort(a,a+3);
	
	//第一个 
	if(b[0]=='A')  cout<<a[0];
	else if(b[0]=='B')  cout<<a[1];
	if(b[0]=='C')  cout<<a[2];
	
	cout<<" ";
	
	//第二个
	if(b[1]=='A')  cout<<a[0];
	else if(b[1]=='B')  cout<<a[1];
	if(b[1]=='C')  cout<<a[2];
	
	cout<<" ";
	
	//第三个
	if(b[2]=='A')  cout<<a[0];
	else if (b[2]=='B')  cout<<a[1];
	if(b[2]=='C')  cout<<a[2];
	return 0;
}

P1046 [NOIP2005 普及组] 陶陶摘苹果

题目描述
陶陶家的院子里有一棵苹果树,每到秋天树上就会结出 10 个苹果。苹果成熟的时候,陶陶就会跑去摘苹果。陶陶有个 30 厘米高的板凳,当她不能直接用手摘到苹果的时候,就会踩到板凳上再试试。

现在已知 10 个苹果到地面的高度,以及陶陶把手伸直的时候能够达到的最大高度,请帮陶陶算一下她能够摘到的苹果的数目。假设她碰到苹果,苹果就会掉下来。

输入格式
输入包括两行数据。第一行包含 10 个 100 到 200 之间(包括 100 和 200 )的整数(以厘米为单位)分别表示 10 个苹果到地面的高度,两个相邻的整数之间用一个空格隔开。第二行只包括一个 100 到 120 之间(包含 100 和 120 )的整数(以厘米为单位),表示陶陶把手伸直的时候能够达到的最大高度。

输出格式
输出包括一行,这一行只包含一个整数,表示陶陶能够摘到的苹果的数目。

输入输出样例
输入
100 200 150 140 129 134 167 198 200 111
110
输出
5
说明/提示
【题目来源】

NOIP 2005 普及组第一题

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[10],b;
	for(int i=0;i<10;i++)
	{
		cin>>a[i];
	}
	
	cin>>b;
	sort(a,a+10);
	
	int temp=0;
	for(int i=0;i<10;i++)
	{
		if(a[i]<=b+30)  temp++;
		else break;
	}
	
	cout<<temp;
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

karshey

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

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

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

打赏作者

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

抵扣说明:

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

余额充值