ACM水题-Differing sequence(AC,递归,分情况)

Differing sequence

Time Limit:1000MS  Memory Limit:65536K
Total Submit:54 Accepted:20

Description

Numbers are beautiful. Sequences are beautiful. One day I sit beside my desk and staring at some sequences I wrote on my paper. And I’ve drown in the beautifulness of these sequences. Suddenly I saw that when I write down the absolute difference between every 2 digits, I can get another sequence. for example: I have a sequence 1397880, then I write down the absolute difference between every 2 digits of 1397880, and I’ll get 262108. As the difference between 1 and 3 is 2, and the difference between 3 and 9 is 6 and so on.. And then I got curious that is a given sequence can be generated by another sequence? such as: 262108 can be generated by 1397880, but 947 can never generated by any given sequence. Now I give this problem to you. You are to tell Yes or No.

Input

Input have several cases. Each case has a sequence of digits. A single 0 terminate the input, and you don’t need to process this 0. the length of sequence will not exceed 100.

Output

If the sequence can be generated, output “Yes”, else “No”.

Sample Input

262108
947
0

Sample Output

Yes
No

Source

GDUT Monthly 2007.5 by ziliang

 

  

/*	--------------------------------------------------------------------------------------------------
	一开始看着这一道题的时候,拖延症又发作了,想了好几天都没有解决方案。其实,不是我不想去想,而是一开始
	就想冲着一个最优方案去想,比如一定要向线性时间复杂度去想,结果。。。一直卡在某个地方,想了好几天都
	没有想出可行的解决的方案。。结果,搞到自己兴趣的减少。后来昨天认真想了一下,其实不难,只是自己钻牛角
	尖而已。

	大概思路:
	
	这一道题其实就是用递归的思想来解决。
	你每选择一个数字,必然会影响到你下一个选择的数字,如果选择的数字符合条件,则再选择一下个数字。如果不符合
	条件则回来最初的递归调用处,而不是上一个递归调用。这里有些特别,前面已经说了,如果改变一个数字,必然
	会改变所有数字,所以必然从第一个调用重新开始。比如说:3 由4、1相减所得,也可用5、2相减所得,但是如果改变
	其中一个数字,前后的都要改变。所以,就是这样。


	此外,这一道需要注意的就是必须要将所有情况分清楚。一开始就是自己没有完全列清所有情况,结果一直WA。
	比如,if里面用&&两个条件,那跟着的else if就应该用3个,就好比2位二进制数字一样,有11 10 01 00这四种情况。
	所以,一定要列清楚。


	状况:AC,0MS

	--------------------------------------------------------------------------------------------------	*/


#include<stdio.h>
#include<string.h>

int n = 0 ;
char szSeq[105] ;

int Find(int nIndex,int nValue) ;

int main(void)
{
	int i = 0 ;
	int fCan = 0 ;
	
	while(scanf("%s",szSeq), strcmp(szSeq,"0") != 0)
	{
		n = strlen(szSeq) ;
		fCan = 0 ;
		
		for(i = 1 ; i <= 9 ; ++i)
		{
			fCan = Find(0,i) ;
			
			if(1 == fCan)
			{
				break ;
			}
		}
		
		if(1 == fCan)
		{
			puts("Yes") ;
		}
		else
		{
			puts("No") ;
		}
	}
	return 0 ;
}


int Find(int nIndex,int nValue)
{
	int nNum = 0 ;
	if(nIndex >= n )
	{
		return 1 ;
	}
	else
	{
		nNum = szSeq[nIndex] - '0' ;
		if(nValue <= nNum)
		{
			if(nNum + nValue < 10 && nNum != nValue)
			{
				return Find(nIndex+1,nNum+nValue) ;
			}
			else if(nNum == nValue && nNum+nValue < 10)
			{
				return Find(nIndex+1,0)||Find(nIndex+1,nNum+nValue) ;
			}
			else if(nNum == nValue)
			{
				return Find(nIndex+1,0) ;
			}
			else
			{
				return 0 ;
			}
		}
		else if(nValue > nNum)
		{
			if(nNum + nValue < 10)
			{
				return Find(nIndex+1,nValue+nNum)||Find(nIndex+1,nValue-nNum) ;
			}
			else
			{
				return Find(nIndex+1,nValue-nNum) ;
			}		
		}
	}
	return 0 ;
}

你可能感兴趣的:(input,each,output)