C++ 投影仪 #4 代码错误
c++ projecteuler #4 code mistake
我正在尝试解决欧拉项目#4中给出的问题:
回文数的两种读法相同。由两个 2 位数字的乘积构成的最大回文是 9009 = 91 × 99。
找到由两个 3 位数字的乘积制成的最大回文。
我的解决方案中没有任何编译错误...但是程序不提供正确的输出。 我想我在定义布尔函数时犯了一些错误。
有什么想法吗?
#include <iostream>
#include <math.h>
using namespace std;
bool isPalindrome(int z) {
// store z in t to compare it later with its reversed number
int t = z;
int rev = 0;
// This while loop reverses the number
while (z > 0) {
int dig = z % 10;
rev = rev * 10 + dig;
z = z / 10;
}
if (t == rev)
return true;
else
return false;
}
void palindrome(int k) {
int b = 0;
int a = pow(10, k);
// calculate product of two numbers
// replace it if it's palindrome and greater than previously stored value b
// for loop to calculate product of all 3 (in general k) digit numbers
for (int i = pow(10,k-1); i = pow (10,k) - 2; i++) {
for (int j = i; j = pow (10,k) - 2; j++) {
int c = i * j;
if (isPalindrome(c) && c > b) {
b = c;
}
}
}
cout << "Largest Palindrome = " << b << endl;
}
int main() {
int n;
cout << "Enter the digit length" <<"t";
cin >> n;
palindrome(n);
return 0;
}
对于它不起作用的示例,我尝试输入 n=3,它只是挂起,没有输出。 我做错了什么?
在我看来
,你的问题出在这条线上,
for (int i = pow(10,k-1); i = pow (10,k) - 2; i++) {
考虑循环将如何终止。
相关文章:
- 错误处理.将系统错误代码映射到泛型
- 为什么我在使用void函数时得到错误代码C2276
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 逻辑运算符上出现错误代码 a')'
- 'val' Arduino 错误代码之前的预期'('
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 根据 GetLastError 直接写入磁盘会导致错误代码 5
- 当我选择大于 720 的矩阵大小时,程序退出并显示错误代码.可能是什么原因?
- 创建进程 API 失败,在窗口 122 上出现错误代码 10
- 使用另一个函数调用一个函数(都在类中)时出现问题.没有错误代码C++
- 当命令失败时,gzip 会在 C++ 中返回错误代码吗?
- 从 C++ 调用 cURL 命令会返回意外的错误代码,如 1792 和 6656
- WSALookupServiceBegin() 上的错误代码 10022(无效的 arugment)
- C++ Schannel POST 400 错误代码错误请求
- openCL-创建子缓冲区返回错误代码13
- 更新了Runge-Kutta(RK4)C++错误代码中的二阶DE
- 获取有关使用未初始化内存的错误代码
- 排序程序的意外错误代码
- 正在将DJI错误代码记录到流中
- 来自API的错误代码..处理什么是好的做法