온라인 회상 검색 센서를 만들려고합니다 (알파벳은 0,1,2,3, ... 9로 구성됩니다). 코드는 다음과 같습니다 :온라인 회문 표시 만들기
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int x=0;
int y=0;
int c;
int i=0;
while(1)
{
cin>>c;
//I keep a track of previous number in x and its reverse in y and use them to create the
//the new number and reverse at every input. Then I compare x and y. If equal the number is
//a palindrome.
/*eg:(When 121 is entered digit by digit)
i=0:-
x=10*0+1 y=0+ 10^0 *1
i=1:-
x=10*1+2 y=1+ 10^1 *2
i=2:-
x=10*12+1 y=21+ 10^2 *1
*/
x=10*x+c;
y=y+ static_cast<int>(pow(10.0,static_cast<double>(i)) *c);
cout<<"y= "<<y<<" and "<<"x= "<<x<<endl;
if(y==x)
cout<<"Palindrome"<<endl;
i++;
}
return 0;
}
먼저 1을 입력하고 예상대로 (palindrome) 표시가되었습니다. 그런 다음 2를 입력했는데 아무런 반응이 없었습니다 (예상대로 'y= 21 and x= 12'
이 인쇄되었습니다). 그러나, 나는 다시 한 입력과도 아무 일도없는이 시간 (는을 예상하지)이 인쇄되었다
y= 120 and x= 121
사람이 말해 줄 수 , 어떻게 y는 거죠 (120)가 121으로 가정 할 때?
왜 이것을 권력으로 모으고 있습니까? – Woot4Moo
@ Woot4Moo ... 그것이 작동하는 방법이기 때문에 ... 121 = 10^2 * 1 + 21 – avinash