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

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
我的代码及注释:
 1 //判断是否为完全平方数
 2 int is1(int n)
 3 {
 4     if((sqrt(n)-(int)sqrt(n))==0)return 1;//sqrt(n)为c语言中开平方的函数,此函数的返回值是double型
 5     else return 0;
 6 }
 7 //判断是否有两位数字相同
 8 int is2(int n)
 9 {
10     int len=0;
11     len=(int)log10(n)+1;//利用c语言中10的对数加1来计算整数的数位
12     int a[len];//定义一个适当长度的数组,用来存放整数的各位数位
13     int i;
14     for(i=0;i<len;i++)
15     {
16         a[i]=n%10;//为数组赋值
17         n=n/10;
18     }
19     int p,q;//判断数组中是否存在至少两个相同的数字
20     int sign=0;
21     for(p=0;p<len;p++)
22     {
23         for(q=p+1;q<len;q++)
24         {
25             if(a[p]==a[q])
26             {
27                 sign=1;break;//若数组中有两个相同的数字,则更改sign的值,并中断循环
28             }
29         }
30     }
31     if(sign==0)return 0;
32     else return 1;
33 }
34
35
36 int IsTheNumber ( const int N )
37 {
38     if(is1(N) && is2(N))//判断条件
39         return 1;
40 }

原文地址:https://www.cnblogs.com/3111-Tomorrow/p/12241545.html

时间: 2024-08-28 17:36:54

pta 6-7 统计某类完全平方数 (20分)的相关文章

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

4-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 ma

PTA-C-4-7 统计某类完全平方数

4-7 统计某类完全平方数   (20分) 本题要求实现一个函数,判断任一给定整数N是否满足条件:它是完全平方数,又至少有两位数字相同,如144.676等. 函数接口定义: int IsTheNumber ( const int N ); 其中N是用户传入的参数.如果N满足条件,则该函数必须返回1,否则返回0. 裁判测试程序样例: #include #include... http://bbs.chinaacc.com/forum-2-3/topic-5829463.htmlhttp://bbs

1038 统计同成绩学生 (20 分)

题目:1038 统计同成绩学生 (20 分) 本题要求读入 N 名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式: 输入在第 1 行给出不超过 1 的正整数 N,即学生总人数.随后一行给出 N 名学生的百分制整数成绩,中间以空格分隔.最后一行给出要查询的分数个数 K(不超过 N 的正整数),随后是 K 个分数,中间以空格分隔. 输出格式: 在一行中按查询顺序给出得分等于指定分数的学生人数,中间以空格分隔,但行末不得有多余空格. 输入样例: 10 60 75 90 55 75 99 82

PTA 乙级 1017 A除以B (20 分) C/C++

1017 A除以B (20 分) 本题要求计算 A/B,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数.你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立. 输入格式: 输入在一行中依次给出 A 和 B,中间以 1 空格分隔. 输出格式: 在一行中依次输出 Q 和 R,中间以 1 空格分隔. 输入样例: 123456789050987654321 7 输出样例: 17636684150141093474 3 1 #include<stdio.h> 2 #include&

PTA乙级(1077 互评成绩计算 (20分),(C++取整)

C++取整 头文件:#include <cmath> 1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 int main() 5 { 6 double a=2.5; 7 cout<<ceil(a)<<endl; //向上取整 8 cout<<floor(a)<<endl; //向下取整 9 cout<<round(a)<<

PTA乙级 (1069 微博转发抽奖 (20分)(vector,map))

1069 微博转发抽奖 (20分) https://pintia.cn/problem-sets/994805260223102976/problems/994805265159798784 1.使用vector来保存输入的用户名. 2.使用map来进行筛选,记录用户是否已经中奖. 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #in

PTA乙级 (1018 锤子剪刀布 (20分))

1018 锤子剪刀布 (20分) https://pintia.cn/problem-sets/994805260223102976/problems/994805304020025344 #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> using names

PTA基础编程题目集6-7 统计某类完全平方数 (函数题)

本题要求实现一个函数,判断任一给定整数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,

4-7 统计某类完全平方数

本题要求实现一个函数,判断任一给定整数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,