本題要求實現(xiàn)一個函數(shù)测暗,判斷任一給定整數(shù)N是否滿足條件:它是完全平方數(shù)馁害,又至少有兩位數(shù)字相同,如144脱货、676等。
函數(shù)接口定義:
int IsTheNumber(constintN );
裁判測試樣例:
#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 = 6
接口函數(shù)代碼:
int IsTheNumber ( const int N )
{
int n=N;//后續(xù)操作會用到變化的N律姨,需用一個變量存儲
int a[10]={0};//保存從N分離出的各個位數(shù)
int d;//N各個位分離的數(shù)字
int k;//開平方后的N
k=sqrt(N);
while(n!=0)
{
d=n%10;//取余分離N
if((k*k==N)&&(a[d]==1))//如果有以前相同的數(shù)字d振峻,則滿足完全平方數(shù)
return 1;
else
a[d]=1;
n=n/10;//下一位
}
return 0;
}