字符串中的数字总和

时间:2019-05-31 18:41:16

标签: c++

在此问题中,输入是一个长度为10 ^ 6的字符串,它必须将数字加起来。我的问题:在每个循环中都添加了数字48,但我不知道原因。如果在第十行使用“ soma = soma + nome [x]-48”,则可以解决该问题。但是我想知道为什么会这样。谢谢

#include <iostream>
#include <cstring>
using namespace std;

int main(){
    char nome[1123456];
    long long soma = 0;
    cin >> nome;
    for(int x = 0; x < strlen(nome); x++){
        soma = soma + nome[x];
        //cout << "The current value of soma is " << soma << endl;
    }
    cout << soma;
    return 0;
}

0 个答案:

没有答案