第一次周赛AC题题解

A题

One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.

Pete and Billy are great fans of even numbers, that’s why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that’s why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight.

Input
The first (and the only) input line contains integer number w (1 ≤ w ≤ 100) — the weight of the watermelon bought by the boys.

Output
Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case.

Examples
Input
8
Output
YES.

在这里插入图片描述
这题难度不大,
首先4KG以下的西瓜分不成2个双数的部分。
所以要满足的第一个条件是西瓜>=4KG
其次大于4KG的西瓜要分成2个双数部分
那么这个西瓜只能是双数所以a%2==0

D题

给定三条边,请你判断一下能不能组成一个三角形。
Input
输入数据第一行包含一个数M,接下有M行,每行一个实例,包含三个正数A,B,C。其中A,B,C <1000;
Output
对于每个测试实例,如果三条边长A,B,C能组成三角形的话,输出YES,否则NO。
Sample Input
2
1 2 3
2 2 2
Sample Output
NO
YES

在这里插入图片描述
这题原本很简单,而且我们也做过。
要用到两边和大于第三边。
但是做的时候,手还是习惯地设置变量为int类型。
这导致了我想一个多小时都没AC,还提交了5次错误答案。所以不敢做了,就先跳过了这题去做其他。
最后才想起。虽然AC了,但是太不应该了。

第I题

统计每个元音字母在字符串中出现的次数。
Input
输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。
Output
对于每个测试实例输出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开。

请特别注意:最后一块输出后面没有空行:)
Sample Input
2
aeiou
my name is ignatius
Sample Output
a:1
e:1
i:1
o:1
u:1

a:2
e:1
i:3
o:0
u:1

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string a[1000];
	int b, c = 0, d = 0,h=0,x;
	int g[1000][5] = {0,0,0,0,0};
	cin >> b;
	getchar();
	for (; b != c; c++)
	{
		getline(cin,a[c]);
	}
	c = 0;
	x = a[c].length();
	for (; b != c;h++)
	{
		for (d = 0;d!=x; d++)
		{
			if (a[c][d] == 97)
				g[h][0]++;
			if (a[c][d] == 101)
				g[h][1]++;

			if (a[c][d] ==105 )
				g[h][2]++;

			if (a[c][d] == 111)
				g[h][3]++;

			if (a[c][d] == 117)
				g[h][4]++;
		}
		c++;
		x = a[c].size();
	}
	for (c = 0, h = 0; h != b; h++)
	{
		cout << "a:" << g[h][0] << endl << "e:" << g[h][1] << endl << "i:" << g[h][2] << endl << "o:" << g[h][3] << endl << "u:" << g[h][4] << endl;
		if (h != b - 1)
			cout<< endl;
	}
		return 0;
}

这题有一个难的地方在于他给的INPUT是包含空格的,
但是用string时,它遇到空格会自动分割成多个字符串,所以在cin的时候 for不能用!=32来判断结束;
所以用到了getline来接受空格,但是getline又有一个问题,那就是getline也会接受回车。
这导致一开始输入整数b后按回车,回车被getline接收了。
解决办法就是再用一个getchar()(应该用getline也可以)来接收掉回车,然后再用第二个getline接收数据;
这个时候for循环的判断条件就不能用!=32了,只能用s.size()来判断;
最后题目要求的最后一行不能有空行,那么就在for循环输出的时候加上一句if来判断;

第H题

在这里插入图片描述

这题不难
从(0,0)走到(a,b)有两种情况:第一走曲线,(a,b)不在坐标轴上,这时候用化曲为直的思想,走到的步数大于等于横坐标加纵坐标,那么可能会有多出的步数。到目的地后多走出一步,就要多一步走回来,所以要用到s-a-b是双数来判断。
第二走直线,直线是特殊的曲线,所以分析一下就知道,第一种情况已经包含了第二种了,所以不用判断;

最后因为是坐标轴,题目给的条件里a,b可能是负数;所以要用到abs()或者fabs()取绝对值;
然后和前面一样的判断方法。

最后周赛总结:题目有难有易。但是就算是容易的题目也有很多的细节要注意。不能掉以轻心。在提交代码之前要多输入不同的数据来检查,不然在提交了多次都没有AC的时候,就感到很烦躁。这次我感觉第J题我也有机会做出来的。已经做到了输入一个时间,输入一个第几天,再输入,再输出但是最后都没有AC。有点遗憾。

有空的时候再尝试,看看哪里错了。

#include<iostream>
#include<string>
using namespace std;
struct time
{
	int year;
	int munth;
	int day;
};
int days(time a, int b = 0)
{
	if (a.year % 4 == 0)
	{
		switch (a.munth)
		{
		case 1:b = 0; break;
		case 2:b = 31; break;
		case 3:b = 60; break;
		case 4:b = 91; break;
		case 5:b = 121; break;
		case 6:b = 152; break;
		case 7:b = 182; break;
		case 8:b = 213; break;
		case 9:b = 244; break;
		case 10:b = 274; break;
		case 11:b = 305; break;
		case 12:b = 335; break;
		}
		return b + a.day;
	}
	else
	{
		switch (a.munth)
		{
		case 1:b = 0; break;
		case 2:b = 31; break;
		case 3:b = 59; break;
		case 4:b = 90; break;
		case 5:b = 120; break;
		case 6:b = 151; break;
		case 7:b = 181; break;
		case 8:b = 212; break;
		case 9:b = 243; break;
		case 10:b = 273; break;
		case 11:b = 304; break;
		case 12:b = 334; break;
		}
		return b + a.day;
	}
};

int main()
{
	string a,p[3];
	while (cin >> a)
	{
		int b=0, c, o[3];
		c = a.size()-1;
		for (; a[c] != 0; c--)
		{
			if (a[c] == 47)
			{
				p[b] = a.substr(c + 1);
				o[b] = atoi(p[b].c_str());
				a.erase(c);
				b++;
			}
			if (b == 2)
			{
				o[b] = atoi(a.c_str());
				break;
			}
		}
		time z;
		z.day = o[0]; z.munth = o[1]; z.year = o[2];
		c=days(z);
		cout << c<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43508349/article/details/84917782
今日推荐