All pastes #640223 Raw Edit

Something

public text v1 · immutable
#640223 ·published 2007-07-31 06:47 UTC
rendered paste body




















 A number that reads the same from right to left as when read from left to right is called a palindrome. The number 12321 is a palindrome; the number 77778 is not. Of course, palindromes have neither leading nor trailing zeroes, so 0220 is not a palindrome.

The number 21 (base 10) is not palindrome in base 10, but the number 21 (base 10) is, in fact, a palindrome in base 2 (10101).

Write a program that reads two numbers (expressed in base 10):

    * N (1 <= N <= 15)
    * S (0 < S < 10000) 

and then finds and prints (in base 10) the first N numbers strictly greater than S that are palindromic when written in two or more number bases (2 <= base <= 10).

Solutions to this problem do not require manipulating integers larger than the standard 32 bits.
PROGRAM NAME: dualpal
INPUT FORMAT

A single line with space separated integers N and S.
SAMPLE INPUT (file dualpal.in)

3 25

OUTPUT FORMAT
N lines, each with a base 10 number that is palindromic when expressed in at least two of the bases 2..10. The numbers should be listed in order from smallest to largest.
SAMPLE OUTPUT (file dualpal.out)

26
27












#include<string>
#include<iostream>
#include<vector>
using namespace std;

static int baseb=10,countb=0;
static bool check;
bool palin(int);int base(int,int);

int main()
{
	int n,s,count=0;

	cin>>n;cin>>s;

	//check for palindromes from s onwards
	for(int i=s;i<10000;i++)
	{
		if(count==(n-1))
		return(0);

		countb=0;check=false;baseb=10;

		if(palin(i))
		{
			cout<<s<<endl;
			count++;
		}
	}

}

bool palin(int no)
{
	char temp[5];string str;int i,j;bool flag=true;

	//convert no to string
	sprintf(temp,"%d",no); str=(string)temp; 
	
	//check for palindrome
	for(i=0,j=str.size()-1;i<str.size();i++,j--)
	{
		if(str[i]!=str[j])
		{ flag=false;break;}
	}

	/*cout<<"no "<<no<<"check "<<check<<"flag "<<flag<<"countb "<<countb<<"base "<<baseb<<endl;*/
	//if palindrome and count==2 return true for the no
	if(flag==true)
	{ 
	countb++;
	if(countb==1)
	return(true);
	}
	else if(check==false);
	{
		check=true;
		for(int i=9;i>=2;i--)
		{
			bool x;baseb=i;
			x=palin(base(no,i));
		}
	}

	
		
	return(false);

}

int base(int no,int i)
{
	cout<<"no "<<no;
	string temp; int ret;char x;
	while(no!=0)
	{
		cout<<no%i<<endl;
		x=no%i+'0';
		temp=temp+x;
		no=no/i;
	
	}
	
	reverse(temp.begin(),temp.end());
	sscanf(temp.c_str(),"%d",&ret);
	/*cout<<"i "<<i<<"temp "<<temp<<endl;*/
	return(ret);
}