算法竞赛入门经典第二版P24-3n+1问题

探讨3n+1猜想的数学问题,通过编程实现对大于1的自然数进行变换,直至变为1的过程,记录变换次数。面对大数值运算,优化数据类型防止溢出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

猜想,对于任意大于1的自然数n,若n为奇数,则将n变为3n+1,否则变为n的一半,经过若干次这样的变换,一定会使n变为1。输入n,输出变化的次数,n <= 10^9

得到的提示:
  • 当需要统计某种事物的个数时,可以用一个变量来充当计数器。
  • 在无法找出错误时,可以使用输出中间结果的方式来找错
//version1.0
#include<stdio.h>
//当需要统计某种事物的个数时,可以用一个变量来充当计数器~
int main()
{
    int n,count = 0;//定义整形变量count的同时初始化为0
    //count的作用是计数器,由于最后输出的是变换的次数,所以需要一个变量来完成计数
    //所以count的作用是一个计算while总共运行了多少次的变量
    scanf("%d",&n);
    while(n>1)
    {
        if(n % 2 == 1)n = n *3+1;
        else n /= 2;
        printf("%d\n",n);//输出中间的结果
        count ++;
    }
    printf("%d\n",count);
    }

在version1.0中输入接近10^9的数对于IDE中的int会溢出

#version2.0
#include<stdio.h>

int main()
{
    int n1, count = 0;
    scanf("%d",&n1);
    long long n = n1;
    while(n>1)
    {
        if(n % 2 == 1)n = n *3+1;
        else n /= 2;
        printf("%d\n",n);//在循环中放一个输出是为了查找错误
        count ++;
    }
    printf("%d\n",count);

}

在这个例子中,int的上界太小了和10^9相比,所以需要更大的范围来控制n不溢出。
所以要考虑数据类型的取值
int整数类型在C99中只规定了至少是16位,并没有规定具体数值
在竞赛中int都是32位整数,范围是-2147483648~2147283647
所以用long long 类型的n才能不溢出

在上一题Point2D和Point3D类的基础上,新建一个TestPointV2类,在TestPointV2类的main()方法中添加如下语句。 Scanner sc = new Scanner(System.in); System.out.println("Please enter the coordinates of p23:"); double p23x = sc.nextDouble(); double p23y = sc.nextDouble(); Point2D p23 = new Point2D(p23x, p23y); System.out.println("Please enter the coordinates of p31:"); double p31x = sc.nextDouble(); double p31y = sc.nextDouble(); double p31z = sc.nextDouble(); Point3D p33 = new Point3D(p31x, p31y, p31z); System.out.println("Please enter the coordinates of p24:"); double p24x = sc.nextDouble(); double p24y = sc.nextDouble(); double p24z = sc.nextDouble(); sc.close(); // The reference of the parent class refers to the object of the subclass. Point2D p24 = new Point3D(p24x, p24y, p24z); System.out.println("Does " + p23 + " coincide with " + p33 + "? -- "+ p23.equals(p33)); System.out.println("Does " + p33 + " coincide with " + p23 + "? -- "+ p33.equals(p23)); System.out.println("Does " + p33 + " coincide with " + p24 + "? -- "+ p33.equals(p24)); System.out.println("Does " + p24 + " coincide with " + p33 + "? -- "+ p24.equals(p33)); System.out.println("Does " + p23 + " coincide with " + p24 + "? -- "+ p23.equals(p24)); System.out.println("Does " + p24 + " coincide with " + p23 + "? -- "+ p24.equals(p23)); 假设引用变量p23、p33和p24所指点对象的坐标依次为(0, 0),(0, 0, 5),(0, 0, 5)。从键盘输入这三个点的坐标值,上述语句的运行结果如下: Please enter the coordinates of p23: 0 0 Please enter the coordinates of p31: 0 0 5 Please enter the coordinates of p24: 0 0 5 Does (0.0, 0.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0) coincide with (0.0, 0.0, 5.0)? -- true Does (0.0, 0.0, 5.0) coincide with (0.0, 0.0)? -- true 该结果显然不符合事实,请分析原因并改进Point2D类的代码,使得上述TestPointV2类的代码能够得到正确的运行结果。
05-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值