Skip to content

Instantly share code, notes, and snippets.

@zetachang
Created December 20, 2011 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zetachang/1501633 to your computer and use it in GitHub Desktop.
Save zetachang/1501633 to your computer and use it in GitHub Desktop.
#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