NOIP 2000 进制转换 解题报告

  我也不知道为什么,负数的进制就是这么写的,反正记住就是,如果余数是负数,那就加上base,然后再用n-新余数 再除以base。

  代码如下:

#include <stdio.h>
#include <stdlib.h>
#define MAX 10000
int ans[MAX];
int start;
const char str[] = "0123456789ABCDEFGHIJKLNNOPQRSTUVWXYZ";

int main(int argc, char **argv)
{
	int i;
	int n, base;
	int r;
	while(scanf("%d%d", &n, &base) == 2){
		printf("%d=", n);
		do{
			r = n % base;
			if(r < 0){
				r -= base;
			}
			ans[MAX - 1 - start] = r;
			start++;
			n = (n - r) / base;
		}while(n != 0);
		for(i = start; i > 0; i--){
			printf("%c", str[ans[MAX - i]]);
		}
		printf("(base%d)\n", base);
	}
	return 0;
}

  

posted @ 2011-08-04 17:27  zqynux  阅读(1094)  评论(0编辑  收藏  举报