当前位置:首页 » 编程语言 » 验证角谷定理c语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

验证角谷定理c语言

发布时间: 2022-01-23 19:23:49

c语言角谷定理求次数

下面的程序先从键盘上给我一个整数X。然后如果它是奇数就乘以3+1。否则就除以2。并且同时记录次数,当X的值变为E的时候,停止循环输出处理的次数。
#include <stdio.h>
int main()
{ int n=0,x;
scanf("%d",&x);
while(x!=1)
{if(x%2)x=3*x+1;
else x/=2;
n++;
}
printf("%d\n",n);
return 0;
}

❷ c++编程验证“角谷猜想”

#include<stdio.h>
int
main()
{
int
n,count=0;
printf("Please
enter
number:");
scanf("%d",&n);
/*输入任一整数*/
do{
if(n%2)
{
n=n*3+1;
/*若为奇数,n乘3加1*/
printf("[%d]:%d*3+1=%d\n",++count,(n-1)/3,n);
}
else
{
n/=2;
/*若为偶数n除以2*/
printf("[%d]:
%d/2=%d\n",++count,2*n,n);
}
}while(n!=1);
/*n不等于1则继续以上过程*/
}
以上是c语言的,再给你弄个c++的
#include<iostream>
using
namespace
std;
int
main()
{
int
n,count=0;
cout<<"Please
enter
number:";
cin>>n;
/*输入任一整数*/
do{
if(n%2)
{
n=n*3+1;
/*若为奇数,n乘3加1*/
++count;
cout<<"["<<count<<"]:"<<(n-1)/3<<"*3+1="<<n<<endl;
}
else
{
n/=2;
/*若为偶数n除以2*/
++count;
cout<<"["<<count<<"]:"<<2*n<<"/2="<<n<<endl;
}
}while(n!=1);
/*n不等于1则继续以上过程*/
}

❸ C语言作业,角谷问题

#include <stdio.h>

int main()

{int x,x1;

scanf("%d",&x);

while(x!=1)

{if(x%2)

{x1=3*x1+1;

printf("%d*3+1=%d ",x,x1);

}

else

{x1=x/2;

printf("%d/2=%d ",x,x1);

}

x=x1;

}

return 0;

}

❹ C语言 角谷猜想 要求验证角谷猜想,从键盘上输入一个自然数n,输出所有n到1经历的所有数。

没用过C语言 用PYTHON给你写一个 你可以照着改改

num=6
print(num,end='')
while(num!=1):
if(num%2)==0:#偶数
num=num//2
else:#奇数
num=num*3+1
print(num,end='')#63105168421

❺ 角谷猜想 c语言

#include<stdio.h>
int main()
{
int n, t, m = 0;
scanf("%d", &n);
t = n;
printf("%d ", t);
while(t != 1)
{
if(t % 2)
t = t * 3 + 1;
else
t /= 2;
m++;
printf("%d ", t);
}
printf("\n%d calculate %d times,the answer become to 1!\n", n, m);
return 0;
}

❻ c语言与角谷猜想

#include<stdio.h>
main()
{int
a,b,count;
printf("请输入a的值,以便检验角谷猜想。\na=");
scanf("%d",&a);
b=a;
while
(a!=1)
{if
(a>1&&a%2==0)
{a=a/2
;printf
("→%d\n",a);}
else
{a=3*a+1;printf("→%d\n",a);}
count++;}printf("%d需经过%d步才得到1.",b,count);}
//如果能够逆推出,比方说,23的下一步是70,那么都有哪些数的下一步是70?如此逆推,就能写成一棵"数论树".

❼ 用c语言程序验证角合定理

#include "stdio.h"
proveJiaoGu(int n)
{
int count=1;
while(n!=1 && count<=1000){ /*阈值设为1000*/
if(n%2==0) /*n为偶数*/
{
printf("%d/2=%d\n",n,n/2);
n = n/2;
}
else
{
printf("%d*3+1=%d\n",n,n*3+1); /*n为奇数*/
n=n*3+1;
}
count++;
}
if(count<1000 && n==1)
printf("This natural number is according to JiaoGu Guess\n");
}
main()
{
int n;
printf("Please input a number to verify\n");
scanf("%d",&n);
printf("-------- Step of Verification---------\n");
proveJiaoGu(n);
getche();
}

❽ 验证角谷猜想 c语言

if (count!=0)
printf("\b\n");加个\b退位

❾ 怎样用C语言验证角谷定理要具体过程。

#include "stdio.h"
proveJiaoGu(int n)
{
int count=1;
while(n!=1 && count<=1000){ /*阈值设为1000*/
if(n%2==0) /*n为偶数*/
{
printf("%d/2=%d\n",n,n/2);
n = n/2;
}
else
{
printf("%d*3+1=%d\n",n,n*3+1); /*n为奇数*/
n=n*3+1;
}
count++;
}
if(count<1000 && n==1)
printf("This natural number is according to JiaoGu Guess\n");
}
main()
{
int n;
printf("Please input a number to verify\n");
scanf("%d",&n);
printf("-------- Step of Verification---------\n");
proveJiaoGu(n);
getche();
}

❿ 角谷猜想,C语言,输出过程

代码比较简单,各语句意义见注释。。。

#include<stdio.h>

intmain(intargc,char*argv[])

{

inti;/*定义要处理的变量*/

scanf("%d",&i);/*输入变量值*/

while(i!=1)

{

if(i%2)/*奇数*/

{

printf("%d*3+1=%d ",i,i*3+1);/*按题目要求的格式打印出来*/

i=i*3+1;/*更新变量值*/

}

else/*偶数*/

{

printf("%d/2=%d ",i,i/2);/*按题目要求的格式打印出来*/

i=i/2;/*更新变量值*/

}

}

printf("END ");/*最后打印END*/

return0;

}

另外几个测试结果图片: