这道题还是卡了我半天,测试数据对了四分之三,一直想不出来最后一个错误的数据是怎么产生的,于是在网上找了一份别人的源码,自己又测了一些数据,发现都没问题。
一直到最后测试了FFFFFFFF的时候,发现产生了越界,才反应过来是int数据小了,必须用long
//这是我的源码,这种做法,效率不高,勉强解决问题
import java.util.Scanner;
public class Main {
public static void main(String[] args){
String str = "";
Scanner sc = new Scanner(System.in);
str = sc.next();
long result = 0;
char[] ch = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
for (int i=0;i<str.length();i++){
long t=1;
for (int j=0;j<ch.length;j++){
if (str.charAt(i) == ch[j]){
for (int m=0;m<str.length()-i-1;m++){
t = 16*t;
}
t = j*t;
System.out.println("t="+t);
}
}
result = result+t;
System.out.println("result="+result);
}
System.out.print(result);
}
}
对于进制转换的题目,个人觉得也没多大意思,如果用java做的话,直接调用API,就可以把题目解决~~~不过需要把API掌握的很熟
如下:
import java.util.Scanner;
public class Main{
public static void transform() {
Scanner sc = new Scanner(System.in);
String arry = "";
String number = sc.next();
if (number.length() <= 8) {
arry = Long.valueOf(number, 16).toString();
}
System.out.println(arry);
}
public static void main(String[] args) {
transform();
}
}
在网上看到的其他人的一些做法,部分做法略显麻烦。
如下:
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
char s[10000];
cin>>s;
int len=strlen(s);
long long int ans=0;
for(int i=0;i<len;i++)
{
if(s[i]>='A'&&s[i]<='Z')
{
s[i]=int(s[i]-'A')+10+'0';
}
ans+=((s[i]-'0')*(pow(16,len-1-i)));
}
cout<<ans<<endl;
return 0;
}
import java.util.Scanner;
public class Main{
public static void convertHexIntoDecimal(char[] arryHex, String hexStr){
StringBuilder binaryStb = new StringBuilder();
int len = hexStr.length();
long count = 1;
long decimal = 0;
String binaryStr = "";
arryHex = hexStr.toCharArray();
for (int i = 0; i < len; i++) { //Convert Hex into Binary
switch (arryHex[i]) {
case '0':
binaryStb.append("0000");
break;
case '1':
binaryStb.append("0001");
break;
case '2':
binaryStb.append("0010");
break;
case '3':
binaryStb.append("0011");
break;
case '4':
binaryStb.append("0100");
break;
case '5':
binaryStb.append("0101");
break;
case '6':
binaryStb.append("0110");
break;
case '7':
binaryStb.append("0111");
break;
case '8':
binaryStb.append("1000");
break;
case '9':
binaryStb.append("1001");
break;
case 'A':
binaryStb.append("1010");
break;
case 'B':
binaryStb.append("1011");
break;
case 'C':
binaryStb.append("1100");
break;
case 'D':
binaryStb.append("1101");
break;
case 'E':
binaryStb.append("1110");
break;
case 'F':
binaryStb.append("1111");
break;
default:
break;
}
}
binaryStr = binaryStb.toString();
len = binaryStr.length();
for(int i = len - 1; i >= 0; i--){
if(binaryStr.charAt(i) == '1'){
decimal = decimal + count;
}
count = count * 2;
}
System.out.println(decimal);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char [] arryHex = new char[8];
String hexStr = sc.next();
convertHexIntoDecimal(arryHex,hexStr);
}
}
#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
char strHex[8];
int i = 0, len = 0, temp = 0;
__int64 nDec = 0;
gets(strHex);
len = strlen(strHex);
for (i = 0; strHex[i] != '\0'; ++i)
{
switch(strHex[i])
{
case 'A': temp = 10; break;
case 'B': temp = 11; break;
case 'C': temp = 12; break;
case 'D': temp = 13; break;
case 'E': temp = 14; break;
case 'F': temp = 15; break;
default: temp = strHex[i]-'0'; break;
}
nDec += (temp*pow(16,len-1-i));
}
printf("%I64d",nDec);
return 0;
}