跳转至

Contest 10

判断满足条件的三位数
Description
本题要求实现一个函数,统计给定区间内的三位数中有两位数字相同的完全平方数(如144、676)的个数。
函数接口定义:
int search( int n );
其中传入的参数int n是一个三位数的正整数(最高位数字非0)。函数search返回[101, n]区间内所有满足条件的数的个数。
Input
一行一个正整数n。(101<=n<=999)
Output
一个整数表示答案。
Sample Input 1 
500
Sample Output 1
6
#include <stdio.h>
int search(int n);
int square(int n);
int same(int n);
int main()
{
    int n,count;
    scanf("%d",&n);
    count=search(n);
    printf("%d",count);
    return 0;

}
int search(int n)
{
    int i,a=0,b=0,count=0;
    for (i = 101; i <=n; i++)
    {
        a=square(i);
        b=same(i);
        if (a==1&&b==1)
        {
            count++;
        }  
    }
    return count;

}
int square(int n)
{
    int i=1,flag=0;
    for ( i = 1; i*i<=n; i++)
    {
        if (n==i*i)
        {
            flag=1;
            break;
        }

    }
    return flag;

}
int same(int n)
{
    int a,flag=0,i=0,temp=n;
    int sum[10]={0,0,0,0,0,0,0,0,0,0};
    for ( i = 0; i < 3; i++)
    {
        a=temp%10;
        sum[a]++;
        temp=temp/10;
    }
    for ( i = 0; i < 10; i++)
    {
        if (sum[i]>=2)
        {
            flag=1;
            break;
        }

    }
    return flag;

}
递归求阶乘和
Description
本题要求实现一个计算非负整数阶乘的简单函数,并利用该函数求1!+2!+3!+...+n!的值。
函数接口定义:
double fact( int n );
double factsum( int n );
函数fact应返回n的阶乘,建议用递归实现。函数factsum应返回1!+2!+...+n!的值。题目保证输入输出在双精度范围内。
Input
一行一个非负整数n。(n >= 0)
Output
第一行输出fact(n)的值。
第二行输出factsum(n)的值。(两个数均保留0位小数)
Sample Input 1 
10
Sample Output 1
3628800
4037913
#include <stdio.h>
double fact( int n );
double factsum( int n );
int main()
{
    int n;
    scanf("%d",&n);
    printf("%.0f\n",fact(n));
    printf("%.0f\n",factsum(n));
    return 0;

}
double fact( int n )
{
    double result;
    if (n==0||n==1)
    {
        result=1;
    }
    else
    {
        result=n*fact(n-1);
    }
    return result;    
}
double factsum( int n )
{
    int sum=0,i;
    for ( i = 1; i <=n ; i++)
    {
        sum+=fact(i);
    }
    return sum;
}
递归实现指数函数
Description
本题要求实现一个计算x^n(n≥1)的函数。
函数接口定义:
double calc_pow( double x, int n );
函数calc_pow应返回x的n次幂的值。建议用递归实现。题目保证结果在双精度范围内。
Input
一行两个数,实数x和整数n。(n>=1)
Output
一行一个数表示答案。(保留0位小数)
Sample Input 1 
2 3
Sample Output 1
8
#include <stdio.h>
double calc_pow( double x, int n );
int main()
{
    double x;
    int n;
    scanf("%lf%d",&x,&n);
    printf("%.0f",calc_pow(x,n));
    return 0;

}
double calc_pow( double x, int n )
{
    double result=0;
    if (n==0)
    {
        result=1;
    }
    else
    {
        result=x*calc_pow(x,n-1);
    }
    return result;

}
递归求简单交错幂级数的部分和
Description
本题要求实现一个函数,计算下列简单交错幂级数的部分和:
f(x,n)=x−x^2+x^3−x^4+⋯+(−1)^(n−1)*x^n
函数接口定义:
double fn( double x, int n );
其中题目保证传入的n是正整数,并且输入输出都在双精度范围内。函数fn应返回上述级数的部分和。建议尝试用递归实现。
Input
一行两个数,实数x和正整数n。
Output
一行一个数表示fn(x,n)的值。(保留两位小数)
Sample Input 1 
0.5 12
Sample Output 1
0.33
#include <stdio.h>
double fn( double x, int n );
int main()
{
    double x;
    int n;
    scanf("%lf%d",&x,&n);
    printf("%.2f",fn(x,n));
    return 0;
}
double fn( double x, int n )
{
    double result=0;
    if (n==1)
    {
        result=x;
    }
    else
    {
        result=pow(-1,n-1)*pow(x,n)+fn(x,n-1);
    }
    return result;
}
递归计算Ackermenn函数
Description
本题要求实现Ackermenn函数的计算,其函数定义如下:
图片.png
函数接口定义:
int Ack( int m, int n );
其中m和n是用户传入的非负整数。函数Ack返回Ackermenn函数的相应值。题目保证输入输出都在长整型
Input
一行两个非负整数m和n。
Output
一行一个数表示Ack(m,n)的答案。
Sample Input 1 
2 3
Sample Output 1
9
#include <stdio.h>
int Ack( int m, int n );
int main()
{
    int m,n;
    scanf("%d%d",&m,&n);
    printf("%d",Ack(m,n));
    return 0;

}
int Ack( int m, int n )
{
    int result=0;
    if (m==0)
    {
        result=n+1;
    }
    else if (m>0)
    {
        if (n==0)
        {
            result=Ack(m-1,1);
        }
        if (n>0)
        {
            result=Ack(m-1,Ack(m,n-1));
        }

    }
    return result;
}
递归求Fabonacci数列
Description
本题要求实现求Fabonacci数列项的函数。Fabonacci数列的定义如下:
f(n)=f(n−2)+f(n−1) (n≥2),其中f(0)=0,f(1)=1。
函数接口定义:
int f( int n );
函数f应返回第n个Fabonacci数。题目保证输入输出在长整型范围内。建议用递归实现。
Input
一行一个非负整数n。
Output
一行一个数表示f(n)的值。
Sample Input 1 
6
Sample Output 1
8
#include <stdio.h>
int f( int n );
int main()
{
    int n;
    scanf("%d",&n);
    printf("%d",f(n));
    return 0;
}
int f( int n )
{
    if (n==0)
    {
        return 0;
    }
    else if (n==1)
    {
        return 1;
    }
    return f(n-1)+f(n-2);

}
十进制转换二进制
Description
本题要求实现一个函数,将自然数n转换为二进制后输出。
函数接口定义:
void dectobin( int n );
函数dectobin应在一行中打印出二进制的n。建议用递归实现。
Input
一行一个自然数n。
Output
一行一个字符串表示二进制的n。
Sample Input 1 
10
Sample Output 1
1010
#include<stdio.h>
void dectobin(int n);
int main()
{
    int n;
    scanf("%d", &n);
    dectobin(n);
    return 0;
}
void dectobin(int n)
{
    if(n < 2 && n>=0)
    {
        printf("%d", n);
        return ;
    }

    dectobin(n/2);
    printf("%d", n%2);

}
递归实现顺序输出整数
Description
本题要求实现一个函数,对一个整数进行按位顺序输出。
函数接口定义:
void printdigits( int n );
函数printdigits应将n的每一位数字从高位到低位顺序打印出来,每位数字占一行。
Input
一行一个整数n。
Output
输出若干行,每行一个数字。
Sample Input 1 
12345
Sample Output 1
1
2
3
4
5
#include<stdio.h>
void printdigits( int n );
int main()
{
    int n;
    scanf("%d", &n);
    printdigits(n);
    return 0;
}
void printdigits( int n )
{

    if(n < 10)
    {
        printf("%d\n", n);
        return ;
    }

    printdigits(n/10);
    printf("%d\n", n%10);

}
递归统计素数个数
Description
输入n(n<10)个整数,统计其中素数的个数。
函数接口定义:
int countPrime( int n );
函数countPrime应返回输入前n个数中素数个数。
Input
第一行一个正整数n。(1<=n<10)
第二行n个正整数a[1..n]。(1<=a[i]<=1000)
Output
一行一个整数表示答案。
Sample Input 1 
3
2 3 101
Sample Output 1
3
#include <stdio.h>
int countPrime( int n,int *a );
int main()
{
    int n;
    scanf("%d",&n);
    int l=0;
    int a[n];
    for (l = 0; l < n; l++)
    {
        scanf("%d",&a[l]);
    }
    printf("%d",countPrime(n-1,a));
    return 0;    
}
int countPrime( int n,int *a )
{
    int count=0;
    if (a[n]==1)
        {
            count=count+0;
        }
        else if (a[n]==2)
        {
            count++ ;
        }
        else
        {
            int k=0;
            for ( k = 2; k <=a[n]/2; k++)
            {
                if (a[n]%k==0)
                {
                    break;
                }

            }
            if (k>a[n]/2)
            {
                count++;
            }
        }    
    if (n==0)
    {
        return count;
    }
    count=count+countPrime(n-1,a);
    return count;
}
三角形面积
Description
三角形面积为
图片.png
其中a、b、c分别是三角形的3条边。请分别定义计算s和area的宏,再使用函数实现。比较两者在形式上和使用上的区别。
Input
一行三个整数分别表示三角形的3条边。
Output
一行一个实数表示三角形的面积。(保留两位小数)
Sample Input 1 
3 4 5
Sample Output 1
6.00
#include <stdio.h>
#include <math.h>
#define s(a,b,c) (a+b+c)/2.0
#define area(s,a,b,c) sqrt(s*(s-a)*(s-b)*(s-c))
int main()
{
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    double s,area;
    s=s(a,b,c);
    area=area(s,a,b,c);
    printf("%.2f",area);
    return 0;
}