1559: Vive la Difference!

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

题目描述

Take any four positive integers: abcd. 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 bb with cc 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 abc 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 ( 1abcd2, 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) == && (a + b + c + d)){
int cnt = ;
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\n", cnt);
}
}

HDUSTOJ-1559 Vive la Difference!(简单题)的更多相关文章

  1. HDU 1753 大明A+B(字符串模拟,简单题)

    简单题,但要考虑一些细节: 前导0不要,后导0不要,小数长度不一样时,有进位时,逆置处理输出 然后处理起来就比较麻烦了. 题目链接 我的代码纯模拟,把小数点前后分开来处理,写的很繁杂,纯当纪念——可怜 ...

  2. BZOJ 2683: 简单题

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 913  Solved: 379[Submit][Status][Discuss] ...

  3. 【BZOJ-1176&2683】Mokia&简单题 CDQ分治

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1854  Solved: 821[Submit][St ...

  4. Bzoj4066 简单题

    Time Limit: 50 Sec  Memory Limit: 20 MBSubmit: 2185  Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...

  5. Bzoj2683 简单题

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1071  Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...

  6. 这样leetcode简单题都更完了

    这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...

  7. [BZOJ2683][BZOJ4066]简单题

    [BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...

  8. 团体程序设计天梯赛-练习集L1-014. 简单题

    L1-014. 简单题 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 这次真的没骗你 —— 这道超级简单的题目没有任何输入. ...

  9. bzoj 4066: 简单题 kd-tree

    4066: 简单题 Time Limit: 50 Sec  Memory Limit: 20 MBSubmit: 234  Solved: 82[Submit][Status][Discuss] De ...

随机推荐

  1. Django orm self 自关联表

    自关联模型 自关联模型就是表中的某一列,关联了这个表的另外一列.最典型的自关联模型就是地区表.省市县都在一张表里面.省的pid为null,市的pid为省的pid,县的pid为市的ID. class A ...

  2. delphi回调函数高级应用

    用法一 Type TProc=procedure() of object; procedure Callback(proc:TProc); begin ... end; procedure mypro ...

  3. js 获取 URL的参数 session

    <script type="text/javascript"> window.onload = function () { //var content = getPar ...

  4. java总结2

    1,对象数组,必须指定了数组长度,长度是固定的 2,除了ArrayList<E>以外,类赋值给变量,只有string类拿到的是值,其他类拿到的都是类的地址值, ArrayList<E ...

  5. JUnit——运行多个测试方法

    TDD=Test Drive Development   在实际应用中作用特别大,因为我们会定义很多的类和方法,也会存在很多的更新操作,这样如果用main函数进行测试,则会异常麻烦. 而如果用这种多个 ...

  6. VTK 编译过程中出现的hdf5长度(I64)错误解决办法

    最近在使用vtk和cuda做大规模图像处理方面的问题研究,在编译vtk的过程中发现第三方库hdf5不能够解决I64长度的探测识别问题.为了节约大家的时间,现在把我经过实践得到的解决方案共享出来,这里要 ...

  7. (51)LINUX应用编程和网络编程之六Linux高级IO

    3.6.1.非阻塞IO 3.6.1.1.阻塞与非阻塞 阻塞:阻塞具有很多优势(是linux系统的默认设置),单路IO的时候使用阻塞式IO没有降低CPU的性能 补充:阻塞/非阻塞, 它们是程序在等待消息 ...

  8. JMS学习十一(ActiveMQ Consumer高级特性之独有消费者(Exclusive Consumer))

    一.简介 Queue中的消息是按照顺序被分发到consumers的.然而,当你有多个consumers同时从相同的queue中提取消息时, 你将失去这个保证.因为这些消息是被多个线程并发的处理.有的时 ...

  9. jdk环境配置(windows版)

    JAVA_HOME C:\Program Files\Java\jdk1.7.0_80 Path %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; CLASSPATH .;%J ...

  10. Python的 counter内置函数,统计文本中的单词数量

    counter是 colletions内的一个类 可以理解为一个简单的计数 import collections str1=['a','a','b','d'] m=collections.Counte ...