(HDUStep 1.2.6)decimal system(任意进制转十进制)

题目:

 

decimal system

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3622 Accepted Submission(s): 1419
 
Problem Description As we know , we always use the decimal system in our common life, even using the computer. If we want to calculate the value that 3 plus 9, we just import 3 and 9.after calculation of computer, we will get the result of 12.
But after learning <<The Principle Of Computer>>,we know that the computer will do the calculation as the following steps:
1 computer change the 3 into binary formality like 11;
2 computer change the 9 into binary formality like 1001;
3 computer plus the two number and get the result 1100;
4 computer change the result into decimal formality like 12;
5 computer export the result;

In the computer system there are other formalities to deal with the number such as hexadecimal. Now I will give several number with a kind of change method, for example, if I give you 1011(2), it means 1011 is a number in the binary system, and 123(10) means 123 if a number in the decimal system. Now I will give you some numbers with any kind of system, you guys should tell me the sum of the number in the decimal system.  

Input There will be several cases. The first line of each case contains one integers N, and N means there will be N numbers to import, then there will be N numbers at the next N lines, each line contains a number with such form : X1….Xn.(Y), and 0<=Xi<Y, 1<Y<=10. I promise you that the sum will not exceed the 100000000, and there will be at most 100 cases and the 0<N<=1000.  
Output There is only one line output case for each input case, which is the sum of all the number. The sum must be expressed using the decimal system.  
Sample Input

3
1(2)
2(3)
3(4)

4
11(10)
11(2)
11(3)
11(4)

 

Sample Output

6
23

 

 
Source HDU 2007-6 Programming Contest  
Recommend xhd  

      



题目分析:

      其实就是任意进制转十进制的一道题。对应的还有十进制转任意进制的题。做这种题其实只需要写出一个任意进制转十进制的函数toTen()即可。因为十进制专人以及之C++中有itoa()这个函数。需要注意的是如果希望将字符串转换成数字可以使用atoi()这个函数



任意进制转十进制的原理分析:

1、二进制数、八进制数、十六进制数转十进制数 
有一个公式:二进制数、八进制数、十六进制数的各位数字分别乖以各自的基数的(N-1)次方,其和相加之和便是相应的十进制数。个位,N=1;十位,N=2…举例: 
110B=1*2的2次方+1*2的1次方+0*2的0次方=0+4+2+0=6D 
110Q=1*8的2次方+1*8的1次方+0*8的0次方=64+8+0=72D 
110H=1*16的2次方+1*16的1次方+0*16的0次方=256+16+0=272D 
2、十进制数转二进制数、八进制数、十六进制数 
方法是相同的,即整数部分用除基取余的算法,小数部分用乘基取整的方法,然后将整数与小数部分拼接成一个数作为转换的最后结果。 
例:见四级指导16页。 
3、二进制数转换成其它数据类型 
3-1二进制转八进制:从小数点位置开始,整数部分向左,小数部分向右,每三位二进制为一组用一位八进制的数字来表示,不足三位的用0补足, 
就是一个相应八进制数的表示。 
010110.001100B=26.14Q 
八进制转二进制反之则可。 
3-2二进制转十进制:见1 
3-3二进制转十六进制:从小数点位置开始,整数部分向左,小数部分向右,每四位二进制为一组用一位十六进制的数字来表示, 
不足四位的用0补足,就是一个相应十六进制数的表示。 
00100110.00010100B=26.14H 
十进制转各进制 
要将十进制转为各进制的方式,只需除以各进制的权值,取得其余数,第一次的余数当个位数,第二次余数当十位数,其余依此类推,直到被除数小于权值,最后的被除数当最高位数。 
一、十进制转二进制 
如:55转为二进制 
2|55 
27――1 个位 
13――1 第二位 
6――1 第三位 
3――0 第四位 
1――1 第五位 
最后被除数1为第七位,即得110111 
二、十进制转八进制 
如:5621转为八进制 
8|5621 
702 ―― 5 第一位(个位) 
87 ―― 6 第二位 
10 ―― 7 第三位 
1 ―― 2 第四位 
最后得八进制数:127658 
三、十进制数十六进制 
如:76521转为十六进制 
16|76521 
4726 ――5 第一位(个位) 
295 ――6 第二位 
18 ――6 第三位 
1 ―― 2 第四位 
最后得1276516 
二进制与十六进制的关系 
2进制 0000 0001 0010 0011 0100 0101 0110 0111 
16进制 0 1 2 3 4 5 6 7 
2进制 1000 1001 1010 1011 1100 1101 1110 1111 
16进制 8 9 a(10) b(11) c(12) d(13) e(14) f(15) 
可以用四位数的二进制数来代表一个16进制,如3A16 转为二进制为: 
3为0011,A 为1010,合并起来为00111010。可以将最左边的0去掉得1110102 
右要将二进制转为16进制,只需将二进制的位数由右向左每四位一个单位分隔,将各单位对照出16进制的值即可。 
二进制与八进制间的关系 
二进制 000 001 010 011 100 101 110 111 
八进制 0 1 2 3 4 5 6 7 
二进制与八进制的关系类似于二进制与十六进制的关系,以八进制的各数为0到7,以三位二进制数来表示。如要将51028 转为二进制,5为101,1为001,0为000,2为010,将这些数的二进制合并后为1010010000102,即是二进制的值。 
若要将二进制转为八进制,将二进制的位数由右向左每三位一个单位分隔,将事单位对照出八进制的值即可。



代码实现如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

/**
 * 任意进制转换成10进制的方法
 */
long toTen(char a[], int bit) {
	int length = strlen(a);
	int i, b = 1, sum = 0; //i要做数组a的下标,注意其起止范围
	for (i = length - 1; i >= 0; i--) {
		if (a[i] >= 'A') {
			sum += (a[i] - 'A' + 10) * b;
			b *= bit;
		} else {
			sum += (a[i] - '0') * b;
			b *= bit;
		}
	}
	return sum;
}

int main() {
	int n;
	while (scanf("%d", &n) != EOF) {
		int i;

		int sum = 0;
		for (i = 0; i < n; ++i) {
			int a, b;
			char left, right;
			cin >> a >> left >> b >> right;

			char number[1003];
			itoa(a, number, 10);//将十进制转换成任意进制,在这里主要适用于将a转成字符串.需要注意的是可能需要使用C++
			sum += toTen(number, b);
		}


		printf("%d\n", sum);
	}

	return 0;
}






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