zoukankan      html  css  js  c++  java
  • HDUSTOJ-1559 Vive la Difference!(简单题)

    1559: Vive la Difference!

    时间限制: 3 Sec  内存限制: 128 MB
    提交: 18  解决: 14
    [提交][状态][讨论版]

    题目描述

    Take any four positive integers: a, b, c, d. Form four more, like this:

    | a - b|,| b - c|,| c - d|,| d - a|

    That is, take the absolute value of the differences of a with b, b with c, c with d, and d with a. (Note that a zero could crop up, but they'll all still be non-negative.) Then, do it again with these four new numbers. And then again. And again. Eventually, all four integers will be the same. For example, start with 1,3,5,9:


    1 3 5 9 
    2 2 4 8 (1) 
    0 2 4 6 (2) 
    2 2 2 6 (3) 
    0 0 4 4 (4) 
    0 4 0 4 (5) 
    4 4 4 4 (6)

    In this case, the sequence converged in 6 steps. It turns out that in all cases, the sequence converges very quickly. In fact, it can be shown that if all four integers are less than 2n, then it will take no more than 3*n steps to converge!

    Given a, b, c and d, figure out just how quickly the sequence converges.

    输入

    There will be several test cases in the input. Each test case consists of four positive integers on a single line ( 1$ le$a, b, c, d$ le$2, 000, 000, 000), with single spaces for separation. The input will end with a line with four 0's.

    输出

    For each test case, output a single integer on its own line, indicating the number of steps until convergence. Output no extra spaces, and do not separate answers with blank lines.

    样例输入

    1 3 5 9
    4 3 2 1
    1 1 1 1
    0 0 0 0
    

    样例输出

    6
    4
    0
    这道题原来以为直接搞会超时的,结果没超时,而且还很充裕
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
     
    int main(){
        int a, b, c, d;
        while(scanf("%d %d %d %d", &a, &b, &c, &d) == 4 && (a + b + c + d)){
            int cnt = 0;
            while(true){
                if(a == b && b == c && c ==d) break;
                int tmp = a;
                a = abs(a - b);
                b = abs(b - c);
                c = abs(c - d);
                d = abs(d - tmp);
                cnt++;
            }
            printf("%d
    ", cnt);
        }
    }
  • 相关阅读:
    [HNOI2008]玩具装箱toy(dp+斜率优化)
    hdu 4597 Play Game(记忆化搜索)
    下载文件,ie文件名称乱码问题
    HDU 2138 How many prime numbers
    Codeforces Round #277 (Div. 2)---A. Calculating Function (规律)
    MapReduce-MulitipleOutputs实现自己定义输出到多个文件夹
    EFM8单片机与I2C外设通信
    linux杂谈(十一):LDAPserver的搭建
    Redis命令学习-string类型操作
    Java 实现单链表反序
  • 原文地址:https://www.cnblogs.com/Pretty9/p/7406638.html
Copyright © 2011-2022 走看看