Spring Couplets 春联 c语言题解——2021 年江苏省大学生程序设计大赛Problem-A

原题链接:Problem - A - Codeforces

A. Spring Couplets

time limit per test:1 second

memory limit per test:256 megabytes

input:standard input

output:standard output

Spring Festival 2022 is coming. Vingying can't hold back his excitement and starts to prepare the spring couplets.

A spring couplet is a pair of lines of poetry that adhere to specific rules. They can be seen on doorways in Chinese communities, displayed as part of the Spring Festival. The text of the couplets is often traditional and contains hopes for prosperity.

A spring couplet needs to meet various rules. For example, both lines must have the same number of Chinese characters, and the meanings of the two lines must be related, and so on. Here, we only focus on the tonal rules.

As we know, there are four primary tones of Chinese characters: flat (ā), rising (á), falling-rising (ǎ), and falling (à). These can be categorized into two tone patterns: level tones and oblique tones. In detail, the flat tone and rising tone belong to level tones, and the falling-rising tone and falling tone belong to oblique tones.

A spring couplet must adhere to the following tonal rules:

  • The tone pattern of one line must be the inverse of the other. It means if one character is of a level tone, its corresponding character in the other line must be of an oblique tone, and vice versa.
  • The last character of the first line should be of an oblique tone, which forces the last character of the second line to be of a level tone.

Due to Vingying's creative mind, he creates T couplets instantly. He wants to know whether each couplet adheres to the tonal rules.

Input

The first line of the input contains an integer T (1≤T≤100), indicating the number of couplets.

For each couplet, the first line contains an integer n (1≤n≤20), indicating the number of Chinese characters for each line.

For the next two lines, each line contains n strings, each of which represents a Chinese character. Each string consists of lowercase letters, indicating the spelling of the Chinese character, followed by a digital number 1 (flat), 2 (rising), 3 (falling-rising), or 4 (falling) representing the tone.

It is guaranteed that the length of each string is no more than 7 and no less than 2.

Output

For each couplet, if it adheres to the tonal rules, print YES in a single line. Otherwise, print NO in a single line.

Example

input

4
7
qian1 men2 wan3 hu3 tong2 tong2 ri4
zong3 ba3 xin1 tao2 huan4 jiu4 fu2
7
ping2 ping2 ze4 ze4 ping2 ping2 ze4
ze4 ze4 ping2 ping2 ze4 ze4 ping2
7
gou3 li4 guo2 jia1 sheng1 si3 yi3
qi3 yin1 huo4 fu2 bi4 qu1 zhi1
3
nun1 heh1 heh1
a4 a4 a4

output

YES
YES
NO
NO

Note

For the third example, the tones of the first character do not adhere to the rule, as they are both oblique tones.

For the fourth example, the tones of the last character do not adhere to the rule, as the last character in the first line is not of an oblique tone.

翻译: 

 A.春联

时间限制:1s

内存限制:256MB

输入:标准输入

输出:标准输出

2022年春节即将到来。Vingying忍不住激动起来,开始准备春联。

春联是一对遵循特定规则的诗句。他们可以在中国社区的门口看到,这是春节的一部分。对联的文本通常是传统的,包含着对繁荣的希望。

春联需要符合各种规则。例如,两行必须有相同数量的汉字,并且两行的含义必须相互关联等等。在这里,我们只关注声调规则

众所周知,汉字有四种基本声调:阴平(ā)、阳平(á)、上声(ǎ)和去声(à)。这些音调可以分为两种:平声仄声。具体来说,阴平和阳平属于平声,上声和去声属于仄声

春联必须遵守以下声调规则

  • 一行的平仄必须与另一行相反。这意味着,如果一个汉字是平声,那么另一行中对应的汉字必须是仄声,反之亦然。
  • 第一行的最后一个汉字应该是仄声,这使得第二行的最后一个汉字是平声

由于Vingying的创造性思维,他立即创作了T幅对联。他想知道每一副对联是否符合声调规则

输入

输入的第一行包含一个整数T(1≤T≤100),表示对联的数量。

对于每幅对联,第一行包含一个整数n(1≤n≤20) ,表示每行的汉字数。

对于接下来的两行,每行包含n个字符串,每个字符串表示一个汉字。每个字符串由小写字母组成,表示汉字的拼写,后面是数字1(阴平)、2(阳平)、3(上声),或4(去声)表示声调。

保证每个字符串的长度不大于7且不小于2。

输出

对于每一副对联,如果它遵循声调规则,则在一行中打印“YES”。否则,在一行中打印“NO”。

样例:

输入:

4
7
qian1 men2 wan3 hu3 tong2 tong2 ri4
zong3 ba3 xin1 tao2 huan4 jiu4 fu2
7
ping2 ping2 ze4 ze4 ping2 ping2 ze4
ze4 ze4 ping2 ping2 ze4 ze4 ping2
7
gou3 li4 guo2 jia1 sheng1 si3 yi3
qi3 yin1 huo4 fu2 bi4 qu1 zhi1
3
nun1 heh1 heh1
a4 a4 a4

输出:

YES
YES
NO
NO

提示:

对于第三个示例,第一个字符的声调不符合规则,因为它们都是仄声

对于第四个示例,最后一个字符的声调不符合规则,因为第一行中的最后一个字符不是仄声

思路:

先遍历对联的第一行,遇到空格停止,然后遍历第二行,遇到空格停止,这两个空格的前一个字符即表示声调,判断它们是否符合规则,不符则打印NO,运用goto方便跳出两层循环。若符合则循环上述操作。当循环结束还有最后一个汉字没有判断。要判断最后一个只需要保证第一行最后一个字符为3或4并且第二行最后一个字符为1或2即可。对联每行只有一个汉字,通过第一个while循环只会将first遍历到'\0',需要第二个while循环将second遍历到'\0'。

c语言实现:

#include 
#include 
int isRight(char* ch1, char* ch2)
{
	if ((abs(*ch1 - *ch2) <= 1) && (*ch1 + *ch2 != '5' + '0'))
		return 1;
	else
		return 0;
}
 
int main()
{
	int T, i;
	scanf("%d", &T);
	for (i = 0; i < T; i++)
	{
		int k = 0, l = 0, n;
		scanf("%d", &n);
		getchar();
		char first[140] = { 0 };
		char second[140] = { 0 };
		gets(first);
		gets(second);
		while (first[k] != '\0')
		{
			if (first[k] == ' ')
				while (second[l] != '\0')
				{
					if (second[l] == ' ')
					{
						if (isRight(first+k-1, second+l-1))
						{
							printf("NO\n");
							goto end;
						}
						l++;
						break;
					}
					l++;
				}
			k++;
		}
		while (second[l] != '\0')
			l++;
		if ((first[k - 1] >= '3') && second[l - 1] <= '2')
			printf("YES\n");
		else
			printf("NO\n");
	end:
		;
	}
	return 0;
}

你可能感兴趣的:(C语言小题目,spring,c语言,指针)