标签:blog io ar for sp 2014 log bs amp
习题3-6
输入基数b(2<=b<=10)和正整数n(十进制),输出n的b进制表示。
#include <stdio.h> #include <stdlib.h> #define MAXN 100 int a[MAXN]; int main(int argc, char *argv[]) { int b, n, i = 0, j; scanf("%d %d", &b, &n); while(n/b != 0 || n%b != 0) { a[i++] = n%b; n = n/b; } for(j = i-1; j >= 0; j--) printf("%d", a[j]); printf("\n"); system("PAUSE"); return 0; }
习题3-7
输入基数b(2<=b<=10)和正整数n(b进制),输出n的十进制表示。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define MAXN 100 char a[MAXN]; int main(int argc, char *argv[]) { int b, length, i, j; int n = 0; scanf("%d", &b); scanf("%s", a); length = strlen(a); for(i = length-1, j = 0; i >= 0; i--, j++) { n = n + (a[i] - '0')*pow(b,j); } printf("%d\n", n); system("PAUSE"); return 0; }
标签:blog io ar for sp 2014 log bs amp
原文地址:http://blog.csdn.net/oceaniwater/article/details/40785227