首页 > 学院 > 开发设计 > 正文

c语言之辗转相除法实现十进制转换为八进制

2019-11-10 17:10:24
字体:
来源:转载
供稿:网友
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <string.h>#include <stdlib.h>void Convert_8_001(int n){#define LEN sizeof(unsigned int)* 4	char result[LEN + 1] = { 0 };	int mod;	char* p = result;	for (int i = LEN - 1; i >= 0; --i)	{		mod = n % 8;		result[i] = mod + '0';		n /= 8;		if (n == 0)		{			p = p + i;			break;		}	}	PRintf("%s/n", p);}void Convert_8(int n){#define LEN sizeof(unsigned int)* 4	char result[LEN + 1] = { 0 };	char* p = result;	int count = 0;	while (n)	{		int mod = n % 8;		result[LEN - count-1] = mod + '0';		count++;		n = n / 8;	}	//移动指针指向数组后面的8进制。	p =p+ LEN-count;	printf("%s/n", p);}//输入一个整数,将其转换成八进制数输出。//c语言之辗转相除法实现十进制转换为八进制int main(){	unsigned int n;	while (scanf("%d", &n) != EOF)	{		Convert_8(n);	}	return 0;}

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <string.h>#include <stdlib.h> int main(){    long int n;    while (scanf("%d", &n) != EOF)    {        if (n == 0)        {            printf("0/n");            continue;        }        char result[12] = { 0 };        while (n)        {            int m = n % 8;             char t[2] = {0};            sprintf(t, "%d", m);            strcat(result, t);            n= n / 8;        }        //字符串逆序        int len = strlen(result);        for (int i = 0; i < len/2; i++)        {            char tmp=result[i];            result[i] = result[len-i -1];            result[len-i-1] = tmp;        }        //strcpy(result,_strrev(result));//逆序        printf("%s/n", result);             }    return 0;}添加笔记


上一篇:NYOJ 773 开方数

下一篇:hdu1045【dfs】

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表