1,编程验证角谷猜想#include "iostream.h" int main(){ int num,temp; cin>>num; while (num!=1) { if (num%2==0) { num=num/2; } else { num=num*3+1; } if (num==1) { cout<<"Right!"<private sub command1_click()dim n as integern = inputbox("请输入n:")doif n mod 2 = 0 thencall ous(n)elsecall jis(n)end ifloop until n <= 1print nend subprivate sub ous(m as integer)m = m / 2end subprivate sub jis(m as integer)m = m * 3 + 1end sub
2,用VB编程验证角谷猜想Private Sub Command1_Click()Dim n As Integern = InputBox("请输入n:")DoIf n Mod 2 = 0 ThenCall ous(n)ElseCall jis(n)End IfLoop Until n <= 1Print nEnd SubPrivate Sub ous(m As Integer)m = m / 2End SubPrivate Sub jis(m As Integer)m = m * 3 + 1End Sub<a href="http://wenwen.soso.com/z/urlalertpage.e?sp=shttp%3a%2f%2fzhidao.baidu.com%2fquestion%2f38152608.html%3fsi%3d1" target="_blank">http://wenwen.sogou.com/z/q710635483.htm?si=1</a> 这里有答案饿【编程实现谷角猜想,编程验证角谷猜想】
3,java程序 谷角猜想import java.io.*; public class gujiaopublic static void main(String[] args) throws IOExceptionBufferedReader br = new BufferedReader(new InputStreamReader(System.in));System.out.println("请输入任意自然数");int n =Integer.parseInt(br.readLine());for (int i = 0; i<=n; i++ )if ( n%2 == 0)n = n/2;System.out.print(n);}if ( n%2 != 0)n = n*3;n = n-1;System.out.print(n);}} }}不知道是不是你要的题目要求不是若是奇数,乘3加1,若是偶数/2,你的程序怎么反一反?。坑Ω檬莄%2==1吧!还有楼上的程序也不好啊,如果一上来输入1就直接返回1了,干吗还要经过(num%2==1)?import java.io.*; public class jiegupublic static void main(string args[]) throws ioexceptionint c; c=(int)system.in.read(); system.out.print(dgui(c)); } static int dgui(int c)if(c==1) return 1; else if (c%2==0)return dgui(c/2);else return 3*c+1;} } }
4,编程求助C语言怎么用递归方法解决角谷猜想你的程序这样改#include "stdio.h"int F(int a) if (a==1) return 1; if (a%2) return 1+F(3*a+1); else return 1+F(a/2);}void main() int a=0; scanf("%d",a); printf("%d",F(a));}有的数字递归次数很长导致堆栈溢出我是这样算的,可以一次算出1~n的循环步数有到ProjectEuler题目就是这个#include#include using namespace std; #define N 100 int a[N+1]=; int f(int n); void main() { int i=1; a[1]=1; for(i=1;i<=N;i++) a[i]=f(i); for(i=1;i<=N;i++) cout<<<" "<< } int f(int n) { if(n<=N&&a[n]) return a[n]; else { if(n%2) return 1+f(n*3+1); else return 1+f(n/2); } }if (a%2==0) return(F(3*a+1));if (a%2) return(F(a/2));这两个反了,改为if (a%2==0) return(F(a/2));if (a%2) return(F(3*a+1));#include#include int F(int a) { if (a==1) return 0; if (a%2) return 1+F(3*a+1); else return 1+F(a/2); } 楼主你这一句scanf("%d",a)忘加&了; int main() { int a=0; printf("请输入数字:"); scanf("%d",&a); printf("运算步数为:%d\n",F(a)); system("pause"); return 0; }LS 的正确 。。不过个人认为void F(int a) if (a==1)return ; if(a%2==1)a=a*3+1; elsea=a/2; printf("%d\n",a); F(a);}递归是吧 , 这样就可以了#include "stdio.h"static int k=0;void F(int a) if (a==1)return ; k++; if(a%2==1)a=a*3+1; elsea=a/2; printf("%d\n",a); F(a);}void main() int a=0; printf("input your number\n"); scanf("%d",&a); F(a); printf("the function totally calculated %d times",k);}
推荐阅读
- ipad儿童游戏推荐,ipad有什么适合小孩子玩的游戏
- 手机怎么控制照片的大小,手机拍照大小是怎样控制的
- python手机软件,手机软件下载
- 苹果手机如何改成中文版本,苹果手机设置从英文怎么换成中文
- 加工中心仿真软件,加工中心仿真软件怎样下载
- 王者荣耀国服版手机下载,王者DX699可以下载手机QQ
- 迷你编程教学3D打印,如何用3D打印制作皮卡丘 神奇宝贝 宠物小
- pc 控制安卓,如何使用电脑控制手机?答案1
- kotlin web,java 页面怎么跳 kotlin页面