• 迎接2012新赛季——HDOJ系列热身赛(2) Problem A HDU 4161 Iterated Difference


    Problem A

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


    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
     
     
     
     
     
    水题~~~~~
    迭代就可以了!
    #include<stdio.h>
    #include<math.h>
    #include<iostream>
    using namespace std;
    int a[50];
    int main()
    {
    int N,res,j;
    int iCase=0;
    while(scanf("%d",&N),N)
    {
    iCase++;
    for(int i=0;i<N;i++)
    {
    scanf("%d",&a[i]);
    }
    for(j=0;j<N-1;j++)
    {
    if(a[j]!=a[j+1])break;
    }
    if(j>=N-1)
    {
    printf("Case %d: 0 iterations\n",iCase);
    continue;

    }
    for(res=1;res<=1000;res++)
    {
    a[N]=a[0];
    for(int i=0;i<N;i++)
    {
    a[i]=abs(a[i]-a[i+1]);
    }
    for(j=0;j<N-1;j++)
    {
    if(a[j]!=a[j+1])break;
    }
    if(j>=N-1)break;
    }
    if(res<=1000)printf("Case %d: %d iterations\n",iCase,res);
    else printf("Case %d: not attained\n",iCase);
    }
    return 0;
    }
  • 相关阅读:
    Java网络编程详解
    android 取mac若干问题
    android问题 This version of android studio is incompatible with the gradle version used.
    c#将DataTable内容导出为CSV文件
    C#下利用正则表达式实现字符串搜索功能的方法(转)
    C#正则表达式入门
    java 文件类 null与exists()是不一样的
    c#线程中下载文件到本地
    Git 常用命令
    Web 监听器
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2378235.html
Copyright © 2020-2023  润新知