角谷猜想递归编程,编程验证角谷猜想

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 , pascal语言编程角谷猜测这个好像不是纯角谷猜想吧 , 麻烦楼主把题目说清楚点儿,我这儿有个角谷猜想的递归程序,希望对你有用处:varn,t:longint;procedure jg(n:longint);beginif n>1 then if n mod 2 = 0 thenbeginjg(n div 2);t:=t+1;endelsebeginjg(n * 3 + 1);t:=t+1;end;end;begint:=0;readln(n);jg(n);writeln(t);end.角谷猜想是吧?varn,i:integer;beginrepeatif i mod 2 =0 then begin i:=i div 2;write(i div 2) endelse begin i:=i*3+1; write (i*3+1); end;until i=1;end.你的语法有点错误我帮你改好了这个你试一下应该是可以的
3,java程序 谷角猜想题目要求不是若是奇数,乘3加1,若是偶数/2,你的程序怎么反一反?。坑Ω檬莄%2==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;} } }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);}} }}不知道是不是你要的【角谷猜想递归编程,编程验证角谷猜想】
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); } }递归是吧 , 这样就可以了#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);}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);}

    推荐阅读