Created
December 20, 2011 13:54
-
-
Save zetachang/1501633 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
void three_n_plus_one(int n,int *count,int a[]); | |
int main(void) | |
{ | |
int count=0,input[3],a[100],i,j; | |
printf("請輸入三個值:"); | |
scanf("%d %d %d",&input[0],&input[1],&input[2]); | |
for(i=0;i<3;i++) | |
{ | |
three_n_plus_one(input[i],&count,a); | |
printf("總計算次數為:%d\n",count+1); | |
printf("The proccess:"); | |
for(j=0;j<count;j++) | |
{ | |
printf("%d ",a[j]); | |
} | |
printf("1\n"); | |
} | |
scanf(" "); | |
//system("PAUSE"); | |
return 0; | |
} | |
void three_n_plus_one(int n,int *count,int a[]) | |
{ | |
printf("您輸入的值為:%d\n",n); | |
a[0] = n; | |
int cnt = 0; | |
while(n!=1) | |
{ | |
if(n%2!=0) | |
{ | |
n=n*3+1; | |
cnt++; | |
a[cnt] = n; | |
} | |
if(n%2==0) | |
{ | |
n=n/2; | |
cnt++; | |
a[cnt] = n; | |
} | |
} | |
*count = cnt; | |
//printf("計算的過程為:%d\n",a[]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment