C++ 试题

C++ 试题

1. 成绩存入数组

【题目】m 个人的成绩存放在 score 数组中,请编写函数 fun, 它的功能是:将低于平均分的人作为函数值返回,将低于平均分的分数放在 below 所指定的函数中。

【代码】

#include <stdio.h>
​
int fun(double score[], int m, double below[]){
    int i, k = ;
    double aver = .;
    for (i = ; i < m; i++){
        aver += score[i];
    }
    aver /= m;
    for (i = ; i < m; i++){
        if (score[i] < aver){
            below[k++] = score[i];
        }
    }
    return k;
}
​
int main(){//main function is used to Test fun function
    double score[] = {78,65,45,68,9};
    double below[5];
    int i = fun(score, 5, below);
    printf("%d\n", i);
    return ;
}

2. 被 7 或者 11 整除的数

【题目】请编写函数 fun,它的功能是:求出 1 到 1 之内能被 7 或者 11 整除,但不能同时被 7 和 11 整除的所有整数,并将他们放在 a 所指的数组中,通过 n 返回这些数的个数。

【代码】

#include <stdio.h>
​
void fun(int a[], int *n){
    int i, j = ;
    for (i = 2; i < 1; i++){
        if ((i % 7 == || i % 11 == ) && i % 77 != ){
            a[j++] = i;
        }
    }
    *n = j;
}
​
int main(){//main function is used to Test fun function
    int n = ;
    int a[1];
    fun(a, &n);
    printf("%d\n",n);
    return ;
}

3. 求出能整除 x 且不是偶数的整数

【题目】请编写函数 void fun(int x, int pp[], int *n), 它的功能是:求出能整除 x 且不是偶数的各整数,并按从小到大的顺序放在 pp 所指的数组中,这些除数的个数通过形参 n 返回。

【代码】

#include <stdio.h>
​
void fun(int x, int pp[], int *n){
    int i, j = ;
    for (i = 1; i < x; i++){
        if (x % i ==  && i %2 != ){
            pp[j++] = i;
        }
        *n = j;
    }
}
​
int main(){//main function is used to Test fun function
    int n = ;
    int pp[1];
    fun(6, pp, &n);
    printf("%d\n",n);
    return ;
}

4. 统计字母出现的次数

【题目】请编写一个函数 void fun(char *tt, int pp[]), 统计在 tt 字符中" a "到" z " 26 各字母各自出现的次数,并依次放在 pp 所指的数组中。

【代码】

#include <stdio.h>
#include <string.h>
​
void fun(char *tt, int pp[]){
    int i, j = ;
    for (i = ; i < strlen(tt); i++){
        if (tt[i] >= 'a' && tt[i] <= 'z'){
            j = tt[i] - 'a' ;
            pp[j] = pp[j] + 1;
        }
    }
}
​
int main(){//main function is used to Test fun function
    char tt[] = {'a','b','a'}; 
    int i;
    int pp[26];
    //int pp[26] = {};
    for (i = ; i < 26; i++){
        pp[i] = ;
    } 
    fun(&tt, pp);
    
    for (i = ; pp[i] != '\'; i++){
        if (pp[i] > ){
            char c = i + 'a';
            printf("%c = %d\n", c, pp[i]);
        }
    }   
    return ;
}

5. 指定素数输入数组

【题目】请编写一个函数 void fun(int m, int k, int xx[]), 该函数的功能是:将大于整数 m 且紧靠 m 的 k 个素数存入 xx 所指的数组中。

【代码】

#include <stdio.h>
#include <math.h>
​
void fun(int m, int k, int xx[]) {
    int i, j, count = ;
    for (i = m + 1;count < k ; i++) {
        for (j = 2; j <= sqrt(i); j++) {
            if (i % j == ) {
                break;
            }
        }
        if (j > sqrt(i)) {
            xx[count++] = i;
        }
    }
}
​
int main() {
    int a[1];
    fun(3,5,a);
    int i;
    for (i = ; i < 5; i++) {
        printf("%d ", a[i]);
    }
}

6. 删除指定下标的字符

【题目】请编写一个函数void fun(char a[],char[],int n),其功能是:删除以各字符串中指定下标的字符。其中,a指向原字符串,删除后的字符串存放在b所指的数组中,n中存放指定的下标。

【代码】

#include <stdio.h>
#include <string.h>
​
 void fun(char a[], char b[], int n) {
    int i, j = ;
    for (i = ; i < strlen(a); i++) {
        if (i != n) {
            b[j++] = a[i];
         }
     }
     b[j] = '\';
 }
 
 int main() {
    char a[] = "Hello";
    char b[1];
    fun(a, b, 2);
    puts(b);
    return ;
 }

7. 元素下标存放k中

【题目】请编写一个函数int fun(int *s,int t,int *k),用来求出数组的最大元素在数组中的下标并存放在k所指的储存单元中。

【代码】

#include <stdio.h>
#include <string.h>
​
int fun(int *s, int t, int *k) {
    int i, max;
    max = s[];
    for (i = 1; i < t; i++) {
        if (s[i] > max) {
            max = s[i];
            *k = i;
        }
    }
    return ;
}
​
int main() {
    int a[4];
    int i;
    for (i = ; i < 4; i++) {
        scanf("%d", &a[i]);
    } 
    int value = ;
    
    fun(&a, 4, &value);
    printf("%d", value);
    return ;
}

8. 计算并输出下列多项式值

【题目】编写函数fun,功能是:根据以下公式计算s,计算结果作为函数值返回;n通过形参传入。s=1+1/(1+2)+1/(1+2+3)+.......+1/(1+2+3+4+......+n)

【代码】

#include <stdio.h> 
​
double fun(int n) {
    int i, j, t;
    double s = .;
    for (i = 1; i <= n; i++) {
        t = ;
        for (j = 1; j <= i; j++) {
            t += j;
        }
        s += 1./t;
    }
    return s;
}
​
int main() {
    printf("%lf\n", fun(1));
    return ;
}

9. 求值

【题目】编写一个函数fun,它的功能是:根据以下公式求P的值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不吃糖...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值