4-7 统计某类完全平方数 (20分)

来源:互联网 发布:可可英语好用吗 知乎 编辑:程序博客网 时间:2024/04/29 12:48

本题要求实现一个函数,判断任一给定整数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 n,m;n=(int)sqrt(N);m=n*n;if(m==N){int num[10]={0};int i;while(m>0){for(i=0;i<=9;i++){if(m%10==i){num[i]+=1; if(num[i]==2){return 1;}} }m=m/10;}return 0;}return 0;}


0 0
原创粉丝点击