我有一个学校作业。但是我还没有实现一些代码。 在一行中读取三个数字a和b后,请反向读取此数字。 最后,需要比较两个读数并输出大量读数。
我是用C ++实现的,但是我无法将读取的两个数字相除。
这是一个示例
-input:
123 451
-输出:
321
这是我的代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(void){
char input[8];
cin.getline(input,8,'\n');
string compare1="";
string compare2="";
// I want to parsing this input string(line) to compare1 / compare2
// but I can’t.
reverse(compare1.begin(), compare1.end());
reverse(compare2.begin(), compare2.end());
if(compare1.compare(compare2)<0){
cout<<compare2<<endl;
}else{
cout<<compare1<<endl;
}
return 0;
}
bool next = false;
for(int i=0; input[i]!='\0'; i++){
if(input[i] == ' '){
next = true;
continue;
}
if(next){
compare2 += input[i];
}else {
compare1 += input[i];
}
}
答案 0 :(得分:1)
您可以制作想要制造的零件
bool next = false;
for(int i=0; input[i]!='\0'; i++){
if(input[i] == ' '){
next = true;
continue;
}
if(next){
compare2 += input[i];
}else {
compare1 += input[i];
}
}