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:
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
a, b, c, d
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) == && (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!(简单题)的更多相关文章
- HDU 1753 大明A+B(字符串模拟,简单题)
简单题,但要考虑一些细节: 前导0不要,后导0不要,小数长度不一样时,有进位时,逆置处理输出 然后处理起来就比较麻烦了. 题目链接 我的代码纯模拟,把小数点前后分开来处理,写的很繁杂,纯当纪念——可怜 ...
- BZOJ 2683: 简单题
2683: 简单题 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 913 Solved: 379[Submit][Status][Discuss] ...
- 【BZOJ-1176&2683】Mokia&简单题 CDQ分治
1176: [Balkan2007]Mokia Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1854 Solved: 821[Submit][St ...
- Bzoj4066 简单题
Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 2185 Solved: 581 Description 你有一个N*N的棋盘,每个格子内有一个整数,初 ...
- Bzoj2683 简单题
Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1071 Solved: 428 Description 你有一个N*N的棋盘,每个格子内有一个整数, ...
- 这样leetcode简单题都更完了
这样leetcode简单题都更完了,作为水题王的我开始要更新leetcode中等题和难题了,有些挖了很久的坑也将在在这个阶段一一揭晓,接下来的算法性更强,我就要开始分专题更新题目,而不是再以我的A题顺 ...
- [BZOJ2683][BZOJ4066]简单题
[BZOJ2683][BZOJ4066]简单题 试题描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x ...
- 团体程序设计天梯赛-练习集L1-014. 简单题
L1-014. 简单题 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 这次真的没骗你 —— 这道超级简单的题目没有任何输入. ...
- bzoj 4066: 简单题 kd-tree
4066: 简单题 Time Limit: 50 Sec Memory Limit: 20 MBSubmit: 234 Solved: 82[Submit][Status][Discuss] De ...
随机推荐
- 8.django单独执行文件
import os import django # 导入django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'noju.settings')d ...
- sql len函数 CASE WHEN
SELECT LEN('SQL Server LEN') length, LEN('SQL Server LEN ') length_with_trailing_blanks; SELECT (CAS ...
- C++ - C++简介
一.C与C++ 一般来说,计算机要处理两个概念--算法和数据.C在面世时是过程性的语言,过程性的语言指的是程序的过程较比其他语言,会更加的有序可读(清晰性和可读性).因为它把程序分解成各个分支,并执行 ...
- #417 Div2 Problem C Sagheer and Nubian Market (二分 && std::accumulate)
题目链接 : http://codeforces.com/problemset/problem/812/C 题意 : 给你 n 件物品和你拥有的钱 S, 接下来给出这 n 件物品的价格, 这些物品的价 ...
- 2018百度之星初赛A轮 度度熊学队列
注意:刚开始用数组存deque<int> qa[MAX]会爆内存 需要改用map<int, deque<int> > qa优化 不明觉厉 #include<b ...
- c++ STL map 用法
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...
- JMS学习五(ActiveMQ的本地事务)
1.ActiveMQ的本地事务 在一个JMS客户端,可以使用本地事务来组合消息的发送和接收.JMS Session接口提供了commit和rollback方法.事务提交意味着生产的所有消息被发送,消费 ...
- 关于tomcat的server.xml里host节点配置的一些说明
其实对这个一直搞不清楚,后来公司的网站遭遇与IIS进行整合,然后上面有几个应用需要进行配置,所以对tomcat的server.xml进行了一些深入的了解 Host的节点主要是起一个对域名解析到那个虚拟 ...
- ES6 暂时性死区
在ES6中,声明变量新增了两个关键字:let命令和const命令 如果在区块中存在let或者const命令时,任何变量都必须在声明之前使用,无论是区块外部的全局变量或者是区块内部的变量: /* 区块外 ...
- leetcode-mid-Linked list- 200. Number of Islands¶
mycode 57.92% class Solution(object): def numIslands(self, grid): """ :type grid: Li ...