您的位置:首页 > 编程语言 > C语言/C++

统计某类完全平方数

2017-01-10 17:34 239 查看
本题要求实现一个函数,判断任一给定整数
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



输入样例:

105 500


输出样例:

cnt = 6


#include "stdafx.h"
#include <stdio.h>
#include <math.h>

int IsTheNumber(const int N);
int _tmain(int argc, _TCHAR* argv[])
{
int n1, n2, i, cnt=0;
scanf_s("%d %d", &n1, &n2);
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,M;
//对N开平方后取整,再用两个整数相乘与N对比
//来判定得到的开平方是否为整数
n =(int)sqrt(1.0*N);
M = n * n;
if (M == N)  //因为N为常量int型 故用M作为判定
{
int num[10] = { 0 };
while (M > 0)
{
m = M % 10;
num[m] += 1; //将得到的余数对应的组数加1;
if (num[m] == 2)
{
return 1;
}
M = M / 10;
}
return 0;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  函数 C语言