English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

題目:A palindromic number is a 'symmetrical' number like 16461 or 158851, thatremains the same when its digits are reversed.Write to Boolean function to determine if a given number is a palindromicnumber. It is is, return true; otherwise return false.[Demand]Write a function called onesDigit() to return the ones digit of a givennumber.Write a function called highestDigit() to return the highest digit of a givennumber.Call these functions to determine the palindromic numbers.我的程式碼:#include int  onesDigit(int a){        a=a%10;        return a;}int highestDigit(int a,int i){        a=a/i;        return a;}bool answer(int a){        int i;        for(i=1;!(a>=i && a<(i*10));i = i*10){}        int b=highestDigit(a,i);        int c=onesDigit(a);        if (b==c){                if(i==1||i==10)                        return 1;                a=(a-(b*i))/10;                 answer(a);        }        return 0;}void main (){        int a;        printf("請輸入一個數字:");        scanf("%d",&a);        if (answer(a))                printf("true\\n");        else                printf("false\\n");}只有兩位數以下才會正確其餘不管怎麼輸入都是false該怎麼改呢?

2006-11-24 06:39:13 · 1 個解答 · 發問者 柳子 5 in 電腦與網際網路 程式設計

1 個解答

//Power by Microsoft Visual Studio 2005//可以使用 Dev-C++ 編譯此程式#include#include#includeusing namespace std;int main(int argc,char **argv){ //=====START=====// //題目:迴文 //比較數值右方與左方的位數是否相同 bool fun_ANS(int NUMBER); int number; cout<<"Input a number: "; cin>>number; cout<<"It\'s "<(counter/2); for(i=0,temp=NUMBER,counter--;i(pow(10.0,counter))+getOnesDigit;  temp/=10;//降位數  counter-=2; }//迴圈比較數值 return bFLAG;}//回傳布林值int onesDigit(int NUM){ return NUM%10;}//取低位數值int highestDigit(int NUM){ int quotient,remainder; quotient=NUM; while(quotient!=0){  remainder=quotient%10;  quotient/=10; } return remainder;}//取高位數值

2006-11-26 00:39:32 補充:
題目:迴文,如 16461 或 158851 高位數與低位數的數值相同且兩兩對稱。試寫一個布林型態的函式,假如數值是迴文,傳回值為 true,如果不是迴文,傳回值為 false。[條件]試寫函式名為 onesDigit 計算並傳回數值的低位數。試寫函式名為 highestDigit 計算並傳回數值的高位數。呼叫上述函式來決定數值是否為迴文。

2006-11-26 00:42:30 補充:
因為你的函式只能計算到兩個位數,最終必須要做大幅度的修改,所以我重寫原始碼。

2006-11-25 19:22:19 · answer #1 · answered by Big_John-tw 7 · 0 0

fedest.com, questions and answers