MKnez wants to construct an array s_1,s_2, \ldots , s_n s1, s2,…, sn satisfying the following conditions:
Each element is an integer number different from 00;
For each pair of adjacent elements their sum is equal to the sum of the whole array.
More formally, s_i \neq 0 si=0 must hold for each 1 \leq i \leq n1≤ i≤ n. Moreover, it must hold that s_1 + s_2 + \cdots + s_n = s_i + s_{i+1} s1+ s2+⋯+ sn= si+ si+1 for each 1 \leq i < n1≤ i< n.
Help MKnez to construct an array with these properties or determine that it does not exist.
Input
Each test contains multiple test cases. The first line contains the number of test cases t t (1 \leq t \leq 1001≤ t≤100). The description of the test cases follows.
The only line of each test case contains a single integer n n (2 \leq n \leq 10002≤ n≤1000) — the length of the array.
Output
For each test case, print "YES" if an array of length n n satisfying the conditions exists. Otherwise, print "NO". If the answer is "YES", on the next line print a sequence s_1,s_2, \ldots, s_n s1, s2,…, sn satisfying the conditions. Each element should be a non-zero integer in the range [-5000,5000][−5000,5000], i. e. -5000 \leq s_i \leq 5000−5000≤ si≤5000 and s_i \neq 0 si=0 should hold for each 1 \leq i \leq n1≤ i≤ n.
It can be proved that if a solution exists then there also exists one which satisfies the additional constraints on the range.
If there are several correct answers, print any of them.
Sample 1
Note
In the first test case, [9,5][9,5] is a valid answer since 9+59+5 (the sum of the two adjacent elements s_1+s_2 s1+ s2) is equal to 9+59+5 (the sum of all elements). Other solutions include [6,-9], [-1,-2], [-5000,5000], \ldots[6,−9],[−1,−2],[−5000,5000],…
For the second test case, let us show why some arrays do not satisfy the constraints:
[1,1,1][1,1,1] — s_1+s_2 = 1+1 = 2 s1+ s2=1+1=2 and s_1+s_2+s_3=1+1+1 = 3 s1+ s2+ s3=1+1+1=3 differ;
[1,-1,1][1,−1,1] — s_1+s_2=1+(-1)=0 s1+ s2=1+(−1)=0 and s_1+s_2+s_3=1+(-1)+1 = 1 s1+ s2+ s3=1+(−1)+1=1 differ;
[0,0,0][0,0,0] — The array s s cannot contain a 00.
This is not a proof, but it can be shown that the answer is "NO".
思路:题意大致就是数组要满足相邻元素之和相等且于所有元素之和相等,所以我们可以列出式子:假如有6个元素:S1+S2=S2+S3=S3+S4=S4+S5=S5+S6=S1+S2+S3+S4+S5+S6.我们可以得到S1=S3,S2=S4,也就是偶数与奇数相等,所以3(S1+S2)=S1+S2+S3+S4+S5+S6=S1+S2,所以S1+S2一定是0。我们发现这是偶数个元素的时候,奇数的元素的时候会多出来一个元素不能配对:举个栗子:5个元素的时候S1+S2=S2+S3=S3+S4=S4+S5=S1+S2+S3+S4+S5,此时我们可以得到2*(S1+S2)+S5=S1+S2;又因为S5=S1.所以3*S1+2*S2=S1+S2 -> 2*S1+S2等于0.但是Sn不能为零。所以我们可以得到2*S1=-S2.,那么假如元素个数为7呢,
:S1+S2=S2+S3=S3+S4=S4+S5=S5+S6=S6+S7=S1+S2+S3+S4+S5+S6+S7. ->
3*(S1+S2)+S1=S1+S2. - > 3*S1=-2*S2.用数学归纳法可以知道为(n/2)*S1=-(n/2-1)S2。再举个例子3:S1+S2=S2+S3=S1+S2+S3 ->S1+S2+S3=S1+S2 -> S3=0;,Sn不为0所以3是不可能满足的。
代码实现如下
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
int main()
{
int t,n,a=1,b=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
if(n==3)printf("NO\n");
else
if(n==1)printf("1\n");
else
if(n%2==0)
{
printf("YES\n");
for(int j=0;j<n;j++)
{
printf("%d ",a);
a*=-1;
}
puts("");
}
else
{
b=n/2;
printf("YES\n");
for(int j=0;j<n/2;j++)
{
printf("%d %d ",(b-1),-b);
}
printf("%d\n",b-1);
}
}
}
题目描述
The cows enjoy mooing at the barn because their moos echo back, although sometimes not completely. Bessie, ever the excellent
secretary, has been recording the exact wording of the moo as it goes out and returns. She is curious as to just how much overlap there is.
Given two lines of input (letters from the set a..z, total length in the range 1..80), each of which has the wording of a moo on it, determine the greatest number of characters of overlap between one string and the other. A string is an overlap between two other strings if it is a prefix of one string and a suffix of the other string.
By way of example, consider two moos:
moyooyoxyzooo
yzoooqyasdfljkamo
The last part of the first string overlaps 'yzooo' with the first part of the second string. The last part of the second string
overlaps 'mo' with the first part of the first string. The largest overlap is 'yzooo' whose length is 5.
POINTS: 50
奶牛们非常享受在牛栏中哞叫,因为她们可以听到她们哞声的回音。虽然有时候并不能完全听到完整的回音。Bessie曾经是一个出色的秘书,所以她精确地纪录了所有的哞叫声及其回声。她很好奇到底两个声音的重复部份有多长。
输入两个字符串(长度为1到80个字母),表示两个哞叫声。你要确定最长的重复部份的长度。两个字符串的重复部份指的是同时是一个字符串的前缀和另一个字符串的后缀的字符串。
我们通过一个例子来理解题目。考虑下面的两个哞声:
moyooyoxyzooo
yzoooqyasdfljkamo
第一个串的最后的部份"yzooo"跟第二个串的第一部份重复。第二个串的最后的部份"mo"跟第一个串的第一部份重复。所以"yzooo"跟"mo"都是这2个串的重复部份。其中,"yzooo"比较长,所以最长的重复部份的长度就是5。
输入格式
* Lines 1..2: Each line has the text of a moo or its echo
输出格式
* Line 1: A single line with a single integer that is the length of the longest overlap between the front of one string and end of the other.
输入输出样例
输入 #1复制
abcxxxxabcxabcd abcdxabcxxxxabcx
输出 #1复制
11
说明/提示
'abcxxxxabcx' is a prefix of the first string and a suffix of the second string.
思路:可以用单词接龙里面的方法,找到重叠的字符个数,单词接龙详情参考12月30日刷题总结
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
int ans=0;
int max(int x,int y)//max和min函数就不多说了
{
return x>y?x:y;
}
int min(int x,int y)
{
return x>y?y:x;
}
void check(char *ch1,char *ch2)//用于找寻重叠的字符个数
{
int len1=strlen(ch1),len2=strlen(ch2),len;
len=min(len1,len2);
int flag;
for(int i=0;i<len;i++)
{
flag=1;
for(int j=0;j<i;j++)
{
if(ch1[len1-i+j]!=ch2[j])
{
flag=0;
break;
}
}
if(flag==1)ans=max(i,ans);
}
}
int main()
{
char ch1[81],ch2[81];
scanf("%s%s",ch1,ch2);
check(ch2,ch1);//查找俩次
check(ch1,ch2);
//获取最大值
printf("%d",ans);
}