HDU The 3n + 1 problem(简单题 有坑)

最大的坑就在输入的两个变量的大小并没有规定前面大于后面。。。

 1 #include <stdio.h>
 2
 3 //普通遍历
 4 int Program(int n)
 5 {
 6     int num = 0;
 7     while (1)
 8     {
 9         num++;
10         if (n == 1)
11         {
12             break;
13         }
14         else if (n % 2 != 0)
15         {
16             n = 3 * n + 1;
17         }
18         else
19         {
20             n /= 2;
21         }
22     }
23     return num;
24 }
25
26
27
28 int main(void)
29 {
30     int m, n;
31     while (scanf("%d%d", &m, &n) != EOF)
32     {
33         int x = m;
34         int y = n;
35         if (m > n)
36         {
37             int tmp = m;
38             m = n;
39             n = tmp;
40         }
41         int max = 0;
42         for (int i = m; i <= n; i++)
43         {
44             int res = Program(i);
45             if (res > max)
46             {
47                 max = res;
48             }
49         }
50         printf("%d %d %d\n", x, y, max);
51     }
52     return 0;
53 }

下面进行一点时间上的优化,以空间换时间。。。

 1 #include <stdio.h>
 2
 3 #define MAX 1000010
 4 static int a[MAX] = {0};
 5
 6 //记忆化遍历     降低运行时间      MAX越大能够记录的数值越多,后台数据比较多的情况下运行速度相对也会越快
 7 int ProgramPlus(int n)
 8 {
 9     int num = 0;
10     int tmp = n;
11     while (1)
12     {
13         //因为某些数可能会持续执行n * 3 + 1操作,会超出MAX界限导致失败
14         //所以进行n < MAX判断
15         if (n < MAX && a[n] != 0)
16         {
17             num += a[n];
18             break;
19         }
20         num++;
21         if (n % 2)
22         {
23             n = 3 * n + 1;
24         }
25         else
26         {
27             n /= 2;
28         }
29     }
30     a[tmp] = num;
31     return num;
32 }
33
34 int main(void)
35 {
36     a[1] = 1;
37     int m, n;
38     while (scanf("%d%d", &m, &n) != EOF)
39     {
40         int x = m;
41         int y = n;
42         if (m > n)
43         {
44             int tmp = m;
45             m = n;
46             n = tmp;
47         }
48         int max = 0;
49         for (int i = m; i <= n; i++)
50         {
51             int res = ProgramPlus(i);
52             if (res > max)
53             {
54                 max = res;
55             }
56         }
57         printf("%d %d %d\n", x, y, max);
58     }
59     return 0;
60 }

原文地址:https://www.cnblogs.com/changeFeng/p/9367795.html

时间: 2024-08-13 19:35:18

HDU The 3n + 1 problem(简单题 有坑)的相关文章

hdu 4970 Killing Monsters(简单题) 2014多校训练第9场

Killing Monsters                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Kingdom Rush is a popular TD game, in which you should b

[BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

Task schedule Problem Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务. 有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行. 机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求. Input 输入的第一行包含一个整数T, 表示一共有T组测试数据. 对于每组测试数据: 第一行是两个数字n, m,表示工作表里面有n个任务,

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

hdu 4405(概率dp简单题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1535    Accepted Submission(s): 1050 Problem Description Hzz loves aeroplane

HDU 6182 A Math Problem 水题

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6182 题目描述: 输入N, 输出满足k^k <= N 的 k的个数 解题思路: 水水更健康 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iterator> #in

HDU 1426 Sudoku Killer DFS 简单题

给出一个数独的一部分,然后然后要我们填完整这个数独. Input 本题包含多组测试,每组之间由一个空行隔开.每组测试会给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开.其中1-9代表该位置的已经填好的数,问号(?)表示需要你填的数. Output 对于每组测试,请输出它的解,同一行相邻的两个数用一个空格分开.两组解之间要一个空行.对于每组测试数据保证它有且只有一个解. Sample Input 7 1 2 ? 6 ? 3 5 8 ? 6 5 2 ? 7 1 ? 4 ? ? 8 5 1

HDU2102 A计划 BFS 简单题 有坑点 好题

Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老.年迈的国王正是心急如焚,告招天下勇士来拯救公主.不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出.现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示.骑士们一进入时空传输机就会被转到另一层的相对位置

HDU 1032 The 3n + 1 problem (这个题必须写博客)

The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22148    Accepted Submission(s): 8276 Problem Description Problems in Computer Science are often classified as belonging to a c

hdu 1032 The 3n + 1 problem (打表)

The 3n + 1 problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26353    Accepted Submission(s): 9784 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 Problem Description Problems in Comp