c++语言程序设计题

1.设计一函数fcount,统计一个正整数数组中有几个数组元素是三位偶数。写一主调函数,用键盘输入一个整型数组arr的20个元素值,调用fcount函数之后,在主调函数中输出显示统计结果。

2. 编写一个函数CountLetter,统计出字符串中有多少个不同的英文字母;大小写字母认为是同一个。CountLetter (“Hello”)的结果为4, CountLetter (”A Sample string”) 的结果为11

3. 下面是使用某矩阵类CMatrix的例子和运行结果,请设计完成该矩阵类CMatrix。

 int main ()

    {

        int     S1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

        CMatrix     A (3,5), B (3,5);

        A.Assign (S1); //数组元素赋给矩阵元素

        A.Display ();//矩阵显示

        int     S2[] = {2,0,3,4,5,1,7,8,9,10,11,2,3,14,10};

        B.Assign (S2); //数组元素赋给矩阵元素

        B.Display ();//矩阵显示

        CMatrix     C;

        C = A+ B; //矩阵相加

        C.Display ();

        return 0;

}

运行结果 :

1   2   3   4   5  

6   7   8   9   10 

11  12  13  14  15 

2   0   3   4   5  

1   7   8   9   10 

11  2   3   14  10 

3   0   6   8   10 

7   14  16  18  20 

22  14  16  28  25 

 题一:

程序代码

#include <iostream>
using namespace std;

// fcount函数,统计三位偶数的数量
int fcount(int arr[], int n) {
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] >= 100 && arr[i] <= 999 && arr[i] % 2 == 0) {
            count++;
        }
    }
    return count;
}

int main() {
    int arr[20];
    
    // 从键盘接收20个整数
    cout << "请输入20个整数:" << endl;
    for (int i = 0; i < 20; i++) {
        cin >> arr[i];
    }

    // 调用fcount函数并输出结果
    cout << "三位偶数的数量为: " << fcount(arr, 20) << endl;

    return 0;
}

运行结果

题二:

程序代码

#include <stdio.h>
#include <ctype.h>

// 函数原型
int CountLetter(const char *str);

int main() {
    // 测试示例
    printf("CountLetter(\"Hello\"): %d\n", CountLetter("Hello"));
    printf("CountLetter(\"A Sample string\"): %d\n", CountLetter("A Sample string"));

    return 0;
}

// 统计字符串中不同英文字母的数量
int CountLetter(const char *str) {
    int count = 0;
    int letters[26] = {0};  // 用于记录每个字母是否出现过,初始全部为0

    while (*str != '\0') {
        if (isalpha(*str)) {
            // 将字母转换为小写,因为题目要求大小写字母认为是同一个
            char lowercase = tolower(*str);

            // 如果该字母未出现过,则增加计数,同时标记该字母已出现
            if (letters[lowercase - 'a'] == 0) {
                count++;
                letters[lowercase - 'a'] = 1;
            }
        }

        str++;
    }

    return count;
}

 运行结果

题三:

程序代码

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值