hero-image
Last updated on

2022级《程序设计基础I》实验7-函数的应用


计算表达式

problemId:1199

Description

计算下列表达式值: 

 

Input

输入x和n的值,其中x为非负实数,n为正整数。

Ouput

输出f(x,n),保留2位小数。

samples

<input>—> 3 2 <output>—> 2.00

Code

#include <stdio.h>
#include <math.h>
double f(double x,int n);
int main(){
    int n;
    double x;
    scanf("%lf %d",&x,&n);
    printf("%.2lf",f(x,n));
    return 0;
}
double f(double x,int n){
    if (n>0)
        return sqrt(n+f(x,--n));
    else if (n==0)
        return x;
    
}

求数列的和

problemId:1238

Description

数列的定义如下: 数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。

Input

输入数据有多组,每组占一行,由两个整数n(n< 10000)和m(m< 1000)组成,n和m的含义如前所述。

Ouput

对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。

samples

<input>—> 81 4 2 2 <output>—> 94.73 3.41

Code

#include <stdio.h>
#include <math.h>

double s(double n, int m);

int main()
{
    int n, m;
    double sum;
    while (scanf("%d %d", &n, &m) != EOF)
    {
        sum = s((double)n, m);
        printf("%.2lf\n", sum);
    }

    return 0;
}

double s(double n, int m)
{
    if (m == 0)
    {
        return 0.0;
    }
    else
    {
        return n + s(sqrt(n), m - 1);
    }
}

一元二次方程Ⅰ

problemId:1161

Description

解一元二次方程 $ax ^ 2+bx+c=0$ 的解。保证有解

### Input

$a,b,c$ 的值。

### Ouput

输出两个根 $x_1$ 和 $x_2$,其中 $x_1 \ge x_2$

结果保留两位小数。

### samples \--> 1 5 -2 \--> 0.37 -5.37 ### Code ``` #include #include int main() { double a,b,c,x,x1,x2; scanf("%lf%lf%lf",&a,&b,&c); x=pow(b,2)-4*a*c; x=sqrt(x); x1=(-b+x)/2/a; x2=(-b-x)/2/a; if(x1>x2) printf("%.2lf %.2lf",x1,x2); else printf("%.2lf %.2lf",x2,x1); return 0; } ``` # 求三角形面积 problemId:1206 ### Description

已知三角形的边长a、b和c,求其面积。

Input

输入三边a、b、c。

Ouput

输出面积,保留3位小数。

samples

<input>—> 1 2 2.5

<output>—> 0.950

Code

#include<stdio.h>
#include<math.h>
int main()
{
    double a,b,c,p,s;
    scanf("%lf %lf %lf",&a,&b,&c);
    p=(a+b+c)/2;
    s=(p*(p-a)*(p-b)*(p-c));
    s=sqrt(s);
    printf("%.3lf",s);
    return 0;
}

求实数绝对值

problemId:1209

Description

求实数的绝对值。

Input

输入数据有多组,每组占一行,每行包含一个实数。输入文件直到EOF为止!

Ouput

对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。

samples

<input>—> 123 -234.00 <output>—> 123.00 234.00

Code

#include <stdio.h>
 
 
int main(){
	double number;
	while((scanf("%lf",&number)!=EOF)){
		if(number<0)
			number=-number;
		printf("%.2lf\n",number);
	}
	return 0;
}

C/C++程序训练6---歌德巴赫猜想的证明

problemId:1136

Description

验证“每个不小于6的偶数都是两个素数之和”,输入一个不小于6的偶数n,找出两个素数,使它们的和为n。

Input

输入一个不小于6的偶数n。

Ouput

找出两个素数,使它们的和为n。只需要输出其中第一个素数最小的一组数据即可。

samples

<input>—> 80 <output>—> 80=7+73

Code

#include <stdio.h>
int isPrime( int x );
void Goldbach( int n );
int main()
{
    int n;
    scanf("%d",&n);
    if(n%2==0)
        Goldbach( n );
    return 0;
}
int isPrime( int x ){
    if(x==1)
        return 0;
    if(x==2)
        return 1;
    for (int i = 2; i < x; i++)
        if (x%i==0)
            return 0;
    return 1;
}


void Goldbach( int n ){
    for (int i = 1; i < n; i+=2)
        if(isPrime(n-i)&&isPrime(i)){
            printf("%d=%d+%d",n,i,n-i);
            break;
        }
}

N!

problemId:3931

Description

给出两个数 n, m。求 3748-1 和 3748-2

计算公式:

3748-3

Input

输入数据有多组(数据组数不超过 250),到 EOF 结束。

对于每组数据,输入两个用空格隔开的整数 n, m (0 <= m <= n <= 20) 。

Ouput

对于每组数据输出一行, 3748-1 和 3748-2,用空格隔开。

提醒:因为n!和 m! 数据较大,定义数据类型应用 long long int,输出格式%lld

samples

<input>—> 1 1 5 3 4 3

<output>—> 1 1 60 10 24 4

Code

#include <stdio.h>
long long int f(long long int n);
int main()
{
    long long int n,m,i;
    while(~scanf("%lld %lld",&n,&m))
    {
        long long int a,c;
        a=f(n)/f(n-m);
        c=f(n)/f(n-m)/f(m);
        printf("%lld %lld\n",a,c);
    }
    return 0;
}
long long int f(long long int n){
    if(n==0)
        return 1;
    long long int res=1;
    for (size_t i = 1; i <= n; i++)
        res*=i;
    return res;
}

分段函数

problemId:2557

Description

函数是一种特殊的映射,即数集到数集的映射。对于给定的每个自变量都能给出一个确定的值,这是一件多么牛的事情呀。其实不是函数牛,而是因为它具有这种性质我们的数学家才这么定义了它。函数有很多类型,虽然本质都是映射,但为了方便研究和应用,数学家们做了很多分类。比如线性函数,非线性函数,随机函数,还有一些具有特殊性质的函数等等。

今天我们要关注的是分段函数,所谓分段就是对于整个定义域来说,函数的值域是不连续的。很明显的一个就是绝对值函数,类似于y=|x|,(x,y属于R)。不连续是按照自变量的连续变化函数值的变化不连续而已,但函数仍然不离不弃的给了每个自变量一个值。

总之,函数就是按照规则对自变量进行操作得到相应的值。而程序里的函数就更牛了,它可以对我们的输入(自变量)进行各种我们想做的操作,最后得到输出(值),很好玩吧。

今天,就希望你能用程序里的函数实现数学里的分段函数,加油哦。

这个分段函数长得是这个样子的:

F(x) = log2(x)       0<x<10

       = |x|+sin(x)    x<0

       = 0                 x=0

       = x^2            x>=10 

 

Input

输入第一行给出数据的组数T。

接下来T行每行一个实数X。

 

Ouput

输出T行,每行一个函数值,四舍五入保留到小数点后两位。

希望你能根据函数的表达式,对于给定的每个自变量不离不弃的计算出它的值。

 

samples

<input>—> 4 0 10 5 -1 <output>—> 0.00 100.00 2.32 0.16

Code

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double f(double x)
{
    double y;
    if(x>0&&x<10)y=log2(x);
    else if(x<0)y=abs(x)+sin(x);
    else if(x==0)y=0;
    else y=x*x;
    return y;
}
int main()
{
    int T,i,x;
    scanf("%d",&T);
    for(i=0;i<T;i++)
    {
        scanf("%d",&x);
        printf("%.2lf\n",f(x));
    }
    return 0;
}

C/C++经典程序训练2---斐波那契数列

problemId:1132

Description

编写计算斐波那契(Fibonacci)数列的第n项函数fib(n)(n < 40)。
数列描述:

f1=f2==1;

fn=fn-1+fn-2(n>=3)。

Input

输入整数 n 的值(0 < n < 40)。

Ouput

输出fib(n)的值。

samples

<input>—> 7 <output>—> 13

Code

#include <stdio.h>
int fib(int n);
int main(){
    int n;
    scanf("%d",&n);
    printf("%d",fib(n));
    return 0;
}

int fib(int n){
    if (n==1||n==2)
        return 1;
    else 
        return fib(n-1)+fib(n-2);
}

计算题

problemId:1149

Description

一个简单的计算,你需要计算f(m,n),其定义如下:

当m=1时,f(m,n)=n;

当n=1时,f(m,n)=m;

当m>1,n>1时,f(m,n)= f(m-1,n)+ f(m,n-1)

Input

第一行包含一个整数T(1<=T<=100),表示下面的数据组数。

以下T行,其中每组数据有两个整数m,n(1<=m,n<=2000),中间用空格隔开。

Ouput

对每组输入数据,你需要计算出f(m,n),并输出。每个结果占一行。

samples

<input>—> 2 1 1 2 3 <output>—> 1 7

Code

#include <stdio.h>
int f(int m,int n);
int main(){
    int T,m,n;
    scanf("%d",&T);
    for (int i = 0; i < T; i++)
    {
        scanf("%d%d",&m,&n);
        if(i!=T-1)
            printf("%d\n",f(m,n));
        else
            printf("%d",f(m,n));
    }
    
    return 0;
}
int f(int m,int n){
    if(m==1)
        return n;
    if(n==1)
        return m;
    return f(m-1,n)+f(m,n-1);
}

斐波那契?

problemId:1689

Description

给出一个数列的递推公式,希望你能计算出该数列的第N个数。递推公式如下:

F(n)=F(n-1)+F(n-2)-F(n-3). 其中,F(1)=2, F(2)=3, F(3)=5.

很熟悉吧,可它貌似真的不是斐波那契数列呢,你能计算出来吗?

### Input

   输入只有一个正整数N(N>=4).

### Ouput

   输出只有一个整数F(N).

### samples \--> 5 \--> 8 ### Code ``` #include int f(int x) { int n; if(x==1) n=2; else if(x==2) n=3; else if(x==3) n=5; else n=f(x-1)+f(x-2)-f(x-3); return n; } int main() { int x; scanf("%d",&x); printf("%d",f(x)); return 0; } ``` # 高中数学? problemId:2400 ### Description 高中数学大家都学过数列,其中一个重要的概念就是数列的通项,可以代表数列中每一项的一个表达式。

 今天我们的问题就跟通项有关系,说,给你一个数列的通项和数列中的前几项,希望你能求出它的第n项。

 通项表达式如下:

 F(1) = 0;

 F(2) = 1;

 F(n) = 4F(n-1)-5F(n-2);

Input

输入数据第一行是一个正整数T,T<100。接下来T行,每行一个整数n, 2<n<50。

Ouput

输出有T行,对于输入中每行中的n按照通项计算出F(n)。

samples

<input>—> 4 3 4 5 6

<output>—> 4 11 24 41

Code

#include<stdio.h>
#include<math.h>
int f(int n);
int main()
{
 int m;
 int k;
 scanf("%d",&k);
 while(k--)
 {
  scanf("%d",&m);
  printf("%d\n",f(m));
 }
 return 0;
}
int f(int n)
{
 int x;
 if(n==1)
  return 0;
 if(n==2)
  return 1;
 else
  return 4*f(n-1)-5*f(n-2);
}

计算组合数

problemId:1586

Description

计算组合数。C(n,m),表示从n个数中选择m个的组合数。

计算公式如下:

若:m=0,C(n,m)=1

否则, 若 n=1,C(n,m)=1

             否则,若m=n,C(n,m)=1

                         否则 C(n,m) = C(n-1,m-1) + C(n-1,m).

 

### Input

第一行是正整数N,表示有N组要求的组合数。接下来N行,每行两个整数nm (0 <= m <= n <= 20)

### Ouput 输出N行。每行输出一个整数表示C(n,m) ### samples \--> 3 2 1 3 2 4 0 \--> 2 3 1 ### Code ``` #include int f(int n,int m) { int y; if(m==0) { y=1; } else { if(n==1) { y=1; } else { if(m==n) { y=1; } else { y=f(n-1,m-1)+f(n-1,m); } } } return y; } int main() { int t; int n,m; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); printf("%d\n",f(n,m)); } return 0; } ```