C++ 如何找到位数大于5的数字之和
所以我想写一个函数,返回数组中所有数字的总和,这些数字的位数大于5,例如,如果数组是[12,66,23,67],那么答案是66+67 这段代码对数组中的所有数字求和,我不知道为什么C++ 如何找到位数大于5的数字之和,c++,C++,所以我想写一个函数,返回数组中所有数字的总和,这些数字的位数大于5,例如,如果数组是[12,66,23,67],那么答案是66+67 这段代码对数组中的所有数字求和,我不知道为什么 using namespace std; int func(int n[], int size){ int digit, S=0, a; for(int i=0; i<size; i++){ a= n[i]; while( n[i]!=0){
using namespace std;
int func(int n[], int size){
int digit, S=0, a;
for(int i=0; i<size; i++){
a= n[i];
while( n[i]!=0){
digit= n[i]%10;
if(digit>=5){
n[i]= n[i]/10;
}
else break;
}
S=S+a;
}
return S;
}
int main()
{
int n[3], i;
for(int i=0; i<3; i++){
cin>>n[i];
}
cout<<func(n, 3)<<endl;
return 0;
}```
使用名称空间std;
int func(int n[],int size){
整数位数,S=0,a;
对于(int i=0;i=5){
n[i]=n[i]/10;
}
否则就断了;
}
S=S+a;
}
返回S;
}
int main()
{
int n[3],i;
对于(int i=0;i>n[i];
}
coutS=S+a
这段代码是在while循环外和inside-for循环内,这将添加数组中的所有元素主要问题是当您应该将a
添加到S
这些线路导致了问题:
int func(int n[], int size){
int digit, S=0, a;
for(int i=0; i<size; i++){
a= n[i];
while( n[i]!=0){
digit= n[i]%10;
if(digit>=5){ // Wrong. Do "digit > 5"
n[i]= n[i]/10;
}
else break; <--- exiting this while loop
}
S=S+a; <--- ...but still adding?
}
return S;
}
然后,只需检查每个数字是否大于5:
// If number was valid
if(each_digit_greater_than_5)
{
// Add to sum
S = S + a;
}
附加说明:
如何编写代码通常比编写内容更重要。
这里有一些思想食粮
- 使用更好的格式:整个代码块应统一缩进。这:
及
及
而不是S
,n
和a
- 评论!:编程的另一个非常重要的部分(注意:不是编码)。
你在编码吗
还是编程
Making a clear, maintainable and debuggable code.
这个问题是用C++来标记的,我想给出一个附加的答案,使用C++,特别是现代C++算法。我还添加了注释到代码中,并使用了有意义的变量名。我建议你以后也尝试这样做。
示例代码在做什么
首先,它通知用户有关软件的信息,并询问应该检查和添加多少值。这些值将存储在std::vector
中。使用std::copy\n
和std::istream\u迭代器
我们读取用户给出的值。std::istream\u迭代器
只调用提取器操作符>
然后我们调用子函数来计算和并将其显示给用户
子函数由一个Lampda定义和一个std::acculate
语句组成
lambda将int转换为std::string
,然后检查数字中是否有任何字符小于“5”。然后返回一个反向结果
这样,您可以看到,用C++实现的实现非常简单:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <numeric>
int sumOfNumbersWithDigitsGreaterThan5(const std::vector<int>& v) {
// Create a lambda that checks, if all digits are greater than 5
auto check5 = [](const int i) -> bool { std::string s{ std::to_string(i) };
return !std::any_of(s.begin(), s.end(), [](const char c) { return c <= '5'; }); };
// Calculate the sume of all given values in the vector with all digits greater than 5
return std::accumulate(v.begin(), v.end(), 0,
[&](const int init, const int i) { return init + (check5(i) ? i : 0); });
}
int main() {
// Inform the user, what to do
std::cout << "Sum of all numbers having all digits greater than 5\n\nHow many numbers to you want to sum?: ";
// Get the number of values that we want to sum up. Check plausibility
if (int numberOfValuesToCheck{}; (std::cin >> numberOfValuesToCheck) && (numberOfValuesToCheck > 0)) {
// Create a std::vector for our values having the requested number of elements
std::vector<int> values(numberOfValuesToCheck);
// Read the requested number of elements from std::cin to the values vector
std::copy_n(std::istream_iterator<int>(std::cin), numberOfValuesToCheck, values.begin());
// Show result
std::cout << "\n\nThe sum for all numbers with all digits greater 5 is: " <<
sumOfNumbersWithDigitsGreaterThan5(values) << "\n";
}
else {
std::cerr << "\n\n*** Error: Wrong input\n";
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
大于5位数的整数总和(常数标准::向量&v){
//创建一个lambda,用于检查所有数字是否大于5
自动检查5=[](const int i)->bool{std::string s{std::to_string(i)};
return!std::(s.begin()、s.end()、[](const char c){return c numberOfValuesToCheck)&(numberOfValuesToCheck>0)中的任意一个){
//为具有所需元素数的值创建一个std::vector
std::向量值(numberOfValuesToCheck);
//从std::cin读取请求的元素数到值向量
std::copy_n(std::istream_迭代器(std::cin),numberOfValuesToCheck,values.begin();
//显示结果
std::我不能考虑a=n[i];
和S=S+a
。我也建议你做一些。
...
for(int i=0; i<size; i++){ // The base block
a= n[i]; // new block start, add indent
while( n[i]!=0){ // same block, same indent
digit= n[i]%10; // new block, add indent
...
sum_of_special_numbers
array
current_number
Don't care if anyone understands it or not, just doing it cause they said so.
Making a clear, maintainable and debuggable code.
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <numeric>
int sumOfNumbersWithDigitsGreaterThan5(const std::vector<int>& v) {
// Create a lambda that checks, if all digits are greater than 5
auto check5 = [](const int i) -> bool { std::string s{ std::to_string(i) };
return !std::any_of(s.begin(), s.end(), [](const char c) { return c <= '5'; }); };
// Calculate the sume of all given values in the vector with all digits greater than 5
return std::accumulate(v.begin(), v.end(), 0,
[&](const int init, const int i) { return init + (check5(i) ? i : 0); });
}
int main() {
// Inform the user, what to do
std::cout << "Sum of all numbers having all digits greater than 5\n\nHow many numbers to you want to sum?: ";
// Get the number of values that we want to sum up. Check plausibility
if (int numberOfValuesToCheck{}; (std::cin >> numberOfValuesToCheck) && (numberOfValuesToCheck > 0)) {
// Create a std::vector for our values having the requested number of elements
std::vector<int> values(numberOfValuesToCheck);
// Read the requested number of elements from std::cin to the values vector
std::copy_n(std::istream_iterator<int>(std::cin), numberOfValuesToCheck, values.begin());
// Show result
std::cout << "\n\nThe sum for all numbers with all digits greater 5 is: " <<
sumOfNumbersWithDigitsGreaterThan5(values) << "\n";
}
else {
std::cerr << "\n\n*** Error: Wrong input\n";
}
return 0;
}