c++ - 我想将读入的两个数字除以一行
问题描述
我有一个学校作业。但是我还没有实现一些代码。在一行中读完三个数字 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];
}
}
解决方案
您可以制作您想要制作的部分
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];
}
}
推荐阅读
- java - 如何根据 LocalTime 列表创建通知?
- azure - 单页应用程序的 Azure CDN Microsoft 标准规则引擎重写 URL
- python - 任务管理器不想在用户未登录时执行任务
- kubernetes - Ecplise hono 1.0.0 需要哪个 k8s 版本?
- .net - 如何从 WebView2 中的 ExecuteScript 获取同步返回
- android - 在片段中实例化 WorkManager 时 requireActivity() 或获取应用程序上下文
- java - 在一个分支搜索 Game Hive 中 Spider 的实现后,递归停止
- kotlin - 如何简化 Kotlin 中的谓词链
- haskell - 如何在 Haskell Stack+Nix 项目中添加 Nix 项目作为依赖项
- java - 显示整数 (>0) 是否可以被 2 和 3 整除