一、C语言实验——计算表达式
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
计算下列表达式值:
Input
输入x和n的值,其中x为非负实数,n为正整数。
Output
输出f(x,n),保留2位小数。
Sample Input
3 2
Sample Output
2.00
#include<stdio.h>
#include<math.h>
int main()
{
int x,y;
double z;
scanf("%d%d",&x,&y);
z = sqrt(1+x);
for(int i = 2;i<=y;i++)
{
z = sqrt(i+z);
}
printf("%.2lf\n",z);
return 0;
}
二、求数列的和
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
数列的定义如下: 数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。
Input
输入数据有多组,每组占一行,由两个整数n(n< 10000)和m(m< 1000)组成,n和m的含义如前所述。
Output
对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。
Sample Input
81 4
2 2
Sample Output
94.73
3.41
#include<stdio.h>
#include<math.h>
int main()
{
int n,m;
double z,s;
while(~scanf("%d %d",&n,&m))
{
s = n;
z = n;
for(int i = 0; i<m-1; i++)
{
z = sqrt(z);
s = s+z;
}
printf("%.2lf\n",s);
}
return 0;
}
三、C语言实验——一元二次方程Ⅰ
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
解一元二次方程ax2+bx+c=0的解。保证有解
Input
a,b,c的值。
Output
两个根X1和X2,其中X1>=X2。 结果保留两位小数。
Sample Input
1 5 -2
Sample Output
0.37 -5.37
Hint
提示:计算过程中,分母是(2*a)
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
double x1,x2,z;
scanf("%d %d %d",&a,&b,&c);
x1 = (-1*b+sqrt(b*b-4*a*c))/(2*a);
x2 = (-1*b-sqrt(b*b-4*a*c))/(2*a);
if(x1<x2)
{
z = x1;
x1 = x2;
x2 = z;
}
printf("%.2lf %.2lf\n",x1,x2);
return 0;
}
四、求三角形面积
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
已知三角形的边长a、b和c,求其面积。
Input
输入三边a、b、c。
Output
输出面积,保留3位小数。
Sample Input
1 2 2.5
Sample Output
0.950
Hint
海伦公式求三角形面积。如果三角形的三边为a, b, c且p=(a+b+c)/2,则三角形面积为(p*(p-a) * (p - b) * (p -c))的平方根。
#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 = sqrt(p*(p-a)*(p-b)*(p-c));
printf("%.3lf\n",s);
return 0;
}
五、求实数绝对值
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
求实数的绝对值。
Input
输入数据有多组,每组占一行,每行包含一个实数。输入文件直到EOF为止!
Output
对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。
Sample Input
123
-234.00
Sample Output
123.00
234.00
Hint
EOF结束的语句是这样使用的,今后还后很多这样的题目,千万要记住哦...
while (scanf("%f",&a)!=EOF)
{ }
如果输入数据有多组,每组占一行。
每行有两个整数a和n,分别用空格分隔。
读到文件结束的输入形式为:
while (scanf("%d %d",&a,&n)!=EOF)
{ }
#include<stdio.h>
int main()
{
double a;
while(scanf("%lf",&a)!=EOF)
{
if(a>=0)
printf("%.2lf\n",a);
else
printf("%.2lf\n",-a);
}
return 0;
}
六、C/C++程序训练6---歌德巴赫猜想的证明
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
验证“每个不小于6的偶数都是两个素数之和”,输入一个不小于6的偶数n,找出两个素数,使它们的和为n。
Input
输入一个不小于6的偶数n。
Output
找出两个素数,使它们的和为n。只需要输出其中第一个素数最小的一组数据即可。
Sample Input
80
Sample Output
80=7+73
#include<stdio.h>
#include<math.h>
int main()
{
int a,f;
scanf("%d",&a);
f = 0;
for(int i = 2; i<=a/2; i++)
{
int flag = 0;
for(int j = 2; j<=sqrt(i); j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
int flag1= 0;
for(int j = 2; j<=sqrt(a-i); j++)
{
if((a-i)%j==0)
{
flag1=1;
break;
}
}
if(flag1==0)
{
printf("%d=%d+%d\n",a,i,a-i);
f = 1;
break;
}
}
if(f==1)
break;
}
return 0;
}
七、N!
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
给出两个数 n, m。求 和 。
计算公式:
Input
输入数据有多组(数据组数不超过 250),到 EOF 结束。
对于每组数据,输入两个用空格隔开的整数 n, m (0 <= m <= n <= 20) 。
Output
对于每组数据输出一行, 和 ,用空格隔开。
提醒:因为n!和 m! 数据较大,定义数据类型应用 long int,输出格式%ld
Sample Input
1 1
5 3
4 3
Sample Output
1 1
60 10
24 4
Hint
请注意数据范围,可能需要使用 long long 等 64 位整型,输出格式%lld
Source
【“师创杯”山东理工大学第九届ACM程序设计竞赛 正式赛】MLE_kenan
#include<stdio.h>
int main()
{
long int n,m,a,c;
while(scanf("%ld%ld",&n,&m)!=EOF)
{
a = 1;
c = 1;
for(int i=n-m+1;i<=n;i++)
{
a = a * i;
}
for(int i=1;i<=m;i++)
{
c = c * i;
}
c = a/c;
printf("%ld %ld\n",a,c);
}
return 0;
}
八、分段函数
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem 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。
Output
输出T行,每行一个函数值,四舍五入保留到小数点后两位。
希望你能根据函数的表达式,对于给定的每个自变量不离不弃的计算出它的值。
Sample Input
4
0
10
5
-1
Sample Output
0.00
100.00
2.32
0.16
Hint
log2(x)是以2为底x的对数.
#include<stdio.h>
#include<math.h>
int main()
{
int T;
double x;
scanf("%d",&T);
while(T--)
{
scanf("%lf",&x);
if(x>0&&x<10)
x = log2(x);
else if(x<0)
x = fabs(x)+sin(x);
else if(x==0)
x = 0;
else
x = x * x;
printf("%.2lf\n",x);
}
return 0;
}
九、C/C++经典程序训练2---斐波那契数列
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
编写计算斐波那契(Fibonacci)数列的第n项函数fib(n)(n<40)。
数列:
f1=f2==1;
fn=fn-1+fn-2(n>=3)。
Input
输入整数n的值。
Output
输出fib(n)的值。
Sample Input
7
Sample Output
13
#include<stdio.h>
int main()
{
int n,f1,f2,f3;
scanf("%d",&n);
f1 = f2 =1;
for(int i = 3;i<=n;i++)
{
f3 = f2 + f1;
f1 = f2;
f2 = f3;
}
printf("%d\n",f3);
return 0;
}
十、计算题
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem 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),中间用空格隔开。
Output
对每组输入数据,你需要计算出f(m,n),并输出。每个结果占一行。
Sample Input
2
1 1
2 3
Sample Output
1
7
#include<stdio.h>
int f(int x,int y);
int main()
{
int T,n,m;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
printf("%d\n",f(m,n));
}
return 0;
}
int f(int x,int y)
{
int s;
if(x==1)
s = y;
else if(y==1)
s = x;
else if(x>1&&y>1)
s =f(x-1,y)+f(x,y-1);
return s;
}
十一、斐波那契?
Time Limit: 1000 ms Memory Limit: 32768 KiB
Problem 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).
Output
输出只有一个整数F(N).
Sample Input
5
Sample Output
8
#include<stdio.h>
int main()
{
int n,f1,f2,f3,f;
scanf("%d",&n);
f1 = 2;
f2 = 3;
f3 = 5;
for(int i = 4; i<=n; i++)
{
f = f3 + f2 - f1;
f1 = f2;
f2 = f3;
f3 = f;
}
printf("%d\n",f);
return 0;
}
十二、高中数学?
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
高中数学大家都学过数列,其中一个重要的概念就是数列的通项,可以代表数列中每一项的一个表达式。
今天我们的问题就跟通项有关系,说,给你一个数列的通项和数列中的前几项,希望你能求出它的第n项。
通项表达式如下:
F(1) = 0;
F(2) = 1;
F(n) = 4*F(n-1)-5*F(n-2);
Input
输入数据第一行是一个正整数T,T<100。接下来T行,每行一个整数n, 2<n<50。
Output
输出有T行,对于输入中每行中的n按照通项计算出F(n)。
Sample Input
4
3
4
5
6
Sample Output
4
11
24
41
#include<stdio.h>
int main()
{
int T,n,f1,f2,f;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
f1 = 0;
f2 = 1;
for(int i = 3; i<=n; i++)
{
f = 4*f2 - 5*f1;
f1 = f2;
f2 = f;
}
printf("%d\n",f);
}
return 0;
}
十三、计算组合数
Time Limit: 1000 ms Memory Limit: 32768 KiB
Problem 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行,每行两个整数n,m (0 <= m <= n <= 20)。
Output
输出N行。每行输出一个整数表示C(n,m)。
Sample Input
3
2 1
3 2
4 0
Sample Output
2
3
1
#include<stdio.h>
int f(int x,int y);
int main()
{
int T,n,m;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
printf("%d\n",f(m,n));
}
return 0;
}
int f(int x,int y)
{
int s;
if(y==0)
s = 1;
else if(x==1)
s = 1;
else if(x==y)
s = 1;
else
s = f(x-1,y-1)+f(x-1,y);
return s;
}