dualpalindromes - codetrick
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):
Solutions to this problem do not require manipulating integers larger than the standard 32 bits.
http://mangogao.com/read/68.html
Read full article from dualpalindromes - codetrick
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)
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 28http://code.antonio081014.com/2012/09/usaco-dual-palindromes.html
public void solve() throws Exception { BufferedReader br = new BufferedReader(new FileReader("dualpal.in")); BufferedWriter out = new BufferedWriter(new FileWriter("dualpal.out")); String[] str = br.readLine().split("\\s"); int N = Integer.parseInt(str[0]); int S = Integer.parseInt(str[1]); int count = 0; for (S++; S <= Integer.MAX_VALUE; S++) { if (isValid(S)) { out.write("" + S + "\n"); count++; } if (count >= N) break; } out.close(); } public boolean isValid(int a) { int count = 0; for (int base = 2; base <= 10; base++) { if (isPalindrome(Integer.toString(a, base))) count++; } return count >= 2 ? true : false; } public boolean isPalindrome(String a) { for (int i = 0; i < a.length() / 2; i++) if (a.charAt(i) != a.charAt(a.length() - i - 1)) return false; return true; }http://bbsunchen.iteye.com/blog/1852036
- string changeBase(int formerNum, int baseNum)
- {
- string latterNumString;
- while(formerNum)
- {
- char c;
- int num = formerNum % baseNum;
- if(num < 10)
- {
- c = (char)(num+48);
- }else
- {
- c = (char)(num+55);
- }
- latterNumString.insert(latterNumString.begin(),c);
- formerNum = (int)(formerNum / baseNum);
- }
- return latterNumString;
- }
http://mangogao.com/read/68.html
const
char
key[]=
"0123456789"
;
const
int
MaxLenth=16
int
main()
{
ifstream fin(
"dualpal.in"
);
ofstream fout(
"dualpal.out"
);
int
B,N,S,num,k,kk,p;
char
s[MaxLenth];
fin >> N >> S;
while
(S++ && N)
{
p=0;
//对于每个S,求p个回文
B=10;
while
(B>1 && p<2)
{
num=S;
//转换进制
k=0;
while
(num>0)
{
s[++k]=key[num%B];
num/=B;
}
//判断回文
kk=k;
while
(k && s[k]==s[kk-k+1]) k--;
if
(k==0) p++;
B--;
}
if
(p==2)
{
fout << S << endl;
N--;
}
}
return
0;
}