8.2 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, 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

 代码:

int IsTheNumber(const int N)
{
    int x = sqrt(N);
    int n = N, flag = 0;
    if (x * x == n)
    {
        if (n > 0)
        {
            int arr[10], i = 0;
            while (n)
            {
                arr[i++] = n % 10;
                n /= 10;
            }
            for (int j = 0; j < i; j++)
            {
                for (int m = j + 1; m < i; m++)
                {
                    if (arr[j] == arr[m])
                        flag = 1;
                }
            }
        }
        return flag;
    }
    else
        return 0;
}

(注意m=j+1) 

6-9 统计个位数字

本题要求实现一个函数,可统计任一整数中某个位数出现的次数。例如-21252中,2出现了3次,则该函数应该返回3。

函数接口定义:

int Count_Digit ( const int N, const int D );

其中ND都是用户传入的参数。N的值不超过int的范围;D是[0, 9]区间内的个位数。函数须返回ND出现的次数。

裁判测试程序样例:

#include <stdio.h>

int Count_Digit ( const int N, const int D );

int main()
{
    int N, D;
    
    scanf("%d %d", &N, &D);
    printf("%d\n", Count_Digit(N, D));
    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

-21252 2

输出样例:

3

代码: 

int Count_Digit ( const int N, const int D )
{
    int sum=0;
    int n=N;
    if(n==0)
        return 1;
    else if(n<0)
        n=-n;
    while(n)
    {
        int m=n%10;
        if(m==D)
            sum++;
        n/=10;
    }
    return sum;
}

(注意是const int N) 

6-10 阶乘计算升级版 

 

本题要求实现一个打印非负整数阶乘的函数。

函数接口定义:

void Print_Factorial ( const int N );

其中N是用户传入的参数,其值不超过1000。如果N是非负整数,则该函数必须在一行中打印出N!的值,否则打印“Invalid input”。

裁判测试程序样例:

#include <stdio.h>

void Print_Factorial ( const int N );

int main()
{
    int N;
    
    scanf("%d", &N);
    Print_Factorial(N);
    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

15

输出样例:

1307674368000

 代码:

void Print_Factorial(const int N)
{
    if (N < 0)
    {
        printf("Invalid input");
    }
    else 
    {
        int arr[3000], i = 0, j = 0, carry = 0;
        arr[0] = 1;
        for (int i = 2; i <= N; i++)
        {
            for (j = 0; j <= l; j++)
            {
                int tmp = arr[j] * i + carry;
                arr[j] = tmp % 10;
                carry = tmp / 10;
            }
            while (carry)
            {
                arr[j++] = carry % 10;
                carry /= 10;
                l++;
            }
        }
        for (l; l >= 0; l--)
        {
            printf("%d", arr[l]);
        }
    }
}

(思路) 

6-12 判断奇偶性 

本题要求实现判断给定整数奇偶性的函数。

函数接口定义:

int even( int n );

其中n是用户传入的整型参数。当n为偶数时,函数返回1;n为奇数时返回0。注意:0是偶数。

裁判测试程序样例:

#include <stdio.h>

int even( int n );

int main()
{    
    int n;

    scanf("%d", &n);
    if (even(n))
        printf("%d is even.\n", n);
    else
        printf("%d is odd.\n", n);
    
    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例1:

-6

输出样例1:

-6 is even.

输入样例2:

5

输出样例2:

5 is odd.

代码:

int even( int n )
{
    if(n<0)
        n=-n;
    if(n==0)
        return 1;
    else
    {
        if(n%2==0)
            return 1;
        else return 0;
    }
}

(不能用else if,因为会判定为一个if里面,不执行下面的了) 

 

 

  • 26
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值