由两个3位数字与C的乘积构成的最大回文
代码试图找到由两个2位数字的乘积构成的最大回文。答案是91*99=9009,但我一直得到990,这甚至不是回文。我真的很感谢你的帮助由两个3位数字与C的乘积构成的最大回文,c,algorithm,C,Algorithm,代码试图找到由两个2位数字的乘积构成的最大回文。答案是91*99=9009,但我一直得到990,这甚至不是回文。我真的很感谢你的帮助 #include <stdio.h> int main() { int i = 10; int j = 10; int a = 0; int b = 0; int array[100] = {0}; int divider = 10; int num; int great; i
#include <stdio.h>
int main()
{
int i = 10;
int j = 10;
int a = 0;
int b = 0;
int array[100] = {0};
int divider = 10;
int num;
int great;
int product;
int n;
int flag;
/*Loop through first 2 digit number and second 2 digit number*/
while (i<100)
{
while (j < 100)
{
product = i*j;
array [a] = product % 10;
n = product / divider;
while (n != 0)
{
a++;
num = n%10;
divider *=10;
array[a]=num;
n = product/divider;
}
flag = 0;
while (b<a)
{
if (array[b] != array[a])
{
flag = 1;
}
b++;
a--;
}
if (flag == 0)
{
great = product;
}
j++;
a = 0;
b = 0;
}
i++;
}
printf("The largest palindrome is %d \n", great);
return 0;
}
#包括
int main()
{
int i=10;
int j=10;
int a=0;
int b=0;
int数组[100]={0};
整数除法器=10;
int-num;
int伟大;
int产品;
int n;
int标志;
/*循环通过第一个2位数字和第二个2位数字*/
而(i似乎在循环开始时没有重新初始化变量。它们保留以前迭代中的值。例如,j
和除法器
j = 10;
在启动“j”循环之前,即:
j = 10;
while (j < 100) ...
下面是一段代码片段,您可以尝试
#include <stdio.h>
void main()
{
int a = 1; // first integer
int b = 1; // second integer
int currentNumber;
int currentPalin; if a palindrome is found, its stored here
while (a<100){ //loop through the first number
while (b<100){ // loop through the second number
currentNumber = a*b;
if (currentNumber == reverse(currentNumber) ){ //check for palindrome
currentPalin = currentNumber;
}
b = b+1; //increment the second number
}
b = a; // you could have set b=1 but it would not be an efficient algorithm because
//some of the multiplication would occur twice. eg- (54*60) and (60*54)
a = a +1; //increment the first number
}
printf ("Largest palindrom is %d \n", currentPalin);
getchar();
}
// method for finding out reverse
int reverse(int n){
int reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
// when you divide a number by 10, the
//remainder gives you the last digit. so you are reconstructing the
//digit from the last
n = n/10;
}
return reverse;
}
#包括
void main()
{
int a=1;//第一个整数
int b=1;//第二个整数
int-currentNumber;
int currentPalin;如果找到回文,则将其存储在此处
而(a是解决问题的另一种方法
#include<stdio.h>
int reverse(int num)
{
int result = 0;
while( num > 0)
{
result = result * 10 + (num%10);
num/=10;
}
return result;
}
int main()
{
int last_best = 1;
int best_i=1;
int best_j = 1;
const int max_value = 99;
for( int i = max_value ; i > 0 ; --i)
{
for(int j = i ; j > 0 ; --j){
int a = i * j;
if( last_best > a )
break;
else if ( a == reverse(a) )
{
last_best = a;
best_i = i;
best_j = j;
}
}
}
printf("%d and %d = %d\n", best_i,best_j,last_best);
}
#包括
反向整数(整数)
{
int结果=0;
while(num>0)
{
结果=结果*10+(数值%10);
num/=10;
}
返回结果;
}
int main()
{
int last_best=1;
int-best_i=1;
int-best_j=1;
const int max_值=99;
对于(int i=最大值;i>0;--i)
{
对于(int j=i;j>0;--j){
int a=i*j;
如果(最后一个最佳>a)
打破
如果(a==反向(a))
{
last_best=a;
最佳_i=i;
最佳_j=j;
}
}
}
printf(“%d和%d=%d\n”,最佳i,最佳j,最后最佳);
}
这很容易理解。如果你得到的不是回文的东西,你有没有试着集中在代码中检查数字是否是回文的部分?试着缩小你的问题范围…只是对代码的一个注释…而不是使用一个单一的代码块,把它分成函数…比如int is\u palindrome(int-in)
。只需对算法进行注释就更容易理解和调试…您正在尝试查找最大回文数…因此,最好从最高点开始检查乘积,并在找到第一个回文数时停止。现在,两位数小数的乘积回文将在le上讨论那么,关于标题中的3位数字
呢?为什么每次j都必须初始化为0?这是两个产品的数字之一。他可能想检查i
和j
的所有组合,而不仅仅是0
与所有j
对比,其余的i
与100
对比。两者都是d j在循环的底部递增。@wrangler在i
从10递增到11时,j
等于100,并且它永远不会重新初始化为10,因此i==11
的下一次迭代甚至不会进入内部循环体。您的算法遗漏了方块。(碰巧,给定范围内没有回文方块,但仍应检查它们。)您还假设最后一个回文是最高的回文,这在这里是正确的,因为其中一个因子是99,但是如果我们忽略99的乘积,则有97*55==5335
,它小于96*88==8448
,尽管它是在循环中稍后计算的。您确实需要检查乘积是否大于当前最佳匹配。另外,请将currentPalin
初始化为合适的值,以便您可以判断是否找到任何回文。谢谢!@MOehm,我已根据您的建议更新了答案。您的解决方案与phositronax的错误相同:您假设在您的解决方案中,回文最高的首先出现嵌套循环,但在一般情况下不一定如此。(在这里,因为其中一个因子是99。)
#include <stdio.h>
void main()
{
int a = 1; // first integer
int b = 1; // second integer
int currentNumber;
int currentPalin; if a palindrome is found, its stored here
while (a<100){ //loop through the first number
while (b<100){ // loop through the second number
currentNumber = a*b;
if (currentNumber == reverse(currentNumber) ){ //check for palindrome
currentPalin = currentNumber;
}
b = b+1; //increment the second number
}
b = a; // you could have set b=1 but it would not be an efficient algorithm because
//some of the multiplication would occur twice. eg- (54*60) and (60*54)
a = a +1; //increment the first number
}
printf ("Largest palindrom is %d \n", currentPalin);
getchar();
}
// method for finding out reverse
int reverse(int n){
int reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
// when you divide a number by 10, the
//remainder gives you the last digit. so you are reconstructing the
//digit from the last
n = n/10;
}
return reverse;
}
#include <stdio.h>
void main()
{
int a = 1;
int b = 1;
int currentNumber;
int currentPalin=0;
while (a<100){
while (b<100){
currentNumber = a*b;
if (currentNumber == reverse(currentNumber) ){
if (currentNumber>currentPalin){
currentPalin = currentNumber;
}
}
b = b+1;
}
b = 1;
a = a +1;
}
if (currentPalin==0){
printf("No Palindrome exits in this range");
}
else {
printf ("Largest palindrome is %d \n", currentPalin);
}
getchar();
}
int reverse(int n){
int reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
return reverse;
}
#include<stdio.h>
int reverse(int num)
{
int result = 0;
while( num > 0)
{
result = result * 10 + (num%10);
num/=10;
}
return result;
}
int main()
{
int last_best = 1;
int best_i=1;
int best_j = 1;
const int max_value = 99;
for( int i = max_value ; i > 0 ; --i)
{
for(int j = i ; j > 0 ; --j){
int a = i * j;
if( last_best > a )
break;
else if ( a == reverse(a) )
{
last_best = a;
best_i = i;
best_j = j;
}
}
}
printf("%d and %d = %d\n", best_i,best_j,last_best);
}