本題地址: http://www.luogu.org/problem/show?pid=1217
題目描述
因爲151既是一個質數又是一個迴文數(從左到右和從右到左是看一樣的),所以 151 是迴文質數。寫一個程序來找出範圍[a,b](5 <= a < b <= 100,000,000)( 一億)間的所有迴文質數;
輸入輸出格式
輸入格式:第 1 行: 二個整數 a 和 b .
輸出格式:輸出一個迴文質數的列表,一行一個。
輸入輸出樣例
輸入樣例#1:5 500輸出樣例#1:
5 7 11 101 131 151 181 191 313 353 373 383
說明
Hint 1: Generate the palindromes and see if they are prime.提示 1: 找出所有的迴文數再判斷它們是不是質數(素數).
Hint 2: Generate palindromes by combining digits properly. You might need more than one of the loops like below.
提示 2: 要產生正確的迴文數,你可能需要幾個像下面這樣的循環。
題目翻譯來自NOCOW。
USACO Training Section 1.5
產生長度爲5的迴文數:
for (d1 = 1; d1 <= 9; d1+=2) { // 只有奇數纔會是素數
for (d2 = 0; d2 <= 9; d2++) {
for (d3 = 0; d3 <= 9; d3++) {
palindrome = 10000*d1 + 1000*d2 +100*d3 + 10*d2 + d1;//(處理迴文數...)
}
}
}