以下针对C++循环结构与边界优化的修改方案中,同时修复原代码的边界错误并完成合理优化的是?

dw646588 单选题 共 个子题

已知原C++代码如下,存在循环边界错误与可优化的重复计算问题:

int calculateTotal(char* str) {
    int total = 0;
    for(int i=0; i<=strlen(str); i++){
        total += str[i];
    }
    return total;
}

以下哪个修改方案同时解决了上述两个问题?

A
int calculateTotal(char* str) {
    int total = 0;
    int len = strlen(str);
    for(int i=0; i<len; i++){
        total += str[i];
    }
    return total;
}
B
int calculateTotal(char* str) {
    int total = 0;
    for(int i=0; i<strlen(str); i++){
        total += str[i];
    }
    return total;
}
C
int calculateTotal(char* str) {
    int total = 0;
    int len = strlen(str);
    for(int i=0; i<=len; i++){
        total += str[i];
    }
    return total;
}
D
int calculateTotal(char* str) {
    int total = 0;
    for(int i=1; i<=strlen(str); i++){
        total += str[i];
    }
    return total;
}
正在加载题目数据…
已答 /
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析
讨论
发布
登录后参与讨论 立即登录
发布讨论需先绑定并验证手机号 去绑定手机号
加载中…
我
我
回复 @
回复 @
发送
还没有人讨论,来发表第一条吧