Problem Statement -Wie kann ich dieses Programm für Eingaben> 10 für die USACO Training Pages Square Palindrome verwenden?
Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that the square of N is palindromic when expressed in base B; also print the value of that palindromic square. Use the letters 'A', 'B', and so on to represent the digits 10, 11, and so on.
Print both the number and its square in base B.
INPUT FORMAT
eine einzige Leitung mit B, wobei die Basis (in der Basis 10 angegeben).
Abtastwerteingang
10
OUTPUT FORMAT
Lines with two integers represented in base B. The first integer is the number whose square is palindromic; the second integer is the square itself. NOTE WELL THAT BOTH INTEGERS ARE IN BASE B!
SAMPLE OUTPUT
1 1
2 4
3 9
11 121
22 484
26 676
101 10201
111 12321
121 14641
202 40804
212 44944
264 69696
Mein Code für alle Eingänge arbeitet < = 10, aber mir einige seltsame Ausgang für Eingänge gibt > 10.
Mein Code-
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int baseToBase(int num, int base) //accepts a number in base 10 and the base to be converted into as arguments
{
int result=0, temp=0, i=1;
while(num>0)
{
result = result + (num%base)*pow(10, i);
i++;
num = num/base;
}
result/=10;
return result;
}
long long int isPalin(int n, int base) //checks the palindrome
{
long long int result=0, temp, num=n*n, x=n*n;
num = baseToBase(num, base);
x = baseToBase(x, base);
while(num)
{
temp=num%10;
result = result*10 + temp;
num/=10;
}
if(x==result)
return x;
else
return 0;
}
int main()
{
int base, i, temp;
long long int sq;
cin >> base;
for(i=1; i<=300; i++)
{
temp=baseToBase(i, base);
sq=isPalin(i, base);
if(sq!=0)
cout << temp << " " << sq << endl;
}
return 0;
}
Für die Eingabe = 11, sollte die Antwort
1 1
2 4
3 9
6 33
11 121
22 484
24 565
66 3993
77 5335
101 10201
111 12321
121 14641
202 40804
212 44944
234 53535
sein Während meine Antwort ist
1 1
2 4
3 9
6 33
11 121
22 484
24 565
66 3993
77 5335
110 10901
101 10201
111 12321
121 14641
209 40304
202 40804
212 44944
227 50205
234 53535
Es gibt einen Unterschied in meine Ausgabe und die benötigt man als 202 zeigt unter 209 und 110 zeigt vor 101.
Hilfe dankbar, danke!
können Sie das Problem Link? Hier drücken Sie den Wert 'temp', der den Wert nach der Konvertierung in die Basis 'B' angibt. Und Ihre Basisumrechnung ist nicht korrekt. Ich vermute, Ursache für "i = 240" ist Ihre berechnete Temperatur 209, während für "i = 244" Temperatur 202 für "B = 11" ist. Deshalb druckt es zuerst 209 und dann 202. –