How to read a large number of numbers?

I need to read a large number (up to 2 ^ 24) of large numbers (maximum 22 characters) in C / C ++. How to do this reading? The numbers are separated by a space, and I have to read everything to the next line. Use getch () and fill the array or is there a more reasonable solution?

+4
source share
3 answers

What are these numbers"? If they are integers, for such large numbers there is usually no built-in support in C; you will need a library to support "bignum". If they are floating point, you can try double , but the accuracy is likely to be insufficient.

It is difficult to be more specific without knowing more about your data, consider showing some numbers in the question, as this is very vague.

+4
source

If your reading is single-threaded, you can use getchar_unlocked() , which avoids the overhead of blocking the thread for each operation. Follow the symbol, copy the number and wait for the space. When you see a space, save the number and reset the current value:

 int *data = new int[MAX_LEN]; int *ptr = data; int ch; *ptr = 0; while ((ch = getchar_unlocked()) != '\n') { if (ch == ' ') { ptr++; *ptr = 0; } else { *ptr = (*ptr *10) + (ch -'0'); } } 

Of course, this snippet ignores "non-essential" things, such as error checking, but it is normal when your input is "sanitized". For example, something like this can be used to save I / O costs with the ACM online judge.

+2
source

First of all, you will need the correct type to store the integer, since it is more than 64 bits int, you probably need a structured type. You can either write your own or use a library, for example

http://gmplib.org/

read the documentation to use.

Then you will need to understand what you want to do with the numbers: add, convert, search or transfer? Once you know, you can work with the data type that you used for the integer.

0
source

Source: https://habr.com/ru/post/1438411/


All Articles