牛客编程语言练习赛第十场(C++解题)

 

1.出生日期输入输出

题目描述

输入一个人的出生日期(包括年月日),将该生日中的年、月、日分别输出。

输入描述:

输入只有一行,出生日期,包括年月日,年月日之间的数字没有分隔符。

输出描述:

三行,第一行为出生年份,第二行为出生月份,第三行为出生日期。输出时如果月份或天数为1位数,需要在1位数前面补0。

示例1

输入

20130225

 

输出

year=2013 month=02 date=25

 

备注:

通过scanf函数的%m格式控制可以指定输入域宽,输入数据域宽(列数),按此宽度截取所需数据;通过printf函数的%0格式控制符,输出数值时指定左面不使用的空位置自动填0。

-AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	int y, m, d;
	scanf("%4d%2d%2d", &y, &m, &d);
	printf("year=%d\n", y);
	printf("month=%02d\n", m);
	printf("date=%02d\n", d);
	return 0;
}



 

2.字符金字塔

题目描述

输入一个字符,用它构造一个三角形金字塔。

输入描述:

输入只有一行,一个字符。

输出描述:

该字符构成的三角形金字塔。

示例1

输入

1

 

输出

 

    1
   1 1
  1 1 1
 1 1 1 1
1 1 1 1 1

-AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	char c;
	cin >> c;
	int space = 4;
	for(int i = 0; i < 5; i++) {
		for(int j = 0; j < space; j++) {
			cout << " ";
		}
		space--;
		for(int j = 0; j < i + 1; j++) {
			cout << c << " ";
		}
		cout << endl;
	}
	return 0; 
}


 

3.大小写转换

题目描述

实现字母的大小写转换。多组输入输出。

输入描述:

多组输入,每一行输入大写字母。

输出描述:

针对每组输入输出对应的小写字母。

示例1

输入

 

A
B

输出

 

a
b

备注:

多组输入过程中要注意“回车”也是字母,所以要“吸收”(getchar())掉该字母。

 

-AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	char c;
	while (cin >> c) {
		cout << (char)(c + 32) << endl;
	}
		
	return 0;
}


 

4.KiKi和酸奶

题目描述

BoBo买了一箱酸奶,里面有n盒未打开的酸奶,KiKi喜欢喝酸奶,第一时间发现了酸奶。KiKi每h分钟能喝光一盒酸奶,并且KiKi在喝光一盒酸奶之前不会喝另一个,那么经过m分钟后还有多少盒未打开的酸奶?

输入描述:

多组输入,每组输入仅一行,包括n,h和m(均为整数)。输入数据保证m <= n * h。

输出描述:

针对每组输入,输出也仅一行,剩下的未打开的酸奶盒数。

示例1

输入

8 5 16

 

输出

4

 

-AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	double n, h, m;
	while (cin >> n >> h >> m) {
		cout << (int)(n - 1.0 * m / h) << endl;
	}
	return 0;
}

 

 

5.衡量人体胖瘦程度

题目描述

在计算BMI(BodyMassIndex ,身体质量指数)的案例基础上,判断人体胖瘦程度。BMI中国标准如下表所示。

 

 

输入描述:

多组输入,每一行包括两个整数,用空格隔开,分别为体重(公斤)和身高(厘米)。

输出描述:

针对每行输入,输出为一行,人体胖瘦程度,即分类。

示例1

输入

 

80 170
60 170
90 160
50 185

输出

 

Overweight
Normal
Obese
Underweight

 

-AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	int w, h;
	while (cin >> w >> h) {
		double th = h / 100.0;
		double bmi = 1.0 * w / (th * th);
		if (bmi < 18.5) {
			cout << "Underweight";
		} else if (bmi <= 23.9) {
			cout << "Normal";
		} else if (bmi <= 27.9) {
			cout << "Overweight";
		} else if (bmi > 27.9) {
			cout << "Obese";
		}
		cout << endl;
	}
	return 0;
}

 

 

6.获得月份天数

题目描述

KiKi想获得某年某月有多少天,请帮他编程实现。输入年份和月份,计算这一年这个月有多少天。

输入描述:

多组输入,一行有两个整数,分别表示年份和月份,用空格分隔。

输出描述:

针对每组输入,输出为一行,一个整数,表示这一年这个月有多少天。

示例1

输入

 

2008 2

输出

29

 

-AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	int year, month;
	int m[] = {31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	while (cin >> year >> month) {
		int flag = 0;
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) flag = 1;
		
			if (month == 2) {
				if (flag) {
					cout << 29;
				} else {
					cout << 28;
				}
			} else {
				cout << m[month - 1];
			}
			
			cout << endl;
	}
			
			
		
	return 0;
}

 

 

7.竞选社长

题目描述

假设你们社团要竞选社长,有两名候选人分别是A和B,社团每名同学必须并且只能投一票,最终得票多的人为社长.

输入描述:

一行,字符序列,包含A或B,输入以字符0结束。

输出描述:

一行,一个字符,A或B或E,输出A表示A得票数多,输出B表示B得票数多,输出E表示二人得票数相等。

示例1

输入

 

ABBABBAAB0

输出

 

B

 

-AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	string str;
	int cntA = 0, cntB = 0;
	getline(cin, str, '0');
	for(char c : str) {
		if (c == 'A') cntA++;
		else if (c == 'B') cntB++;
	}
	if (cntA == cntB) {
		cout << "E";
	} else if (cntA > cntB) {
		cout << "A";
	} else {
		cout << "B";
	}
	return 0;
}

 


 

7.K形图案

题目描述

KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的K形图案。

输入描述:

 

多组输入,一个整数(2~20)。

输出描述:

针对每行输入,输出用“*”组成的K形,每个“*”后面有一个空格。

示例1

输入

 

2

输出

 

* * * 
* *  
*   
* *  
* * * 

示例2

输入

 

3

输出

 

* * * * 
* * *  
* *   
*    
* *   
* * *  
* * * * 

示例3

输入

 

4

输出

 

* * * * * 
* * * *  
* * *   
* *    
*     
* *    
* * *   
* * * *  
* * * * *

这道题疯狂挂在输出格式上,后面知道是空格输出的问题。
吃一堑 长一智。
包括这道题的后面三题的正确输出格式要特别注意,每一行的后面也有可能要输出空格!

-AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	int n;
	while (cin >> n) {
		int t = n + 1;
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < t; j++) {
				cout << "* ";
			}
			for(int j = 0; j < i; j++) {
				cout << " ";
			}
			t--;
			cout << endl;
		}
		
		cout << "*";
		for(int i = 0; i < n + 1; i++) {
			cout << " ";
		}
		cout << endl;
		
		t = 2;
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < t; j++) {
				cout << "* ";
			}
			for(int j = 0; j < n + 1; j++) {
				cout << " ";
			}
			t++;
			cout << endl;
		}
		//cout << endl;
	}
		
	return 0;
}

 

 

8.正斜线形图案

题目描述

KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的正斜线形图案。

输入描述:

多组输入,一个整数(2~20),表示输出的行数,也表示组成正斜线的“*”的数量。

输出描述:

针对每行输入,输出用“*”组成的正斜线。

示例1

输入

 

4

输出

 

   *
  * 
 *  
*   

示例2

输入

 

5

输出

 

    *
   * 
  *  
 *   
* 

吃一堑 长一智。
这道题的正确输出格式要特别注意,每一行的后面也有可能要输出空格!

-AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	int n;
	while (cin >> n) {
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n - i - 1; j++) {
				cout << " ";
			}
			cout << "*";
			putchar(10);
		}
		putchar(10);
	}
	return 0;
}

 

 

 

题目描述

KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的X形图案。

输入描述:

多组输入,一个整数(2~20),表示输出的行数,也表示组成“X”的反斜线和正斜线的长度。

输出描述:

 

针对每行输入,输出用“*”组成的X形图案。

示例1

输入

 

5

输出

 

*   *
 * * 
  *  
 * * 
*   *

示例2

输入

 

6

输出

 

*    *
 *  * 
  **  
  **  
 *  * 
*    *

吃一堑 长一智。
这道题的正确输出格式要特别注意,每一行的后面也有可能要输出空格!


-AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	int n;
	while (cin >> n) {
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				if (j == i || j == n - i - 1) {
					cout << "*";
				} else {
					cout << " ";
				}
			}
			cout << endl;
		}
	}
	return 0;
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

重剑DS

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

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

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

打赏作者

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

抵扣说明:

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

余额充值