目录
一、函数题
6-1 判断奇偶性
本题要求实现判断给定整数奇偶性的函数。
函数接口定义:
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%2==0) return 1;
else return 0;
}
6-2 使用函数计算两点间的距离
本题要求实现一个函数,对给定平面任意两点坐标(x1,y1)和(x2,y2),求这两点之间的距离。
函数接口定义:
double dist( double x1, double y1, double x2, double y2 );
其中用户传入的参数为平面上两个点的坐标(x1
, y1
)和(x2
, y2
),函数dist
应返回两点间的距离。
裁判测试程序样例:
#include <stdio.h>
#include <math.h>
double dist( double x1, double y1, double x2, double y2 );
int main()
{
double x1, y1, x2, y2;
scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
printf("dist = %.2f\n", dist(x1, y1, x2, y2));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
10 10 200 100
输出样例:
dist = 210.24
代码:
double dist( double x1, double y1, double x2, double y2 )
{
double d;
d=sqrt(pow((x2-x1),2)+pow((y2-y1),2));
return d;
}
6-3 使用函数求余弦函数的近似值
本题要求实现一个函数,用下列公式求cos(x)的近似值,精确到最后一项的绝对值小于e:
cos(x)=x0/0!−x2/2!+x4/4!−x6/6!+⋯
函数接口定义:
double funcos( double e, double x );
其中用户传入的参数为误差上限e
和自变量x
;函数funcos
应返回用给定公式计算出来、并且满足误差要求的cos(x)的近似值。输入输出均在双精度范围内。
裁判测试程序样例:
#include <stdio.h>
#include <math.h>
double funcos( double e, double x );
int main()
{
double e, x;
scanf("%lf %lf", &e, &x);
printf("cos(%.2f) = %.6f\n", x, funcos(e, x));
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
0.01 -3.14
输出样例:
cos(-3.14) = -0.999899
代码:
double funcos( double e, double x )
{
int i=1,j;
double r=0,y,a,b;
while(1)
{
a=pow(x,2*i-2)*pow(-1,i+1);b=1;
for(j=1;j<=2*i-2;j++)
b=b*j;
y=a/b;
r+=y;
i++;
if(fabs(y)<e)
return r;
}
}
6-4 递归实现顺序输出整数
本题要求实现一个函数,对一个整数进行按位顺序输出。
函数接口定义:
void printdigits( int n );
函数printdigits
应将n
的每一位数字从高位到低位顺序打印出来,每位数字占一行。
裁判测试程序样例:
#include <stdio.h>
void printdigits( int n );
int main()
{
int n;
scanf("%d", &n);
printdigits(n);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
12345
输出样例:
1
2
3
4
5
代码:
void printdigits( int n )
{
if(n<10)
printf("%d\n",n);
else
{
printdigits(n/10);
printf("%d\n",n%10);
}
}
6-5 使用函数判断完全平方数
本题要求实现一个判断整数是否为完全平方数的简单函数。
函数接口定义:
int IsSquare( int n );
其中n
是用户传入的参数,在长整型范围内。如果n
是完全平方数,则函数IsSquare
必须返回1,否则返回0。
裁判测试程序样例:
#include <stdio.h>
#include <math.h>
int IsSquare( int n );
int main()
{
int n;
scanf("%d", &n);
if ( IsSquare(n) ) printf("YES\n");
else printf("NO\n");
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例1:
90
输出样例1:
NO
输入样例2:
100
输出样例2:
YES
代码:
int IsSquare( int n )
{
int d;
if(n==0||n==1) return 1;
d=sqrt(n);
if(d*d==n) return 1;
else return 0;
}
6-6 函数实现字符串逆序
本题要求实现一个字符串逆序的简单函数。
函数接口定义:
void f( char *p );
函数f
对p
指向的字符串进行逆序操作。要求函数f
中不能定义任何数组,不能调用任何字符串处理函数。
裁判测试程序样例:
#include <stdio.h>
#define MAXS 20
void f( char *p );
void ReadString( char *s ); /* 由裁判实现,略去不表 */
int main()
{
char s[MAXS];
ReadString(s);
f(s);
printf("%s\n", s);
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
Hello World!
输出样例:
!dlroW olleH
代码:
void f( char *p )
{
int count=0,i;
char *ret=p;
while(*p!='\0')
{
count++;
p++;
}
p=ret;
char t;
for(i=0;i<=(count/2-1);i++)
{
t=p[i];
p[i]=p[count-1-i];
p[count-1-i]=t;
}
}
6-7 使用函数输出一个实心的字符矩形
本题要求实现函数输出一个实心的字符矩形,定义并调用函数matrix(length, width, ch)
,它的功能是在屏幕上显示行数为width
、列数为length
,由字符ch
组成的实心矩形图案。
函数接口定义:
void matrix(int length, int width, char ch);
其中length
是矩阵的长度,width
是矩阵的宽度,ch
是输出的字符,要求函数按照如样例所示的格式,打印出行数为width
、列数为length
,由字符ch
组成的实心矩形图案。
裁判测试程序样例:
#include <stdio.h>
void matrix(int length, int width, char ch);
int main()
{
int length, width;
char ch;
scanf("%d %d %c", &length, &width, &ch);
matrix(length, width, ch);
return 0;
}
/* 请在这里填写答案 */
输入样例:
4 2 H
输出样例:
HHHH
HHHH
代码:
void matrix(int length, int width, char ch)
{
int i, j;
for(i=1; i<=width; i++)
{
for(j=1; j<=length; j++)
{
printf("%c", ch);
}
printf("\n");
}
}
二、编程题
7-1 求组合数
本题要求编写程序,根据公式Cnm=m!(n−m)!n!算出从n个不同元素中取出m个元素(m≤n)的组合数。
建议定义和调用函数fact(n)
计算n!
,其中n
的类型是int
,函数类型是double
。
输入格式:
输入在一行中给出两个正整数m和n(m≤n),以空格分隔。
输出格式:
按照格式“result = 组合数计算结果”输出。题目保证结果在double
类型范围内。
输入样例:
2 7
输出样例:
result = 21
代码:
#include<stdio.h>
double fact(int n,int m)
{
int i;
double sum1=1,sum2=1,sum3=1;
for(i=m;i>=1;i--)
{
if(i<=n)
sum1*=i;
if(i<=(m-n))
sum3*=i;
sum2*=i;
}
return sum2/(sum1*sum3);
}
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("result = %.0lf\n",fact(a,b));
return 0;
}
7-2 判断素数
本题的目标很简单,就是判断一个给定的正整数是否素数。
输入格式:
输入在第一行给出一个正整数N
(≤ 10),随后N
行,每行给出一个小于231的需要判断的正整数。
输出格式:
对每个需要判断的正整数,如果它是素数,则在一行中输出Yes
,否则输出No
。
输入样例:
2
11
111
输出样例:
Yes
No
代码:
#include <stdio.h>
#include <math.h>
int main()
{
int n, i, j;
long long x;
scanf("%d", &n);
while(n--)
{
scanf("%lld", &x);
j = 1;
for(i = 2; i <= sqrt(x); i++)
{
if(x % i == 0)
{
j = 0;
break;
}
}
if(x == 1 || j == 0)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
7-3 简单求阶乘问题
本题要求编写程序,计算N的阶乘。
输入格式:
输入在一行中给出一个不超过12的正整数N。
输出格式:
在一行中输出阶乘的值。
输入样例:
4
输出样例:
24
代码:
#include<stdio.h>
double ff(double n);
int main(void)
{
int i,n;
scanf("%d",&n);
double fff=ff(n);
printf("%.0lf\n",fff);
return 0;
}
double ff(double n)
{
double f;
if(n==1) f=1;
else f=ff(n-1)*n;
return f;
}
以上有错误欢迎指正,有建议也欢迎提出!!