Zoj Problem set-1078_ palindrome

Source: Internet
Author: User
Tags integer numbers

"Simple Question"

Topic

Palindrom Numbers time Limit:2 Seconds Memory limit:65536 KB Statement of the Problem

We say that's a number is a palindrom if it was the sane when read from left to right. For example, the number 75457 is a palindrom.

Of course, the property depends on the basis in which is a number is represented. The ' is ' not ' a palindrom in base, but it representation in base 2 (10001) is a palindrom.

The objective of this problem be to verify if a set of given numbers are palindroms into any basis from 2 to 16.

Input Format

Several integer numbers comprise the input. Each number 0 < n < 50000 are given in decimal basis in a separate line. The input ends with a zero.

Output Format

Your program must print the "message number I" is palindrom where I are the given number, followed by the basis wher E The representation of the number is a palindrom. If The number is isn't a palindrom in any basis between 2 and your program must print the number I am not palind Rom.

Sample Input

17
19
0

Sample Output

Number is palindrom on basis 2 4 16
Number is not a palindrom
Source:south America 2001

"Instructions"

Defines a number as a palindrome number that reads from left to right and reads from right to left, such as 75457 is a palindrome number. And some numbers in the 10 is not a palindrome, and in other systems is a palindrome, such as 17 in the 10 system is not a palindrome, but in 2 in the system of the value of 10001 is a palindrome. The topic gives a number of sets of test cases, each group a 10 number, the output of the number in the system (2~16 in the decision can) is a palindrome or in arbitrary into the system is not a palindrome.

Troubleshooting

(a) Analysis: Just try to 2~16 these 15 kinds of system, calculate the given 10 numbers in each system under the numerical judgement is a palindrome.

(ii) Code:

#include <iostream> using namespace std;
	int main () {int n,ntemp,i,basis,k;
	The int res[20],reslen,j,temp;//res stores the numeric strings under each system; Reslen is the string length bool Flag,sflag=false;
		The int m[15];//m array is stored as the binary while (cin>>n) {if (n==0) break of the palindrome numeric string;
		k=0;
			sflag=false;//Initial set Number n arbitrary number of strings are not palindrome//from 2 to 16 in the loop to determine the number of the system under the palindrome for (basis=2;basis<=16;basis++) {ntemp=n;
			j=0;
				Generates a digital string res while (ntemp!=0) {res[j++]=ntemp%basis) under each system;
			Ntemp/=basis;
			flag=true;//assumes that the binary number is a palindrome reslen=j-1;
			TEMP=RESLEN/2; for (j=0;j<=temp;j++) {if (Res[j]!=res[reslen-j]) {//From the beginning and end digits of res to the middle push the res is not a palindrome if the number that corresponds to the position is found to be unequal FLAG=FA
					Lse
				Break
				} if (flag==true)//Once a string of binary digits is a palindrome, the feed is recorded to M array {sflag=true;
			M[k++]=basis;
			} if (sflag==true) {cout<< "number" <<n<< "is palindrom on basis";
			for (j=0;j<k-1;j++) cout<<m[j]<< ';
			
		cout<<m[k-1]<<endl; } if (Sflag==false) cout<< "
	Number ' <<n<< ' is not a palindrom ' <<endl;
return 0; }//accepted

(Solution to 2009/10)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.