Unable to determine cause due to differences in output

Asked 1 years ago, Updated 1 years ago, 372 views

https://atcoder.jp/contests/abc083/tasks/abc083_b

"The output result will be ""0"" in the code below, but I do not know which part to correct, so I would appreciate it if you could let me know."

entering
20 2 5

output
84
*Among integers of 20 or less, 2, 3, 4, 5, 11, 12, 13, 14, 20 have the sum of 2 or more digits and 5 or less.Outputs a total of 84 of these.
#include<bits/stdc++.h>

using namespace std;

int log_2(int n, int a, int b){
    
    intans = 0;
    for(inti=1;i<=n;i++){
        int sum = 0;
        int num=i;
        while(num<=0){
            sum+=num%10;
            // cout <<sum <<endl;
            num/=10;
        }
        // cout <<sum <<endl;
        if(a<=sum&&sum<=b){
            an + = i;    
        }
        
    }
    
    returnans;
}


int main() {
    int n, a, b;
    cin>>n>a>b;
    
    cout<<log_2(n,a,b)<<endl;
    
    return 0;
}

c++

2023-01-24 15:41

1 Answers

The initial value of num is 1, so while(num<=0) will naturally never go around the loop (so it ends with ans=0).

This is the answer to this question.Oira can write the answer to this question, but it's not interesting, and I think you can answer the question yourself.
# I just need to see 84 in the end, right?


2023-01-24 19:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.