6-7 統計某類完全平方數 (20分)
本題要求實現一個函數,判斷任一給定整數N
是否滿足條件:它是完全平方數,又至少有兩位數字相同,如144、676等。
函數接口定義:
int IsTheNumber ( const int N );
其中N
是用戶傳入的參數。如果N
滿足條件,則該函數必須返回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 = 6
#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; } /* 你的代碼將被嵌在這里 */ int IsTheNumber ( const int N ) { int p=N; int num[10]={0}; int n=sqrt(N); if(n*n==N) { while(p!=0) { num[p%10]++;//奇妙小統計 p=p/10; } for(int i=0;i<10;i++) { if(num[i]>=2)//只要碰到有重復的,所屬位置必定大於等於2 { return 1; } } return 0; } else{ return 0; } }