#include<iostream> using namespace std; //十进制 dec //十六进制:hex //八进制:oct int main() { int a; while(cin>>hex>>a) //读入六进制 { cout<<a; cout<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll XToDec(string s,ll x) { //将给定的x进制字符串s转换成10进制 ll ans = 0; for(ll i = 0; i < s.length(); i++) { ans = ans*x + (isdigit(s[i]) ? s[i]-'0' : s[i]-'A'+10); } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); string s; while(cin >> s) { s = s.substr(2,s.length()-2); cout << XToDec(s,16) << endl; } return 0; }
#include<iostream> (720)#include<string.h> #include<math.h> using namespace std; int main(){ char s[1000]; while(gets(s)){ int m=0,sum=0; int len=strlen(s); for(int i=len-1;i>=0;i--){ if(s[i]>='0'&&s[i]<='9') sum=sum+(s[i]-'0')*pow(16,m); else if(s[i]>='A'&&s[i]<='F') sum=sum+(s[i]-'A'+10)*pow(16,m); else if(s[i]>='a'&& s[i]<='f') sum=sum+(s[i]-'a'+10)*pow(16,m); m++; } cout<<sum<<endl; } }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc= new Scanner(System.in); while (sc.hasNext()) { String str=sc.nextLine(); int len=str.length()-2; int num=0; int y=0; for(int i=str.length()-1;i>=2;i--) { if(str.charAt(i)=='A')num+=10*toten(y); else if(str.charAt(i)=='B')num+=11*toten(y); else if(str.charAt(i)=='C')num+=12*toten(y); else if(str.charAt(i)=='D')num+=13*toten(y); else if(str.charAt(i)=='E')num+=14*toten(y); else if(str.charAt(i)=='F')num+=15*toten(y); else num+=Integer.parseInt(String.valueOf(str.charAt(i)))*toten(y); y++; } System.out.println(num); } } public static int toten(int a) { int x=1; if (a==0) return x; for (int i = 0; i < a; i++) { x*=16; } return x; } }
#include <iostream>
#include <string>
using namespace std;
unsigned long HextoDec(string s)
{
unsigned long sum = 0, *result = new unsigned long[s.length()];
for (int i = 0; i < s.length(); i++)
{
if (s[i] >= 'A' && s[i] <= 'F') result[i] = ((unsigned long)(s[i]) - 55) << (4 * (s.length() - 1 - i)); //m乘以的2的n次方 = m << n
else if (s[i] >= 'a' && s[i] <= 'f') result[i] = ((unsigned long)(s[i]) - 87) << (4 * (s.length() - 1 - i));
else if (s[i] >= '0' && s[i] <= '9') result[i] = (unsigned long)(s[i] - 48) << (4 * (s.length() - 1 - i));
sum += result[i];
}
return sum;
}
int main()
{
string s;
while (cin >> s)
{
string hex;
for(int i = 2; i < s.length(); i++) hex += s[i];
cout << HextoDec(hex) << endl;
}
return 0;
}
#include <iostream>#include <string>#include <cmath>using namespace std;intmain(){string str1;while(cin >> str1){string str2=str1.substr(2,str1.size()-2);intsum=0;for(inti = str2.size()-1;i>=0;--i){if(str2[i] >= '0'&& str2[i] <= '9')str2[i] -= 48;if(str2[i] >= 'A'&& str2[i] <= 'F')str2[i] -=55;sum += str2[i]*pow(16,str2.size()-i-1);}cout << sum << endl;}return0;}
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std ;
int len, res;
char str[ 1000 ];
int main()
{
while ( cin >> str )
{
res = 0 ;
len = strlen ( str );
int j = 0 ;
int cal = 0 ;
for ( int i= len - 1 ;i> 1 ;i--)
{
if ( str [i]>= ' 0 ' && str [i]<= ' 9 ' )
cal = str [i]- ' 0 ' ;
else if ( str [i]>= 'a' && str [i]<= 'z' )
cal = str [i]- 'a' + 10 ;
else
cal = str [i]- 'A' + 10 ;
res += cal* pow ( 16 ,j);
j++;
}
cout << res << endl ;
}
return 0 ;
}
十六进制转换十进制与二进制转换十进制同理,不同之处在于二进制的位权是2的指数,而十六进制的位权则是16的指数。针对本题输入的十六进制数值字符串除却开头的"0x",剩余位为有效位。对有效位从右至左依次升幂,同时乘以当位的基数,对每一位执行相同的如上操作,再将结果求和即可得十六进制数值对应的十进制数值。
import java.util.HashMap; import java.util.Scanner; public class Main { private static HashMap<Character, Integer> map; static { map = new HashMap<Character, Integer>(); map.put('A', 10); map.put('B', 11); map.put('C', 12); map.put('D', 13); map.put('E', 14); map.put('F', 15); } public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String input = in.nextLine(); System.out.println(toDecimal(input)); } } private static int toDecimal(String input) { //将十六进制数值字符串转换为十进制数值 int res = 0; int length = input.length() - 1; for (int i = input.length() - 1; i >= 2; i--) { //当前位字符 char cur = input.charAt(i); if (Character.isDigit(cur)) { //如果当前位字符为数字,直接将字符转为相应的int型数值,执行运算 res += Math.pow(16, length - i) * Integer.parseInt(cur + ""); } else { //如果当前位字符为A~F中的字母,根据十六进制规定,将其转换为相应的int型数值,执行运算 res += Math.pow(16, length - i) * map.get(cur); } } return res; } }
#include <bits/stdc++.h> using namespace std; int main(){ int x; while(scanf("%x", &x) == 1){ printf("%d\n", x); } return 0; }
#include <bits/stdc++.h> using namespace std; void pt(int x){ if(x > 9) pt(x / 10); putchar('0' + x % 10); } int main(){ int x; while(scanf("%x", &x) == 1){ pt(x); puts(""); } return 0; }
只是将字符串转换成十进制的数,程序就能通过,看来题目的目的只是将十六进制转换成十进制 #include<stdio.h> #include<string.h> #include<math.h> int main() { char str[100]; int i=0,count,sum; while(gets(str))//用于多次输入 { count=strlen(str);//计算字符串的长度 sum=0; for(i=count-1;i>=0;i--)//从十六进制个位开始,每位都转换成十进制 { if(str[i]>='0'&&str[i]<='9')//数字字符的转换 { sum+=(str[i]-48)*pow(16,count-i-1); } else if(str[i]>='A'&&str[i]<='F')//字母字符的转换 { sum+=(str[i]-55)*pow(16,count-i-1); } } printf("%d\n",sum); } return 0; }