问题 F: 10进制 VS 2进制


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
void convert(int in[], int len, int out[],int &ks, int m, int n) {

    for (int i = 0; i < len;) {
        int k = 0;
        for (int j = i; j < len; ++j) {
            int temp = k * m + in[j];
            in[j] = temp / n;
            k = temp % n;
        }
        out[ks++] = k;
        while (in[i] == 0) i++;
    }


}


int main() {

    char s[1000];
    while (scanf("%s", s) != EOF) {

        int n = strlen(s);
        int in[1000];
        for (int i = 0; i < n; ++i) {
            in[i] = s[i] - '0';
        }
        int mid[1000];
        int ks=0;
        convert(in, n, mid,ks, 10, 2);
        int out[1000];
        int ks2=0;
        convert(mid,ks,out,ks2,2,10);


        for (int j = ks2-1; j >=0 ; --j) {
            printf("%d",out[j]);
        }
        printf("\n");

    }

    return 0;
}
上一篇:用肘方法确定 kmeans 聚类中簇的最佳数量


下一篇:linux kickstart自动安装脚本