【Openjudge】十进制数字转二进制数

从N进制,转为M进制,当然,题目要求不高,所以边角工作没有做,超出十进制以后的转换因为没有定义相关的符号表示,所以会出错。

#include<iostream>
using namespace std;

int main()
{
	int N,M;
	string ns, ms;
	int ni[1010], mi[1010];
	int ti, ri, p, q, pm;
	cin >> ns;
	int i;
	for (i = 0 ; ns[i] != 0; i ++){
		ni[i] = ns[i] - '0';
	}
	ni[i] = -1;

	N = 10; M = 2;

	pm = 0;
	while (ni[0] != 0){
		p = 0;
		q = 0;
		ti = ni[p++];
		while (ti < M && ni[p] != -1){
			ti = ti * N + ni[p++];
		}
		ni[q ++] = ti / M;
		ri = ti % M;
		while (ni[p] != -1){
			ti = ri * N + ni[p ++];
			while (ti < M && ni[p] != -1){
				ni[q ++] = 0;
				ti = ti * N + ni[p++];
			}
			ni[q ++] = ti / M;
			ri = ti % M;
		}
		ni[q] = -1;
		mi[pm ++] = ri;
	}
	mi[pm] = -1;

	for (pm --; pm >= 0; pm --){
		ms.push_back((char)('0' + mi[pm]));
	}

	cout << ms;


}

    原文作者:进制转换
    原文地址: https://blog.csdn.net/DreamChaserYx/article/details/78175032
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞