在 tojinzhi这个函数中使用了一种逻辑 ,即当x不等于零时 在循环中统计 有效的位数 即变量 weisize,
典型的代码如下:
weiSize =0;
do {
int x = a % dc;
if (x != 0) weiSize++;
if (weiSize != 0) weiSize++;
anss[Size++]= (x < 10) ? (x + '0') : (x - 10 + 'A');
a /= dc;
} while (a);
这道题总的思路是利用典型的任意进制转换方法:从任意进制转换为10进制,再从10进制转换为任意进制。转换格式是从字符串转换为数字,再从数字转换为字符串。即只有十进制时用数字。
这道题的代码:
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <math.h>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
const int Max = 10000;
char s[Max],anss[Max];
int Size=0,weiSize =0;
void tonjinzhi(long long a,int dc) {//将10进制转换为任意的dc进制
do {
int x = a % dc;
if (x != 0) weiSize++;
if (weiSize != 0) weiSize++;
anss[Size++]= (x < 10) ? (x + '0') : (x - 10 + 'A');
a /= dc;
} while (a);
//Size--;
return ;
}
void print() {
for (int i = Size - 1; i>=0&&weiSize>1; i--,weiSize--) {
printf("%c", anss[i]);
}
printf("\n");
}
int main()
{
freopen("Text.txt", "r", stdin);
long long n1 = 0,n2 =0;
int d = 0;
while (scanf("%lld %lld %d",&n1,&n2,&d)!=EOF) {
long long ans = n1 + n2;
//sprintf(s, "%lld", ans);
tonjinzhi(ans,d);
print();
Size = 0;
weiSize = 0;
}
return 0;
}