zoj 1078 palindrom numbers
生活随笔
收集整理的這篇文章主要介紹了
zoj 1078 palindrom numbers
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?題目見zoj 1078
?主要是判斷一個整數在基數為2-16之間的某個數字時是否為回文,我是直接該整數轉換成對應基數的表示的逆序列,并計算出該表示下的值,判斷是否等于這個整數值,如果相等,那么就是回文,如果不相等就不是。
?
/* zoj 1078 Palindrom Numbers */ #include <stdio.h>#define MAX 30int isPalindrom(int base, int decNum);int main(void) {int i,count;int n,baseStr[MAX];while(scanf("%d", &n) == 1 && n != 0){count = 0;for(i = 2; i <= 16; i++)if(isPalindrom(i, n))baseStr[count++] = i;if(count == 0)printf("Number %d is not a palindrom\n", n);else{printf("Number %d is palindrom in basis",n);for(i = 0; i < count; i++)printf(" %d",baseStr[i]);printf("\n");}}return 0; } int isPalindrom(int base, int decNum) {int revBaseStr[MAX];int i,count;int temp = decNum;for(count = 0; temp > 0; temp /= base)revBaseStr[count++] = temp%base;for(temp = 0, i = 0; i < count; i++)temp = temp * base + revBaseStr[i];if(temp == decNum)return 1;elsereturn 0; } ??
總結
以上是生活随笔為你收集整理的zoj 1078 palindrom numbers的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 汉诺塔——递归算法
- 下一篇: where、having、group b