本題要求實(shí)現(xiàn)一個函數(shù),判斷任一給定整數(shù)N
是否滿足條件:它是完全平方數(shù),又至少有兩位數(shù)字相同,如144、676等。
int IsTheNumber ( const int N );其中N
是用戶傳入的參數(shù)。如果N
滿足條件,則該函數(shù)必須返回1,否則返回0。
裁判測試程序樣例:
#include <stdio.h>#include <math.h>int IsTheNumber ( const int N );int main(){ int n1, n2, i, cnt; scanf("%d %d", &n1, &n2); cnt = 0; for ( i=n1; i<=n2; i++ ) { if ( IsTheNumber(i) ) cnt++; } PRintf("cnt = %d/n", cnt); return 0;}/* 你的代碼將被嵌在這里 */輸入樣例:
105 500輸出樣例:
cnt = 6int IsTheNumber ( const int N ){ int n,m,i; int ret; int a[10]={0}; n=N; ret=0; /*判斷是否是完全平方數(shù)*/ m=sqrt(n); if (m*m==n) ret=1; else return 0; /*若為完全平方數(shù),則統(tǒng)計輸入整數(shù)中0-9的個數(shù)*/ while(n!=0) { i=n%10; a[i]++; n=n/10; } for(i=0;i<10;i++){ if(a[i]>=2) { ret=1; break; } else ret=0; } return ret;}
新聞熱點(diǎn)
疑難解答