小乐乐在课上学习了二进制八进制与十六进制后,对进制转换产生了浓厚的兴趣。因为他的幸运数字是6,所以他想知道一个数表示为六进制后的结果。请你帮助他解决这个问题。
#include <stdio.h> #include<math.h> #include<time.h> #include<stdlib.h> #define JZ 6 int o(int x) //算最大次方位 { while (1) { int n = rand() % 20; if (pow(JZ, n + 1) > x && pow(JZ, n) <= x) { return n; } } } int p(int o, int x)//算最大次方位,填几 { while (1) { int n = rand() % 10; if (pow(JZ, o) * (n + 1) > x && pow(JZ, o) * (n) <= x && n!=0) { return n; } } } int main() { srand((unsigned int)time(NULL)); int n = 0; int a, b; int i = 0; scanf("%d", &n); int c = o(n); int arr[] = { 0 }; for (i = 0; i <= c; i++) { arr[i] = 0; } while (n) { a = o(n); //printf("%d ", a); b = p(a, n); // printf("%d\n", b); arr[a] = b; //数组 存放a这个次方位的值 n = n - b * pow(JZ, a); } for (i = c; i >= 0; i--) { printf("%d", arr[i]); } return 0; }
#include <stdio.h> int main() { char a[15]; int i = 0; int n = 0; scanf("%d", &n); int Hexadecimal = 6; while (n) { //余数存放到字符串中,在从后向前打印字符串 *(a + i) = n % Hexadecimal; n /= Hexadecimal; i++; } //打印余数组成的字符串,但字符串是反过来的,因为要打印为0的位置i可以为0 while (i-- && i >= 0) { printf("%d", *(a+i)); } return 0; }
#include <stdio.h> int main() { int a = 0; int six[] = {0, 1, 2, 3, 4, 5}; int aSix[10] = {0}; int i = 0; int sz = 0; scanf("%d", &a); for(i = 0; a != 0; i++) { aSix[i] = six[a % 6]; a /= 6; sz++; } for(i = sz - 1; i != -1; i--) { printf("%d", aSix[i]); } return 0; }
#include <stdio.h> void pushStack(int x, int* arr, int* Pointer) { arr[*Pointer + 1] = x; (*Pointer)++; } int popStack(int* arr, int* Pointer) { (*Pointer)--; return arr[*Pointer + 1]; } int main() { int n; scanf("%d\n", &n); int stack[20]; int Pointer = -1; while (n != 0) { pushStack(n % 6, stack, &Pointer); n /= 6; } while (Pointer >= 0) { printf("%d", popStack(stack, &Pointer)); } } //用栈或者递归都可以实现逆序输出
#include <stdio.h> #include <math.h> int main() { long long int n,output=0; int i=0; scanf("%lld", &n); while(n) { output=output+(n%6)*pow(10,i); n/=6; i++; } printf("%lld\n",output); return 0; }