Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
¿Pero esto qué es?
Interesante. Muy buena. Pero tal vez esa primera línea se podría hacer
int q[n = l < n? l : n] = {};
para conservar memoria y ciclos.
No lo he probado, solo es una idea.
C lang:
Submitted: {9848.07753012, 1736.48177667}
Expected: {9848.07753012, 1736.48177667}
Submitted: {781.444791958, 180.410745584}
Expected: {781.444791958, 180.410745584}
a b c d e f g ....
As you know in base 2 all odd numbers has the LSB (Least Significant Bit) set at 1:
0 : 0
1 : 1
2 : 10
3 : 11
4 : 100
5 : 101
[...]
On the other hand the number is given you as a string, aka a character sequence, and the characters are represented in ascii code, thats an integer. In the ascii table the digits representation starts with the 0, then 1, 2, and so on. The ascii code for '0' is 48, so while the digit is even his code is even too. That means I don't need to convert the character to its value to know if the digit is even or odd.
Well, I have an array of sums: sums[0] has the addition of all evens and sums[1] has the addition of all odds. I will use the digit itself to know if it is even or odd:
*digits - I take the digit of the string (his ascii code, remember)
& 1 - I take only his LSB. If it is 1 then the result of the operation is 1; If it is 0 then the result of the operation is 0. I use the result as the index of my array sums.
I think:
9 10
910
91 - 2 digits
Te la has jugado con out[1]. malloc no pone a 0 los valores de la memoria adquirida.
It's true! I just realized now. Thanks.
C lang. There is a badly planned test:
For number: 0 Expected: 0, 0
For number: 170232833617690725 Got: 76, 7
For number: 170232833617690725 Expected: 0, 16
Error. Expected 0, 16 but got 76, 7
The last two tests are the same but expect different solutions.
This comment is hidden because it contains spoiler information about the solution
Oka, since C99 yes, you can. But you can't define it:
int array[n]; // it's ok.
int array[n] = {0}; // this fails.
No tests for C.
The use of malloc is for dynamic allocation and needs to be freed.
if n==0 then result will be a dangling pointer in c.