本题目要求读入若干个代表整数的字符串,然后将其转化为整数。
如果该数>=10000且<=20000,则依次输出其对应的二进制字符串、八进制字符串、十六进制字符串。
否则将字符串中的每个数字抽取出来,然后将所有数字加总求和。
提示:参考jdk文档的Integer,
输入样例:
123
10000
-123
314159265
输出样例:
1 2 3 6
10011100010000,23420,2710
1 2 3 6
3 1 4 1 5 9 2 6 5 36
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
void fun(string s[], int n)
{
for (int i = 0; i < n; i++)
{
int ans = atoi(s[i].c_str());
if (ans >= 10000 && ans <= 20000)
cout << (bitset<15>)ans << "," << oct << ans << "," << hex << ans << endl;
else
cout << dec << ans << endl;
}
}
int main(int argc, char *argv[])
{
string s[4] = { "123","10000","-123","314159265" };
fun(s,4);
return 0;
}
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
void fun(string s[]){
for (int i = 0; i < 4; i++){
int ans = atoi(s[i].c_str());
if(ans >= 10000 && ans <= 20000) cout << (bitset<15>)ans << "," << oct << ans << "," << hex << ans << endl;
else cout << dec << ans << endl;
}
}
string s[4] = { "123","10000","-123","314159265" };
int main() {
fun(s);
return 0;
}