小乐乐在课上学习了二进制八进制与十六进制后,对进制转换产生了浓厚的兴趣。因为他的幸运数字是6,所以他想知道一个数表示为六进制后的结果。请你帮助他解决这个问题。
import java.util.Scanner ; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); StringBuilder r = new StringBuilder(); while(n>0){ r.append( n%6 ); n /= 6 ; } System.out.println( r.reverse()); } }
void modSix(int n) { if(n){ modSix(n / 6); printf("%d", n % 6); } } int main() { int n = 0; scanf("%d",&n); modSix(n); return 0; }
#include<stdio.h> int main() { int n,i=0,j; scanf("%d",&n); int arr[100]; while(n/6!=0) { arr[i]=n%6; n=n/6; i++; } arr[i]=n; for(j=i;j>=0;j--) printf("%d",arr[j]); return 0; }
int main() { int a = 0; int i = 0; int arr[10] = {0}; scanf("%d", &a); while (a != 0) //,循环的条件是这个数被除尽为止,即本数为零 { arr[i] = a % 6; //短除法将一个数除以六取余,将余数放在数组中 a = (a / 6); i++; } for (int j = i-1; j >= 0; j--) //因为短除法倒着取余,所以循环倒着打印每一位 { printf("%d", arr[j]); } return 0; }
#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; }