统计某类完全平方数
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; }
相关文章推荐
- 6-7 统计某类完全平方数(20 分) 本题要求实现一个函数,判断任一给定整数N是否满足条件:它是完全平方数,又至少有两位数字相同,如144、676等。
- PAT-4-7 统计某类完全平方数 (20分)
- 4-7 统计某类完全平方数
- 基础编程题目集:6-7 统计某类完全平方数(20 分)
- 统计某类完全平方数
- 4-7 统计某类完全平方数 (20分)
- 4-7 统计某类完全平方数 (20分)
- 4-7 统计某类完全平方数
- 4-7 统计某类完全平方数 (20分)
- PTA 4-7 统计某类完全平方数
- PAT练习基础编程题目之统计某类完全平方数
- 4-7 统计某类完全平方数
- 6-1 统计某类完全平方数(20 分)
- (函数题)4-7 统计某类完全平方数
- 4-7 统计某类完全平方数 (20分)
- 4-7 统计某类完全平方数
- 4-7 统计某类完全平方数
- 4-7 统计某类完全平方数 (20分)
- 6-3 判断满足条件的三位数 本题要求实现一个函数,统计给定区间内的三位数中有两位数字相同的完全平方数(如144、676)的个数。
- 统计文件信息及生成SQL语句(VBS)实例