You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C++中检测自定义数组中包含数字7的元素并打印

如何在C++中检测自定义数组中包含数字7的元素并打印

嘿,我看了你写的代码和需求啦,作为刚学C++的新手能写出这样的框架已经很棒了!先帮你梳理下现有代码里的几个小问题,再给你解决“检测含7的元素并打印”的方法:

先说说现有代码的小坑

  • 你在main里已经定义了int a1, j;,但第一个for循环里又写了int a1 = 0,这会导致变量重复定义,而且逻辑上你其实是想先获取用户想要的数组大小,不是循环100次输入,这个循环完全没必要~
  • 你用string numbers[100]来存数字,虽然也能实现,但其实可以直接用整数数组,之后再转字符串或者用数学方法检查7,这样更符合常规用法。
  • exit(1)需要包含<cstdlib>头文件,不然可能编译报错。

解决方案:两种方法检测含7的元素

方法1:把数字转成字符串检查(直观易理解)

这个思路是把每个输入的数字转成字符串,然后检查字符串里是否包含字符'7',适合新手理解:

#include <iostream>
#include <string>
#include <cstdlib> // 用于exit函数

using namespace std;

int main() {
    int arrSize;

    // 第一步:获取用户想要的数组大小
    cout << "Enter Wanted Array Size:" << endl;
    cin >> arrSize;

    // 检查数组大小是否合法
    if (arrSize > 100 || arrSize <= 0) {
        cout << "Array size is not allowed (must be between 1 and 100)" << endl;
        exit(1);
    }
    cout << "The Wanted Array Size is: " << arrSize << endl;

    // 第二步:输入数组元素(用整数数组存储)
    int numbers[100];
    cout << "Enter the numbers: " << endl;
    for (int i = 0; i < arrSize; ++i) {
        cin >> numbers[i];
    }

    // 第三步:打印所有元素,同时检测含7的元素
    cout << "The numbers are: ";
    for (int n = 0; n < arrSize; ++n) {
        cout << numbers[n] << "  ";
    }
    cout << endl;

    // 检测并打印含7的元素
    cout << "Numbers containing digit 7: ";
    for (int n = 0; n < arrSize; ++n) {
        // 把整数转成字符串
        string numStr = to_string(numbers[n]);
        // 检查字符串里是否有'7'
        if (numStr.find('7') != string::npos) {
            cout << numbers[n] << "  ";
        }
    }
    cout << endl;

    system("pause");
    return 0;
}

方法2:用数学方法检查(纯数字操作,不用字符串)

如果你不想用字符串,可以用取余和除法来逐个检查数字的每一位:

#include <iostream>
#include <cstdlib>

using namespace std;

// 写一个辅助函数,判断一个数字是否包含7
bool hasDigitSeven(int num) {
    // 处理负数的情况(如果用户输入负数)
    num = abs(num);
    do {
        // 取最后一位数字
        int digit = num % 10;
        if (digit == 7) {
            return true;
        }
        // 去掉最后一位数字
        num = num / 10;
    } while (num > 0);
    return false;
}

int main() {
    int arrSize;

    cout << "Enter Wanted Array Size:" << endl;
    cin >> arrSize;

    if (arrSize > 100 || arrSize <= 0) {
        cout << "Array size is not allowed (must be between 1 and 100)" << endl;
        exit(1);
    }
    cout << "The Wanted Array Size is: " << arrSize << endl;

    int numbers[100];
    cout << "Enter the numbers: " << endl;
    for (int i = 0; i < arrSize; ++i) {
        cin >> numbers[i];
    }

    cout << "The numbers are: ";
    for (int n = 0; n < arrSize; ++n) {
        cout << numbers[n] << "  ";
    }
    cout << endl;

    // 检测并打印含7的元素
    cout << "Numbers containing digit 7: ";
    for (int n = 0; n < arrSize; ++n) {
        if (hasDigitSeven(numbers[n])) {
            cout << numbers[n] << "  ";
        }
    }
    cout << endl;

    system("pause");
    return 0;
}

简单解释下

  • 两种方法都先修正了原有代码的逻辑:先获取数组大小,再输入元素,最后检测。
  • 方法1用to_string把整数转成字符串,再用find方法找'7',如果找到就说明这个数字包含7。
  • 方法2写了一个辅助函数,通过num % 10取最后一位,判断是不是7,然后num / 10去掉最后一位,循环直到数字变成0,这样就能检查每一位了。

你可以根据自己的理解选一种方法,运行试试,有问题再调整~

备注:内容来源于stack exchange,提问作者Creeperassasin1212

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 08:45:29