Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RisingInIris2017/790ffcd7c6fb830f5e33d92055dda36a to your computer and use it in GitHub Desktop.
Save RisingInIris2017/790ffcd7c6fb830f5e33d92055dda36a to your computer and use it in GitHub Desktop.
十进制转八进制
#include<stdio.h>
#include<stdlib.h>
int main()
{
int input;
scanf("%d",&input);//get the input number
int a[10];//get ready for the table of 8*n
int b[10];//get ready for the turth table
int i=0;//take the possible max of n
int oct=1;
// for(i=0;oct<input;i++)
// {
//
// oct=oct*8;
// printf("%d\n",oct);
// }
// a=(int*)malloc(sizeof(int*)*i);
// b=(int*)malloc(sizeof(int*)*i);
a[0]=1;
//printf("ok");
//printf("%d",i);
for(int k=1;k<=9;k++)
{
//printf("ok");
a[k]=8*a[k-1];//get the table
}
//printf("ok");
for(int k=9;k>=0;k--)
{
b[k]=input/a[k];
input=input%a[k];
//printf("%d\n",input);
}
for(int k=9;k>=0;k--)
{
printf("%d",b[k]);
}
return 0;
}
//速度较快
#include<stdio.h>
#define MAXNUM 10
int main()
{
int shengyu,bajinzhi[MAXNUM],count=0,jishu;
printf("shuru shijinzhi shuzi\n");
scanf("%d",&shengyu);
while(shengyu!=0)
{
bajinzhi[count]=shengyu%8;
printf("bajinzhi[%d]=%d\n",count,bajinzhi[count]);
shengyu=shengyu/8;
count++;
}
for(jishu=count-1;jishu>=0;jishu--)
printf("%d",bajinzhi[jishu]);
return 0;
}
//实现简单,但是速度略慢
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment