Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How can you fulfill two answer needs in C++17?

+9 votes
asked Oct 14, 2024 by Trang Nguyen (210 points)
Below is the code for the problem where you enter n and it should output the amount of digits if you write the number of 1 to n. Ex: Inputting 15 should output 21 since 123456789101112131415 has 21 digits.

But i also need to fulfill to goal that 50% of my answers of n less than or equal to 10^6. WHat does that mean and how can I achieve that?

#include <stdio.h>
#include <iostream>                                                             

int findCount(int n){
      if (n == 0)
          return 1;
    long long count = 0;
    while (n != 0) {
        count++;
        n /= 10;
    }

    return count;
}

int main(int argc, char * argv[]) {                           

     
    long long num, i, n, g;                                                                    
    std::cin >> num;
    g = 0;

                              
    if(0 < num && num <= 1000000) {                                                 
        for(long long i = 1; i <= num; i++) {                                         
            n = i;
            g = findCount(n) + g;
        }                                                                       
    }                                                                           

    std::cout << g;
    return 0;
                                                                  
}

1 Answer

0 votes
answered Oct 15, 2024 by Peter Minarik (101,340 points)
edited Oct 15, 2024 by Peter Minarik

Could you please provide the exact instructions/requirements?

But i also need to fulfill to goal that 50% of my answers of n less than or equal to 10^6. WHat does that mean and how can I achieve that?

This means nothing.

"50% of my answers of n less than or equal to 106 [does something/have some characteristics]". We need to know what this 50% should do, and what requirements they should fulfill.

Unless... you mean that "50% of my answers of n ARE less than or equal to 106", meaning that you need to list all the number of digits counter from 1 to X, where X ranges from 1 to n, and 50% of your digit counts must be less than 106 and the other 50% higher than 106.

So the following table would list the numbers and number of lines in the green section is the same as the number of lines in the orange section.

x (from 1 to n)numbers from 1 to xnumber of digits
111
2122
31233
...123......
? - 1123...(?-1)999,999
?132...?1,000,000
? + 1123...(?-1)?(?+1)1,000,001
...123......
END123...(?-1)?(?+1)...ENDEND_COUNT

The best option is to check your requirements again.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...