1,角谷猜想 c语言#include<stdio.h>int main() int n, t, m = 0; scanf("%d", &n); t = n; printf("%d ", t); while(t !制= 1) zdif(t % 2)t = t * 3 + 1;elset /= 2;m++;printf("%d ", t); } printf("\n%d calculate %d times,the answer become to 1!\n", n, m); return 0;}
2 , 验证角谷猜想 c语言if (count!=0)printf("\b\n");加个\b退位if (count!=0)printf("\b\n"); 加个\b退位#includemain() {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?如此逆推,就能写成一棵"数论树".
3,求角谷猜想C语言程序输入一个正整数输出此整数回到1经过的步#include<stdio.h>main() int n,c=0; printf("输入一个小于等于一万的整数:"); scanf("%d",&n); if(n>10000&&n<0) printf("输入错误请重新输入!"); while(n!=1)if(n%2==0)n=n/2;elsen=n*3+1;c++; } printf("%d\n",c);}#include<stdio.h>main() int n=0; int num; doprintf("please input a integrate(num<=10000):");scanf("%d",&num); } while(num>10000); while(num!=1)if(num%2==1)num=num*3+1;elsenum=num/2;n++; } printf("n=%d\n",n);}#include<stdio.h>main() int n=0; int num; doprintf("please input a integrate(num<=10000):");scanf("%d",&num); } while(num>10000); while(num!=1)if(num%2==1)num=num*3+1;elsenum=num/2;n++; } printf("n=%d\n",n);}#includemain() int n=0; int num; doprintf("please input a integrate(num<=10000):");scanf("%d",&num); } while(num>10000); while(num【角谷猜想c 语言编程,角谷猜想 c语言】
4,c语言与角谷猜想#include<stdio.h>main() printf("请输入a的值,以便检验角谷猜想 。\na="); scanf("%d",&a); b=a; while (a!=1)else count++;}printf("%d需经过%d步才得到1.",b,count);}//如果能够逆推出,比方说 , 23的下一步是70,那么都有哪些数的下一步是70?如此逆推,就能写成一棵"数论树".代码比较简单,各语句意义见注释 。。。#include int main(int argc, char *argv[]) int i; /*定义要处理的变量*/ scanf("%d", &i); /*输入变量值*/ while(i != 1) if(i%2)/*奇数*/ printf("%d*3+1=%d\n", i, i*3+1); /*按题目要求的格式打印出来*/ i = i*3+1; /*更新变量值*/ } else /*偶数*/ printf("%d/2=%d\n", i, i/2); /*按题目要求的格式打印出来*/ i = i/2; /*更新变量值*/ } } printf("end\n"); /*最后打印end*/ return 0; }另外几个测试结果图片://希望可以帮到您#include <stdio.h>#include <stdlib.h>int main() int i; static int count=0; printf("Number:\t"); scanf("%d",&i); doif(i%2==0)count++;printf("Step %3d -> %d = %d / 2\n",count,i/2,i);i=i/2;}elsecount++;printf("Step %3d -> %d = 3 x%d + 1\n",count,3*i+1,i);i=i*3+1;} }while(i!=1); return 0;}5 , 编程求助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);}
推荐阅读
- python数据分析 招聘
- 爱思助手极速版安装,苹果手机的爱思助手上怎么安装软件
- 安卓python编辑器下载,有没有安卓版的python编译器
- 苹果14新手机怎么充电好,iphone手机怎么充电好用尽再充还是有时间就充要对电池的寿命
- g272
- 字母怎么读英语,36个英文字母怎么读
- 爱思助手极速版下载安装,爱思助手在哪里下载
- Python iPad,Python能否在 iPad 上运行
- 苹果官网ios12下载安装软件,ios1212怎么下载app