题目描述:其 A+B 和的D进制.
题目不难,主要被输入流是string卡住了,复习一下 string 与 char[] 与int 相关类型的转换
string 转 char[]
#include <iostream>
#include <string>
using std::string;void main()
{
string add_to="hello!";
//std::cout<<add_to;
const string add_on="baby";
const char*cfirst = add_to.c_str();
const char*csecond = add_on.c_str();
char*copy = new char[strlen(cfirst) + strlen(csecond) + 1];
strcpy( copy, cfirst);
strcat( copy, csecond);
add_to = copy;
delete [] copy;
std::cout<<add_to<<std::endl;
char[] 转string:
可以直接复制给string
int转string
通过int 到char[] 再到string
int a= 10002;
char tmp[32];
sprintf(tmp,"%d",a);
string s2 = tmp;
#include <sstream>
int main() {
stringstream ss;
int n = 65535;
string s;
ss << n;
ss >> s;
cout << s << endl;
...}
/*
* Main.cpp
*
* Created on: 2015年3月16日
* Author: jason
*/
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
char tmp[32];
string transfer(long long a,int d) {
if(a>=d) {
sprintf(tmp,"%d",a%d);
string s2 = tmp;
return transfer(a/d,d)+s2;
}
else {
sprintf(tmp,"%d",a);
string s2 = tmp;
return s2;
}
}
int main(){
string s;
char ch[80];
stringstream ss;
int n = 65535;
string s;
ss << n;
ss >> s;
cout << s << endl;
while(cin.getline(ch,80,'\n')) {
s = ch;
int space = 0;
int x = 0,a,b,d;
for(int i=0;i<s.length();i++){
if(s[i]==' '&&space ==0) {
a = x;
x = 0;
space++;
continue;
}
if(s[i]==' '&&space ==1) {
b = x;
x = 0;
space++;
continue;
}
x *=10;
x = x+ s[i]-'0';
}
d = x;
if(d==1)
cout<<"-1";
else
cout<<transfer(a+b,d);
}
return 0;
}