C程序设计基础期中测试笔试(包含快排)

内容纲要

1044. grade

Finish the definition of the following function which computes the grades of n students for a course given their scores.

#include <stdio.h>
void grade(int score[], int n, char grade[])
/*
Precondition:
score[]: the scores (0-100) of n (n>0) students
Postcondition:
Compute the grade ('A': 90-100; 'B': 80-89; 'C': 70-79; 'D': 60-69; 'F': 0-59)
of each student, and store the result in grade[] in the same order as in
score[]. Display the distributions of the grades including the total number
(field with=4) and the percentage (field width=8) of each grade in the following
format:
A:  30  30.00%
B:  20  20.00%
C:  20  20.00%
D:  20  20.00%
F:  10  10.00%
*/
{
    // your definition here
}

#define MAXN 100

int main()
{
    int n;
    scanf("%d", &n);
    int score[MAXN];
    char g[MAXN];
    for (int i = 0; i < n; i++) 
        scanf("%d", &score[i]);
    grade(score, n, g);
}

样例

Input
10
50 60 65 70 75 80 85 90 95 100
Output
A: 3 30.00%
B: 2 20.00%
C: 2 20.00%
D: 2 20.00%
F: 1 10.00%

题解

没有难度的一道题,主要考量switch语句的应用,为了少写点代码,可以先特判100的情况,其余情况将分数除以10,就能用一个整数来代替等第了。

代码

#define MAXN 100
#include <stdio.h>
void grade(int score[], int n, char grade[]){
    int A = 0, B = 0, C = 0, D = 0, F = 0;
    for(int i = 0; i < n; i++){
        if(score[i]==100){
            grade[i] = 'A';
            A++;
            continue;
        }
        switch (score[i]/10)
        {
        case 9:
            grade[i] = 'A';
            A++;
            break;
        case 8:
            grade[i] = 'B';
            B++;
            break;
        case 7:
            grade[i] = 'C';
            C++;
            break;
        case 6:
            grade[i] = 'D';
            D++;
            break;
        default:
            grade[i] = 'F';
            F++;
            break;
        }
    }
    printf("A:%4d%8.2f%%\n", A, (double)A/(double)n*100);
    printf("B:%4d%8.2f%%\n", B, (double)B/(double)n*100);
    printf("C:%4d%8.2f%%\n", C, (double)C/(double)n*100);
    printf("D:%4d%8.2f%%\n", D, (double)D/(double)n*100);
    printf("F:%4d%8.2f%%\n", F, (double)F/(double)n*100);
}

int main()
{
    int n;
    scanf("%d", &n);
    int score[MAXN];
    char g[MAXN];
    for (int i = 0; i < n; i++) 
        scanf("%d", &score[i]);
    grade(score, n, g);
}

1045. calculate pi

题解

小学题,之前写过,只不过这次要保留小数点后10位,需要将MINNUM设置位1e-11。

代码

#include <stdio.h>
double getpi(){
    double addNum = 0.5;
    double SUM = 0.5;
    double dFac = 1.0;
    double t = 0.5;
    double n = 1.0;
    while(addNum > 0.00000000001){
        t /= 4.0;
        dFac *= (2*n-1)/(2*n);
        addNum = dFac * t * (1.0 / (2*n + 1.0));
        n = n + 1;
        SUM += addNum;
    }
    return 6*SUM;
}

int main()
{
    printf("%.10f\n",getpi());
    return 0;
}

1046. partition problem

Given a set of numbers, the partition problem is to find a subset of the numbers that add up to a specific target number. For example, there are two ways to partition the set {1,3,4,5} so that the remaining elements add up to 5:

Select the 1 and the 4 Select just the 5 By contrast, there is no way to partition the set {1,3,4,5} to get 11.

Write a program that enters a target number, and then several integers as elements of the set, and print number of partitions.

For example, print 2 if 5,1,3,4,5 are entered (all integers are separated by a blank), and print 0 if 11,1,3,4,5 are entered.

样例

Input
5 1 3 4 5
Output
2

题解

暴力递归搜索。 每次返回自己的下一位的函数返回值和再下一位的和的函数返回值。

代码

#include <stdio.h>

int isSum(int *a, int i, int num, int sum, int length){
    if(i==length)
        return num==sum?1:0;
    return isSum(a,i+1,num,sum,length)+isSum(a,i+1,num+a[i],sum,length);
}

int main(){
    int sum;
    scanf("%d",&sum);
    int n = 0;
    int temp;
    int a[1001] = {0};
    while(scanf("%d",&temp)!=EOF){
        a[n] = temp;
        n++;
    }
    printf("%d\n",isSum(a,0,0,sum,n));
}

1047. quicksort

Modify quicksort , sort by the number of digits for integers descending. If many integers have identical digits, sort them by their values ascending.

Write a program to enter some integers, to call quicksort , and to print.

题解

建议使用stdlib.h中的qsort函数,自己写cmp。 我自己写的qsort优化不够,很容易超时。 定义结构体,绑定num和位数,然后开结构体数组,按照两个标准排序。这两个标准要在cmp函数中体现。

代码

#include<stdio.h>
#include<stdlib.h>

typedef struct arr{
    int num;
    int digits;
}A;

int cmp(const void *a, const void *b){
    if((*(A*)a).digits!=(*(A*)b).digits)
        return ( (*(A*)a).digits - (*(A*)b).digits);
    else
        return ( (*(A*)b).num - (*(A*)a).num);

}

int getDig(int num){
    int count = 0;
    while(num>9){
        num/=10;
        count++;
    }
    return count+1;
}

int main(){
    A data[100001];
    int n;
    scanf("%d",&n);
    for(int i = 0; i < n; i++){
        scanf("%d", &data[i].num);
        data[i].digits = getDig(data[i].num);
    }
    qsort(data,n,sizeof(A),cmp);
    for(int i = n-1; i >= 0; i--) printf("%d ", data[i].num);
    return 0;
}

留下评论