4-7 统计某类完全平方数
2017-03-08 22:53
225 查看
本题要求实现一个函数,判断任一给定整数
其中
程序代码如下:
void getsite(int b, int array[], int *num)
{
int a = b;
int i = 0;
do
{
array[i] = a % 10;
a = (a - a % 10) / 10;
i++;
} while (a != 0);
*num = i;//注意此处,num是指针!
}
int test(int array[], int num)
{
for (int i = 0; i<num - 1; i++)
{
for (int j = i + 1; j<num; j++)
if (array[i] == array[j])
return 1;
}
return 0;
}
int IsTheNumber(const int N)
{
int i = 10;
int array[1000];
int num = 0;
int mag = 0;
while (i <= N / 2)
{
if (i*i == N)
{
getsite(N, array, &num);
return(test(array, num));
}
i++;
}
return 0;
}
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;
}
/* 你的代码将被嵌在这里 */
程序代码如下:
void getsite(int b, int array[], int *num)
{
int a = b;
int i = 0;
do
{
array[i] = a % 10;
a = (a - a % 10) / 10;
i++;
} while (a != 0);
*num = i;//注意此处,num是指针!
}
int test(int array[], int num)
{
for (int i = 0; i<num - 1; i++)
{
for (int j = i + 1; j<num; j++)
if (array[i] == array[j])
return 1;
}
return 0;
}
int IsTheNumber(const int N)
{
int i = 10;
int array[1000];
int num = 0;
int mag = 0;
while (i <= N / 2)
{
if (i*i == N)
{
getsite(N, array, &num);
return(test(array, num));
}
i++;
}
return 0;
}
相关文章推荐
- 6-3 判断满足条件的三位数 本题要求实现一个函数,统计给定区间内的三位数中有两位数字相同的完全平方数(如144、676)的个数。
- 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练习基础编程题目之统计某类完全平方数
- 6-1 统计某类完全平方数(20 分)
- 4-7 统计某类完全平方数 (20分)
- 4-7 统计某类完全平方数
- 4-7 统计某类完全平方数
- (函数题)4-7 统计某类完全平方数
- 4-7 统计某类完全平方数 (20分)
- ucos 统计任务 OS_TaskStat() 分类: μc /os ii 系统有关知识 2011-04-15 22:26 3818人阅读 评论(1) 收藏