标签:
#include <stdio.h> char Stack[18]; short top = -1; short IsEmpty() { if(top == -1) return 0; return 1; } void push(char e) { Stack[++top] = e; } void pop(char* e) { *e = Stack[top--]; } int main() { int N,temp,R; char e; scanf("%d%d",&N,&R); temp = N; if(-R <= 9) { while(temp) { if(temp%R >= 0) { push(‘0‘+temp%R); temp /= R; } else{ push(‘0‘ + (temp - R*(temp/R+1))); temp = temp/R + 1; } } } else{ while(temp) { if(temp%R >=0) { if(temp%R <= 9) { push(‘0‘+temp%R); } else{//当R=-20,余数可能为10 push(‘A‘+ (temp%R-10)); } temp /= R; } else{ if(-R + temp%R <= 9)//判断差值 push(‘0‘ + (temp - R*(temp/R+1))); else push(‘A‘ + (-R + temp%R - 10)); temp = temp/R + 1; } } } printf("%d=",N); while(IsEmpty()) { pop(&e); putchar(e); } printf("(base%d)\n",R); return 0; }
标签:
原文地址:http://www.cnblogs.com/520xiuge/p/5146401.html