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);
}
}

最新文章

  1. 【Oracle】Oracle 序列步长问题
  2. Best Time to Buy and Sell Stock with Cooldown
  3. iOS应用内付费(IAP)开发步骤列表
  4. Mybatis 示例之 foreach
  5. Quartz Cron 表达式
  6. SQLServer: 解决&ldquo;错误15023:当前数据库中已存在用户或角色
  7. Windows Server 2008 网站访问PHP响应慢的解决方法
  8. Servlet实现后台分页查询
  9. 关于NPOI导入的时候有时出现乱码解决办法
  10. sql面试学到新内容
  11. Spring Jdbc 框架整合的第一天
  12. leetcode 421.Maximum XOR of Two Numbers in an Array
  13. win10 KMS命令激活步骤&lt;转&gt;
  14. Spring Boot 配置 IDEA&amp;DevTools 自编译重启
  15. jQuery 各类判断函数汇总
  16. Axiom3D:Ogre地形组件代码解析
  17. POJ3104 Drying 2017-05-09 23:33 41人阅读 评论(0) 收藏
  18. 热血沙城-3.2移植-古月-cocos2dx源码
  19. 葡萄城报表介绍:B/S 报表软件
  20. Groovy/Spock 测试导论

热门文章

  1. nowcoder 79F 小H和圣诞树 换根 DP + 根号分治
  2. 【bzoj2763】[JLOI2011]飞行路线
  3. 【C++】char* 和 char[] 区别的理解
  4. React 项目 ant design 的 CheckboxGroup 验证
  5. Android 5种Toast特效
  6. week4 作业
  7. SWPU2019 伟大的侦探
  8. 前端iPhone X适配总结
  9. @清晰掉 GNU C __attribute__
  10. MVC简易分页(Razor)