本文主要是介绍hdu1279,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
验证角谷猜想
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5629 Accepted Submission(s): 2936
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1279
Problem Description
数论中有许多猜想尚未解决,其中有一个被称为“角谷猜想”的问题,该问题在五、六十年代的美国多个著名高校中曾风行一时,这个问题是这样描述的:任何一个大于一的自然数,如果是奇数,则乘以三再加一;如果是偶数,则除以二;得出的结果继续按照前面的规则进行运算,最后必定得到一。现在请你编写一个程序验证他的正确性。
Input
本题有多个测试数据组,第一行为测试数据组数N,接着是N行的正整数。
Output
输出验证“角谷猜想”过程中的奇数,最后得到的1不用输出;每个测试题输出一行;每行中只有两个输出之间才能有一个空格;如果没有这样的输出,则输出:No number can be output !。
Sample Input
4 5 9 16 11
Sample Output
5 9 7 11 17 13 5 No number can be output ! 11 17 13 5解题思路:注意下输出格式就行,没什么多说的,简单题。完整代码:#include <functional> #include <algorithm> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <numeric> #include <cstring> #include <climits> #include <cassert> #include <complex> #include <cstdio> #include <string> #include <vector> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <list> #include <set> #include <map> using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL; typedef double DB; typedef unsigned uint; typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7; const int INF = 0x3f3f3f3f; const LL INFF = 0x3f3f3f3f3f3f3f3fLL; const DB EPS = 1e-9; const DB OO = 1e20; const DB PI = acos(-1.0); //M_PI;bool old(int key) {if(key % 2 != 0)return true;return false; }void solve(int n) {int flag = 0;while(n > 1){if(old(n)){if(flag == 0){flag = 1;printf("%d",n);}elseprintf(" %d",n);n = n * 3 + 1;}elsen /= 2;}if(flag == 0)printf("No number can be output !"); }int main() {#ifdef DoubleQfreopen("in.txt","r",stdin);#endifint T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);solve(n);printf("\n");} }
这篇关于hdu1279的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!