首页 > 解决方案 > 我想将读入的两个数字除以一行

问题描述

我有一个学校作业。但是我还没有实现一些代码。在一行中读完三个数字 a 和 b 后,将这个数字反过来读。最后是比较两个读数并输出大量数字的问题。

我在 C++ 中实现了这一点,但我无法将读取的两个数字相除。

这是一个例子

-输入 :

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];
    }
}

标签: c++

解决方案


您可以制作您想要制作的部分

  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];
  }
}

推荐阅读