1. 程式人生 > >C語言基礎程式設計題目(函式題)

C語言基礎程式設計題目(函式題)

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
//6-1 簡單輸出整數
/* 你的程式碼將被嵌在這裡 */
void PrintN ( int N )
{
	int i=0;
	for(i=1;i<N+1;i++)
	{
		printf("%d\n",i);
	}
} 

6-2 多項式求值 (15 分)

本題要求實現一個函式,計算階數為n,係數為a[0] ... a[n]的多項式f(x)=\sum_{i=0}^{n}(a[i]*x^{i})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
//6-2 多項式求值
/* 你的程式碼將被嵌在這裡 */
double f( int n, double a[], double x )
{
	double sum=0;
	int i=0,j=0;
	
	for(i=0;i<n+1;i++)
	{
		sum+=a[i]*pow(x,i);
	}
	
	return sum;
}

//編譯器警告
//[Warning] incompatible implicit declaration of built-in function 'pow' [enabled by default]

6-3 簡單求和 (10 分)

本題要求實現一個函式,求給定的N個整數的和。

函式介面定義:

int Sum ( int List[], int N );

其中給定整數存放在陣列List[]中,正整數N是陣列元素個數。該函式須返回NList[]元素的和。

裁判測試程式樣例:

#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
//6-3 簡單求和
/* 你的程式碼將被嵌在這裡 */
int Sum ( int List[], int N )
{
	int sum=0;
	int i=0;
	
	for(i=0;i<N;i++)
	{
		sum+=List[i];
	}
	
	return sum;
} 

6-4 求自定型別元素的平均 (10 分)

本題要求實現一個函式,求N個集合元素S[]的平均值,其中集合元素的型別為自定義的ElementType

函式介面定義:

ElementType Average( ElementType S[], int N );

其中給定集合元素存放在陣列S[]中,正整數N是陣列元素個數。該函式須返回NS[]元素的平均值,其值也必須是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
//6-4 求自定型別元素的平均
/* 你的程式碼將被嵌在這裡 */
ElementType Average( ElementType S[], int N )
{
	ElementType sum=0.0;
	int i=0;
	
	for(i=0;i<N;i++)
	{
		sum+=S[i];
	}
	
	return (ElementType)sum/N;
}

6-5 求自定型別元素的最大值 (10 分)

本題要求實現一個函式,求N個集合元素S[]中的最大值,其中集合元素的型別為自定義的ElementType

函式介面定義:

ElementType Max( ElementType S[], int N );

其中給定集合元素存放在陣列S[]中,正整數N是陣列元素個數。該函式須返回NS[]元素中的最大值,其值也必須是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
//6-5 求自定型別元素的最大值
/* 你的程式碼將被嵌在這裡 */
ElementType Max( ElementType S[], int N )
{
	ElementType max=S[0];
	int i=0;
	
	for(i=0;i<N;i++)
	{
		if(S[i]>max)
		{
			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
//6-6 求單鏈表結點的階乘和
/* 你的程式碼將被嵌在這裡 */
int FactorialSum( List L )
{
	int sum=0;
	int factorial=1;
	int i=0;
	List p=L; //p指向L的第一個節點 
	
	while(p)
	{
		factorial=1;
		for(i=1;i<=p->Data;i++)
		{
			factorial*=i;
		}
		sum+=factorial;
		p=p->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
//6-7 統計某類完全平方數
int IsTheNumber ( const int N )
{

    int m=N,a;

    a=(int)sqrt(N);
    if(a*a==N) //是完全平方數
    {
        int i;
        int num[10]={0};//陣列初始化為0

        while(m>0)
        {
            for(i=0;i<10;i++) //int的最大值為2147483647
            {
                if(m%10==i)
                {
                    num[i]+=1;
                    if(num[i]==2)
                    {
                        return 1;
                    }
                }
            }
            m=m/10;
        }
        return 0;
    }
    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
//6-8 簡單階乘計算
int Factorial( const int N )
{
    int factorial=1;
    int i;
    if(N>0)
    {
        for(i=1;i<=N;i++)
        {
            factorial*=i;
        }
        return factorial;
    }
    else if(N==0)
        return 1;
    else
        return 0;
}

6-9 統計個位數字 (15 分)

本題要求實現一個函式,可統計任一整數中某個位數出現的次數。例如-21252中,2出現了3次,則該函式應該返回3。

函式介面定義:

int Count_Digit ( const int N, const int D );

其中ND都是使用者傳入的引數。N的值不超過int的範圍;D是[0, 9]區間內的個位數。函式須返回ND出現的次數。

裁判測試程式樣例:

#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
//6-9 統計個位數字
int Count_Digit ( const int N, const int D )
{
    int a=N;
    int i;
    int cnt=0;

    if(a<0)
        a=-a;

    do
    {
        if(a%10==D)
        {
            cnt++;               
        }   
        a=a/10;
    }while(a>0);
    return cnt;
}

6-10 階乘計算升級版 (20 分)

本題要求實現一個列印非負整數階乘的函式。

函式介面定義:

void Print_Factorial ( const int N );

其中N是使用者傳入的引數,其值不超過1000。如果N是非負整數,則該函式必須在一行中打印出N!的值,否則列印“Invalid input”。

裁判測試程式樣例:

#include <stdio.h>

void Print_Factorial ( const int N );

int main()
{
    int N;
	
    scanf("%d", &N);
    Print_Factorial(N);
    return 0;
}

/* 你的程式碼將被嵌在這裡 */

輸入樣例:

15

輸出樣例:

1307674368000
//6-10 階乘計算升級版