写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。 数据范围:保证结果在
区块链毕设网qklbishe.com为您提供问题的解答
using namespace std;
int main()
{
int a;
while(cin>>hex>>a){
cout<<a<<endl;
}
}
/*不太明白这道题想考什么?只是16进制转换而已。要求输出十进制字符串,但以int形式输出也通过了。*/#include <iostream>#include <cstring>#include <cmath>using namespace std;intf(charc,inti){intn;switch(c){case'F': n=15; break;case'E': n=14; break;case'D': n=13; break;case'C': n=12; break;case'B': n=11; break;case'A': n=10; break;default: n=c-'0';}returnn*pow(16,i);}intmain(){chars[10];while(cin>>s){intnumber = 0;for(inti=2;i<strlen(s);++i){number += f(s[i],strlen(s)-i-1);}cout << number << endl;}return0;}
方法采用ASCII码的字符比较
import java.util.*; public class Main{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){ char[] str = sc.nextLine().toCharArray(); int sum = 0; for (int i=2;i<str.length;i++) sum = sum*16+(str[i] >= 'A'?str[i]-'A'+10:str[i]-'0'); System.out.println(sum); } } }
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] cs = br.readLine().toCharArray();
Map<String, Integer> maps = new HashMap<String, Integer>() {
{
put("A", 10);
put("B", 11);
put("C", 12);
put("D", 13);
put("E", 14);
put("F", 15);
}
};
int sum = 0;
for (int i = 2; i < cs.length; i++) {
Integer temp = maps.get(Character.toString(cs[i]));
int spare = cs.length – i – 1;
if (temp != null) {
sum += temp * Math.pow(16, spare);
} else {
sum += Integer.parseInt( Character.toString(cs[i])) * Math.pow(16, spare);
}
}
System.out.println(sum);
}
}
get_str = input() dict_0x = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8, '9':9,'A':10,'B':11,'C':12,'D':13,'E':14,'F':15,'G':16} n = len(get_str) num = 0 for i in range(n, 2, -1): num += dict_0x[get_str[i-1]] * 16**(n-i) print(num)
#include "string.h" #include "stdio.h" int main() { int num ; while (~scanf("%x", &num)) { //输入认定为十六进制数 printf("%dn", num); //输出十进制数 } return 0; }
#include<iostream> #include<string> using namespace std; int main(void) { string str; cin >> str; cout << stoi(str.substr(2, str.size() - 2), nullptr, 16) << endl; return 0; }
import java.util.*; import java.math.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextLine()){ String txt = sc.nextLine(); String hex = txt.substring(2); System.out.println(new BigInteger(hex, 16).toString(10)); } } }
这个主要就是考 大整型数的处理吧
import java.util.*; lic class Main { public static double parsX(String num){ num = num.toUpperCase(); String number = num.substring(2); int total = number.length(); char[] chars = number.toCharArray(); double res = 0; for (int i = 0; i < chars.length; i++) { char c = chars[i]; int index = total-i-1; int realNum = c; if (realNum>=65){ realNum-=55; }else { realNum-=48; } double pow = Math.pow(16, index)*realNum; res+=pow; } return res; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String num = sc.nextLine(); double v = parsX(num); System.out.println(new Double(v).intValue()); } }
遍历字符串就行了,字符本身也是数字
#include<stdio.h> int main(void){ int n; scanf("%x", &n); printf("%d", n); }
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); int x; if("0x".equals(str.substring(0,2))) { x = Integer.parseInt(str.substring(2), 16); //输入的前边两位是OX的话,需要截掉前两位 }else{ x=Integer.parseInt(str,16); } System.out.println(x); } }
import java.util.Map; import java.util.HashMap; import java.util.Scanner; public class Main{ public static void main(String[] args){ HashMap<Character, Integer> map = new HashMap<Character, Integer>(); map.put('0',0); map.put('1',1); map.put('2',2); map.put('3',3); map.put('4',4); map.put('5',5); map.put('6',6); map.put('7',7); map.put('8',8); map.put('9',9); map.put('A',10); map.put('B',11); map.put('C',12); map.put('D',13); map.put('E',14); map.put('F',15); Scanner sc = new Scanner(System.in); String data = new String(sc.nextLine()); data = data.substring(2); int sum = 0; for(Character c: data.toCharArray()){ sum = map.get(c) + sum * 16; // System.out.println(map.get(c)); } System.out.println(sum); // for(Character c : data.toCharArray()){ // System.out.println(c.getClass().toString()); // } } }
#include<iostream> #include<string> #include<math.h> using namespace std; //0、1、2、3、4、5、6、7、8、9、A、B、C、D、E、F int main() { string num; cin>>num; int len=num.size(); //最多8位 int number=0; for(int i=len-1;i>=0;i--) { char c=num[i]; int temp=0; switch(c) { //注意不要忘记break,否则temp一直是15 case 'x': temp=0;break; case '0': temp=0;break; case '1': temp=1;break; case '2': temp=2;break; case '3': temp=3;break; case '4': temp=4;break; case '5': temp=5;break; case '6': temp=6;break; case '7': temp=7;break; case '8': temp=8;break; case '9': temp=9;break; 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; } number=number+pow(16,len-i-1)*temp; } cout<<number<<endl; return 0; }
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine().substring(2); HashMap<Character, Integer> sMap = new HashMap<>(); sMap.put('0', 0); sMap.put('1', 1); sMap.put('2', 2); sMap.put('3', 3); sMap.put('4', 4); sMap.put('5', 5); sMap.put('6', 6); sMap.put('7', 7); sMap.put('8', 8); sMap.put('9', 9); sMap.put('A', 10); sMap.put('B', 11); sMap.put('C', 12); sMap.put('D', 13); sMap.put('E', 14); sMap.put('F', 15); int len = s.length(); int res = 0; int sum = 0; for (int i = 0; i < len; i++) { int bit = len - i; res = sMap.get(s.charAt(i)); if (bit != 1) { for (int j = 0; j < (bit - 1); j++) { res = res * 16; } } else { res = sMap.get(s.charAt(i)); } sum += res; } System.out.println(sum); } }
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
//设置权重等于1
int weight = 1;
//基数乘以权重之和
int sum = 0;
for(int i = str.length()-1;i >= 0;i–){
char c = str.charAt(i);
int n = 0;
if(c >= ‘0’ && c <= ‘9’){
//把c转化为对应的数字
n = c – ‘0’;
} else if( c >= ‘A’ && c <= ‘F’){
n = c – ‘A’ + 10;
}else if( c >= ‘a’ && c <= ‘f’){
n = c – ‘a’ + 10;
}
//基数乘以权重之和
sum = sum + n * weight;
//改变权重,权重等于权重乘以进制数
weight = weight * 16;
}
System.out.print(sum);
}
}
using namespace std;
int main(){
int n;
cin >> hex >> n;
cout << dec << n;
return 0;
}
using namespace std;
int main(){
string s ;
cin >> s;
cout << stoi(s, 0, 16) << endl;
}
以上就是关于问题写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。
数据范围:保证结果在的答案
欢迎关注区块链毕设网-
专业区块链毕业设计成品源码,定制。
区块链NFT链游项目方科学家脚本开发培训