zoukankan      html  css  js  c++  java
  • HDU 4161 Iterated Difference(简单题)

    Iterated Difference

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

    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

    Source

    The 2011 Rocky Mountain Regional Contest

    Recommend

    lcy

     代码如下:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    const int N = 21;
    int a[N], flag, n;
    void Judge()
    {
    for (int i = 0; i < n; ++i)
    {
    if (a[i] != a[0])
    {
    return;
    }
    }
    flag = 1;
    }
    int Abs(int a)//取绝对值
    {
    return a >= 0 ? a : -a;
    }
    void Change()
    {
    int i;
    for (i = 0; i < n; i ++)
    {
    a[i] = Abs(a[i + 1] - a[i]);
    }
    a[n] = a[0];//更新
    }
    int main()
    {
    int i, ans, count = 1;
    while (scanf("%d", &n) != EOF && n)
    {
    memset(a, 0, sizeof(a));
    for (i = 0; i < n ; ++i)
    {
    scanf("%d", &a[i]);
    }
    a[n] = a[0];
    ans = 0;
    flag = 0;
    while (ans <= 1000 && !flag)
    {
    Judge();
    if (!flag)
    {
    ans ++;
    Change();
    }
    }
    printf("Case %d: ", count);
    count ++;
    if (flag)
    {
    printf("%d iterations\n", ans);
    }
    else
    {
    printf("not attained\n");
    }
    }
    }



  • 相关阅读:
    Sprinig.net 双向绑定 Bidirectional data binding and data model management 和 UpdatePanel
    Memcached是什么
    Spring.net 网络示例 codeproject
    jquery.modalbox.show 插件
    UVA 639 Don't Get Rooked
    UVA 539 The Settlers of Catan
    UVA 301 Transportation
    UVA 331 Mapping the Swaps
    UVA 216 Getting in Line
    UVA 10344 23 out of 5
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2378551.html
Copyright © 2011-2022 走看看