int main() { int n,m,i,sum=0; cin>>n>>m; for( i=49;i<=m;i++) { sum+=i%10; i=i/10; sum=sum+i; if(sum==13) cout<<i<<endl; } return 0; }
时间: 2023-12-10 08:06:32 浏览: 75
This program reads two integers n and m from the user and then loops through all integers from 49 to m.
For each integer i in the loop, the program calculates the sum of its digits using the modulo operator and integer division. This sum is stored in the variable sum.
Then, the program divides i by 10 and adds the result to the sum variable. If the resulting sum is equal to 13, the program prints i to the console.
Note that there is a syntax error in the program: the loop condition should be i<=m instead of i to avoid an infinite loop. Additionally, there is no need to declare the variable sum outside of the loop.
Here is a corrected version of the program:
```cpp
#include <iostream>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
for (int i = 49; i <= m; i++) {
int sum = 0;
int temp = i;
while (temp > 0) {
sum += temp % 10;
temp /= 10;
}
sum += i / 10;
if (sum == 13) {
cout << i << endl;
}
}
return 0;
}
```
This version of the program correctly calculates the sum of digits of each integer using a while loop and a temporary variable. It also declares the sum variable inside the loop to avoid reusing its value from previous iterations.
阅读全文