6-1 简单输出整数 (10 分)
本题要求实现一个函数,对给定的正整数N
,打印从1到N
的全部正整数。
函数接口定义:
void PrintN ( int N );
其中N
是用户传入的参数。该函数必须将从1到N
的全部正整数顺序打印出来,每个数字占1行。
裁判测试程序样例:
#include <stdio.h>
void PrintN ( int N );
int main ()
{
int N;scanf("%d", &N);
PrintN( N );return 0;
}/* 你的代码将被嵌在这里 */
输入样例:
3
输出样例:
1
2
3
void PrintN(int N){
for(int i = 1; i<=N; i++){
printf("%d\n",i);
}
}
6-2 多项式求值 (15 分)
本题要求实现一个函数,计算阶数为n
,系数为a[0]
... a[n]
的多项式f(x)=∑i=0n(a[i]×xi) 在x
点的值。
函数接口定义:
double f( int n, double a[], double x );
其中n
是多项式的阶数,a[]
中存储系数,x
是给定点。函数须返回多项式f(x)
的值。
裁判测试程序样例:
#include <stdio.h>
#define MAXN 10
double f( int n, double a[], double x );
int main()
{
int n, i;
double a[MAXN], x;
scanf("%d %lf", &n, &x);
for ( i=0; i<=n; i++ )
scanf("%lf", &a[i]);
printf("%.1f\n", f(n, a, x));
return 0;
}/* 你的代码将被嵌在这里 */
输入样例:
2 1.1
1 2.5 -38.7
输出样例:
-43.1
double f(int n, double a[],double x){
double sum=a[0];
int i ;
for(i=1; i<= n; i++){
sum += a[i]*pow(x,i);
}
return sum;
}
6-3 简单求和 (10 分)
本题要求实现一个函数,求给定的N
个整数的和。
函数接口定义:
int Sum ( int List[], int N );
其中给定整数存放在数组List[]
中,正整数N
是数组元素个数。该函数须返回N
个List[]
元素的和。
裁判测试程序样例:
#include <stdio.h>
#define MAXN 10
int Sum ( int List[], int N );
int main ()
{
int List[MAXN], N, i;scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%d", &List[i]);
printf("%d\n", Sum(List, N));return 0;
}/* 你的代码将被嵌在这里 */
输入样例:
3
12 34 -5
输出样例:
41
int Sum(int List[], int N){
int i;
int su=0;
for(i=0; i<N; i++){
su += List[i];
}
return su;
}
6-4 求自定类型元素的平均 (10 分)
本题要求实现一个函数,求N
个集合元素S[]
的平均值,其中集合元素的类型为自定义的ElementType
。
函数接口定义:
ElementType Average( ElementType S[], int N );
其中给定集合元素存放在数组S[]
中,正整数N
是数组元素个数。该函数须返回N
个S[]
元素的平均值,其值也必须是ElementType
类型。
裁判测试程序样例:
#include <stdio.h>
#define MAXN 10
typedef float ElementType;ElementType Average( ElementType S[], int N );
int main ()
{
ElementType S[MAXN];
int N, i;scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%f", &S[i]);
printf("%.2f\n", Average(S, N));return 0;
}/* 你的代码将被嵌在这里 */
输入样例:
3
12.3 34 -5
输出样例:
13.77
ElementType Average(ElementType S[],int N){
ElementType x;
ElementType sum=0;
int i;
for(i=0;i<N; i++){
sum += S[i];
}
x = sum/N;
return x;
}
6-5 求自定类型元素的最大值 (10 分)
本题要求实现一个函数,求N
个集合元素S[]
中的最大值,其中集合元素的类型为自定义的ElementType
。
函数接口定义:
ElementType Max( ElementType S[], int N );
其中给定集合元素存放在数组S[]
中,正整数N
是数组元素个数。该函数须返回N
个S[]
元素中的最大值,其值也必须是ElementType
类型。
裁判测试程序样例:
#include <stdio.h>
#define MAXN 10
typedef float ElementType;ElementType Max( ElementType S[], int N );
int main ()
{
ElementType S[MAXN];
int N, i;scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%f", &S[i]);
printf("%.2f\n", Max(S, N));return 0;
}/* 你的代码将被嵌在这里 */
输入样例:
3
12.3 34 -5
输出样例:
34.00
ElementType Max(ElementType S[], int N){
ElementType max=S[0];
int i ;
for(i= 0; i<N; i++){
if(max< S[i]){
max = S[i];
}
}
return max;
}
6-6 求单链表结点的阶乘和 (15 分)
本题要求实现一个函数,求单链表L
结点的阶乘和。这里默认所有结点的值非负,且题目保证结果在int
范围内。
函数接口定义:
int FactorialSum( List L );
其中单链表List
的定义如下
typedef struct Node *PtrToNode;
struct Node {
int Data; /* 存储结点数据 */
PtrToNode Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>typedef struct Node *PtrToNode;
struct Node {
int Data; /* 存储结点数据 */
PtrToNode Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */int FactorialSum( List L );
int main()
{
int N, i;
List L, p;scanf("%d", &N);
L = NULL;
for ( i=0; i<N; i++ ) {
p = (List)malloc(sizeof(struct Node));
scanf("%d", &p->Data);
p->Next = L; L = p;
}
printf("%d\n", FactorialSum(L));return 0;
}/* 你的代码将被嵌在这里 */
输入样例:
3
5 3 6
输出样例:
846
int Factorial( int n){
if(n==0 || n == 1)return 1;
else
{
return n*Factorial(n-1);
}
}
int FactorialSum( List L){
int sum = 0;
// List p = L;
while(L){
sum += Factorial(L->Data) ;
L = L->Next;
}
return sum ;
}
6-7 统计某类完全平方数 (20 分)
本题要求实现一个函数,判断任一给定整数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 a = sqrt(N);
int array[10];
for(int i =0 ; i<10 ; i++){
array[i]= 0;
}
if(a*a == N){
int n = N;
while (n)
{
int g = n%10;
array[g]++;
if(array[g]== 2)
return 1;
n=n/10;
}
}
return 0;
}
6-8 简单阶乘计算 (10 分)
本题要求实现一个计算非负整数阶乘的简单函数。
函数接口定义:
int Factorial( const int N );
其中N
是用户传入的参数,其值不超过12。如果N
是非负整数,则该函数必须返回N
的阶乘,否则返回0。
裁判测试程序样例:
#include <stdio.h>
int Factorial( const int N );
int main()
{
int N, NF;
scanf("%d", &N);
NF = Factorial(N);
if (NF) printf("%d! = %d\n", N, NF);
else printf("Invalid input\n");return 0;
}/* 你的代码将被嵌在这里 */
输入样例:
5
输出样例:
5! = 120
int Factorial( const int N){
//int n = N;
int sum =1;
if(N<0 || N>12 ){
return 0;
}else if(N>0)
{
//int n=1;
for (int i =1; i<=N; i++){
sum = sum * i;
}
}else
{
sum =1;
}
return sum;
}
6-9 统计个位数字 (15 分)
本题要求实现一个函数,可统计任一整数中某个位数出现的次数。例如-21252中,2出现了3次,则该函数应该返回3。
函数接口定义:
int Count_Digit ( const int N, const int D );
其中N
和D
都是用户传入的参数。N
的值不超过int
的范围;D
是[0, 9]区间内的个位数。函数须返回N
中D
出现的次数。
裁判测试程序样例:
#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 ){
//D 0--9
int i ;
int cnt=0;
int temp;
// int a[10];
// for(i = 0; i< 10 ;i++){
// a[i]=0;
// }
int n = N;
if(n<0){
n = -n;
}
do{
temp = n%10;
n = n/10;
if( temp == D){
cnt++;
}
}while(n);
return cnt;
}