以下是一个计算各位数字和的C++程序:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main() {
    string n;
    cout << "请输入一个整数 n: ";
    cin >> n;

    vector<int> digits;
    for (char c : n) {
        digits.push_back(c - '0');
    }

    int sum = 0;
    for (int digit : digits) {
        sum += digit;
    }

    cout << "各位数字和为:" << sum << endl;

    return 0;
}

这个程序首先读取输入的整数 n,并将其转换为字符串。然后,将每个字符转换为数字,并将这些数字存储在一个向量中。最后,计算向量中所有数字的和,并输出结果。

请注意,这个程序可以处理数字 n 的位数多达 200 位


原文地址: https://www.cveoy.top/t/topic/h4qc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录