HJ5 进制转换
描述
寫出一個(gè)程序,接受一個(gè)十六進(jìn)制的數(shù),輸出該數(shù)值的十進(jìn)制表示。
輸入描述:
輸入一個(gè)十六進(jìn)制的數(shù)值字符串。注意:一個(gè)用例會(huì)同時(shí)有多組輸入數(shù)據(jù),請(qǐng)參考帖子https://www.nowcoder.com/discuss/276處理多組輸入的問題。
輸出描述:
輸出該數(shù)值的十進(jìn)制字符串。不同組的測(cè)試用例用\n隔開。
示例1
輸入:
0xA
0xAA
輸出:
10
170
#include <iostream>
#include <string>
#include <cmath>int main()
{std::string str;while(getline(std::cin, str)){int result = 0;for(int i=2; i<str.length(); i++){if((str[i] >= 'A' && str[i] <= 'F') || (str[i] >= 'a' && str[i] <= 'f')){result += (str[i] - 'A' + 10) * pow(16, str.length() - i - 1);}if(str[i] >= '0' && str[i] <= '9'){result += (str[i] - '0') * pow(16, str.length() - i - 1);}}std::cout << result << std::endl;}return 0;
}
總結(jié)
- 上一篇: 2022-2028年中国电梯行业市场调查
- 下一篇: 2022-2028中国曝光机市场现状及未