c++ - 比较数字中的数字
问题描述
始终对称地将数字与其中间数字进行比较。如果第一个数字大于最后一个数字,第一个是获胜的,我必须显示它,否则我显示最后一个并保持直到我达到中间数字(这是如果我有奇数个数字),如果数字没有任何东西与之相比,它自动获胜。
例如数字是 13257,答案是 7 5 2。
另一个 583241 答案是 5 8 3。现在我只是想抓住奇数位数。卡住了..这是我的代码。问题是这段代码不显示任何数字,但它在 if 语句中比较它们(我在调试时检查过)。
#include <iostream>
using namespace std;
int countDigit(int n) {
int count = 0;
while (n != 0) {
count++;
n /= 10;
}
return count;
}
int main() {
int n;
cin >> n;
int middle;
int count = countDigit(n);
if (count % 2 == 0) {
cout<<"No mid digit exsist!!";
}
else {
int lastDigit = n % 10;
middle = (count + 1) / 2;
for (int i = 0; i < middle; i++) {
for (int j = lastDigit; j<middle; j--) {
if (i > j) {
cout << i <<' ';
}
else {
cout << j;
}
}
}
}
return 0;
}
解决方案
我们分析需求,然后提出设计。
如果我们有一个由数字组成的数字,我们希望将“左”值与“右”值进行比较。因此,以某种方式从数字的左右数字索引开始。
Look at this number: 123456789
Index: 012345678
Length: 9
在 C 和 C++ 中,索引从 0 开始。
那么,我们会怎么做呢?
- 比较索引 0 和索引 8
- 比较索引 1 和索引 7
- 比较索引 2 和索引 6
- 比较索引 3 和索引 5
- 比较索引 4 和索引 4
因此,左侧的索引向上运行,右侧的索引向下运行。
只要左索引小于或等于右索引,我们就会继续。所有这些都可以在一个for
或while
循环中完成。
没关系,位数是奇数还是偶数。
当然,我们还需要返回数字长度和给定位置的数字位数的函数。但我看到您已经知道如何编写这些函数。所以,我不会在这里进一步解释。
我向您展示了 3 个不同的示例。
- 超级简单而且非常冗长。非常低效,因为我们没有数组。
- 仍然简单,但更压缩。非常低效,因为我们没有数组。
- C++ 解决方案,在您的情况下不允许
- 详细
#include <iostream>
// Get the length of a number
unsigned int length(unsigned long long number) {
unsigned int length = 0;
while (number != 0) {
number /= 10;
++length;
}
return length;
}
// Get a digit at a given index of a number
unsigned int digitAt(unsigned int index, unsigned long long number) {
index = length(number) - index - 1;
unsigned int result = 0;
unsigned int count = 0;
while ((number != 0) && (count <= index)) {
result = number % 10;
number /= 10;
++count;
}
return result;
}
// Test
int main() {
unsigned long long number;
if (std::cin >> number) {
unsigned int indexLeft = 0;
unsigned int indexRight = length(number) - 1;
while (indexLeft <= indexRight) {
if (digitAt(indexLeft, number) > digitAt(indexRight, number)) {
std::cout << digitAt(indexLeft, number);
}
else {
std::cout << digitAt(indexRight, number);
}
++indexLeft;
--indexRight;
}
}
}
压缩
#include <iostream>
// Get the length of a number
size_t length(unsigned long long number) {
size_t length{};
for (; number; number /= 10) ++length;
return length;
}
// Get a digit at a given index of a number
unsigned int digitAt(size_t index, unsigned long long number) {
index = length(number) - index - 1;
unsigned int result{}, count{};
for (; number and count <= index; ++count, number /= 10)
result = number % 10;
return result;
}
// Test
int main() {
if (unsigned long long number; std::cin >> number) {
// Iterate from left and right at the same time
for (size_t indexLeft{}, indexRight{ length(number) - 1 }; indexLeft <= indexRight; ++indexLeft, --indexRight)
std::cout << ((digitAt(indexLeft,number) > digitAt(indexRight, number)) ? digitAt(indexLeft, number) : digitAt(indexRight, number));
}
}
更现代的 C++
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main() {
if (std::string numberAsString{}; std::getline(std::cin, numberAsString) and not numberAsString.empty() and
std::all_of(numberAsString.begin(), numberAsString.end(), std::isdigit)) {
for (size_t indexLeft{}, indexRight{ numberAsString.length() - 1 }; indexLeft <= indexRight; ++indexLeft, --indexRight)
std::cout << ((numberAsString[indexLeft] > numberAsString[indexRight]) ? numberAsString[indexLeft] : numberAsString[indexRight]);
}
}
推荐阅读
- javascript - 需要在 javascript 中使用 firestore 的帮助
- sqlite - FutureBuilder 在不触摸屏幕的情况下无法工作
- c++ - 基于 for 循环的无辜范围不起作用
- android - android推送的FCM消息中的`ttl`和`time_to_live`属性有什么区别?
- python - 根据给定的汉明距离折叠字符串集
- django - 提交表单并从输入字段中检索值后重定向到另一个页面
- c++ - 用于 localhost:Port 的 MSVC++ 中的 TLS 客户端
- powershell - 如何将其值中包含连字符的$变量作为参数传递给另一个函数
- javascript - 如何通过将鼠标悬停在 sigmaJs 中的当前节点上仅对当前节点和连接节点绘制标签为真?
- razor - DB2 razor 中的列或参数定义无效