题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/0337e32b1e5543a19fa380e36d9343d7
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define len 10001 #define maxint 1<<31-1 int chartoint(char c){ int a = c-'0'; if(a>=0&&a<=9){ return a; } return -1; } char inttochar(char c){ return c+'0'; } void newdiv(char str[],int x){ int remain = 0; char temp[len]; for(int i = 0;i<len;i++){ temp[i] = '\0'; } for(int i = 0;i<strlen(str)-1;i++){ int a = chartoint(str[i]); int sum = a+remain*10; temp[i] = inttochar(sum/x); remain = sum%x; } int i = 0; while(temp[i]=='0'){ i++; } int j; for(j = 0;temp[i+j]!='\0';j++){ str[j] = temp[i+j]; } str[j] = '\n'; str[j+1] = '\0'; if(temp[i]=='\0'){ str[0] = '0'; str[1] = '\0'; } } int newmod(char str[],int x){ char c = str[strlen(str)-2]; int a = chartoint(c); return a%x; } int t; int stack[len]; void push(int x){ stack[t++] = x; } int pop(){ if(t==0){ return 1<<31; } return stack[--t]; } int main(){ char str[len]; while(fgets(str,sizeof(str),stdin)){ t = 0; // newdiv(str,2); // printf("%s",str); while(strcmp(str,"0")!=0){ //printf("%s",str); int a = newmod(str,2); push(a); newdiv(str,2); } while(t!=0){ printf("%d",pop()); } printf("\n"); } }