HDU 4161 Iterated Difference


Iterated Difference

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 784    Accepted Submission(s): 503


Problem Description
You are given a list of N non-negative integers a(1), a(2), ... , a(N). You replace the given list by a new list: the k-th entry of the new list is the absolute value of a(k) - a(k+1), wrapping around at the end of the list (the k-th entry of the new list is the absolute value of a(N) - a(1)). How many iterations of this replacement are needed to arrive at a list in which every entry is the same integer?

For example, let N = 4 and start with the list (0 2 5 11). The successive iterations are:

2 3 6 11
1 3 5 9
2 2 4 8
0 2 4 6
2 2 2 6
0 0 4 4
0 4 0 4
4 4 4 4
Thus, 8 iterations are needed in this example.
 

Input
The input will contain data for a number of test cases. For each case, there will be two lines of input. The first line will contain the integer N (2 <= N <= 20), the number of entries in the list. The second line will contain the list of integers, separated by one blank space. End of input will be indicated by N = 0.
 

Output
For each case, there will be one line of output, specifying the case number and the number of iterations, in the format shown in the sample output. If the list does not attain the desired form after 1000 iterations, print 'not attained'.
 

Sample Input
  
  
4 0 2 5 11 5 0 2 5 11 3 4 300 8600 9000 4000 16 12 20 3 7 8 10 44 50 12 200 300 7 8 10 44 50 3 1 1 1 4 0 4 0 4 0
 

Sample Output
  
  
Case 1: 8 iterations Case 2: not attained Case 3: 3 iterations Case 4: 50 iterations Case 5: 0 iterations Case 6: 1 iterations
 



解题思路:

这题其实没什么难度,只要注意把这个数组看成两部分来处理就好了,也就是说s[0]和s[n-1] 作为第一部分,s[1] -> s[n-2] 作为第二个部分,然后通过equal函数来不断的进行循环,停止的条件有两个,一个是sum>1000,还有一个就是数组中的每个元素都相等。注意到以上的两个条件,那就没什么问题了,A掉吧



代码:

# include<cstdio>
# include<iostream>
# include<cstring>
# include<cmath>

using namespace std;

int equal(int * s,int n)
{
    for(int i=1;i < n;i++ )
    {
        if( s[i]!=s[i-1] )
            return 0;
    }
    return 1;
}

int main( void )
{
    int s[21];

    int n,i,t,sum,k=1;

    bool flag;

    while(scanf("%d",&n)!=EOF&&n)
    {
        for(i=0;i<n;i++)
        {
            cin>>s[i];
        }
        sum=0;

        flag=true;

        while( !equal(s,n) )
        {
            t = s[0];
            for(i=0;i<n-1;i++)
                s[i]=abs(s[i+1]-s[i]);
            s[i]=abs(t-s[i]);
            sum++;

            if( sum > 1000 )
            {
                flag = false;
                break;
            }
        }

        if( flag )
            printf("Case %d: %d iterations\n",k++,sum);
        else
            printf("Case %d: not attained\n",k++);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值