# C程序设计基础期中测试笔试（包含快排）

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

#include <stdio.h>
/*
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%
*/
{
}

#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]);
}

### 样例

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%

### 代码

#define MAXN 100
#include <stdio.h>
int A = 0, B = 0, C = 0, D = 0, F = 0;
for(int i = 0; i < n; i++){
if(score[i]==100){
A++;
continue;
}
switch (score[i]/10)
{
case 9:
A++;
break;
case 8:
B++;
break;
case 7:
C++;
break;
case 6:
D++;
break;
default:
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]);
}

## 1045. calculate pi

### 代码

#include <stdio.h>
double getpi(){
double SUM = 0.5;
double dFac = 1.0;
double t = 0.5;
double n = 1.0;
t /= 4.0;
dFac *= (2*n-1)/(2*n);
addNum = dFac * t * (1.0 / (2*n + 1.0));
n = n + 1;
}
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.

### 代码

#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;
}