zoukankan      html  css  js  c++  java
  • HDU 3161 Iterated Difference 暴力

    Iterated Difference

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


    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 <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 100
    const int inf=0x7fffffff;   //无限大
    ll n[maxn];
    ll n2[maxn];
    int N;
    int main()
    {
        int cas=1;
        int N;
        while(cin>>N)
        {
            if(N==0)
                break;
            for(int i=0;i<N;i++)
            {
                cin>>n[i];
                n2[i]=n[i];
            }
            ll ans=0;
            int flag=0;
            int kiss=0;
            for(int i=0;i<N;i++)
            {
                if(n[i]!=n[(i+1)%N])
                {
                    kiss=0;
                    break;
                }
                if(i==N-1)
                    kiss=1;
            }
            while(kiss==0&&ans<=1000)
            {
    
                for(int i=0;i<N;i++)
                {
                    n[i]=fabs(n2[i]-n2[(i+1)%N]);
                }
                for(int i=0;i<N;i++)
                {
                    n2[i]=n[i];
                }
                ans++;
                for(int i=0;i<N;i++)
                {
                    if(n[i]!=n[(i+1)%N])
                    {
                        kiss=0;
                        break;
                    }
                    if(i==N-1)
                        kiss=1;
                }
            }
            if(kiss==1)
                printf("Case %d: %d iterations
    ",cas,ans);
            else
                printf("Case %d: not attained
    ",cas);
            cas++;
        }
        return 0;
    }
     
  • 相关阅读:
    页面加载完没有其他操作的情况下直接获取音频时长为NAN问题
    关于mysql的一些操作
    阿里云服务器登录不上 提示:之前用于连接到 (公网ip) 的凭据无法工作(1核1G) 以及阿里云新版本安全组策略没有开启80端口导致网站只能ping通 访问不到的问题
    微信浏览器禁止页面下拉查看网址(不影响页面内部scroll)
    2018年11月17号第一次参加源创会记录
    使用了eclipse10年之后,我终于投向了IDEA
    spring/spring boot/spring cloud书籍推荐
    python数据库连接例子
    Spring Cloud Eureka配置文件例子与较为详细说明
    spring源代码下载并导入eclipse技巧
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4254708.html
Copyright © 2011-2022 走看看