I was looking at this very easy problem (http://www.codechef.com/AUG13/problems/SPCANDY/ ) and decided that I should do it in C just so I could brush up my C. After numerous attempts, I wasn't able to solve the problem. I was able to solve it on my first attempt using C++. Could someone tell me what the difference between both of my solutions is? Could someone try submitting a C solution and show me what I'm doing wrong?
C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <stdio.h>
#include <stdint.h>
int main() {
uint8_t t, i;
uint64_t n, k;
scanf("%hhu", &t);
for (i = 0; i < t; i++) {
scanf("%lu %lu", &n, &k);
if (k == 0) {
printf("%u %lu\n", 0, n);
} else {
printf("%lu %lu\n", n / k, n % k);
}
}
}
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <stdint.h>
#include <iostream>
usingnamespace std;
int main() {
int t, i;
longlongint n, k;
cin >> t;
for (i = 0; i < t; i++) {
cin >> n >> k;
if (k == 0) {
cout << 0 << " " << n << endl;
} else {
cout << n / k << " " << n % k << endl;
}
}
}
I generally prefer to use specific uintX_t types, but I was having an issue with that with cin/cout. I tried the C code using int/long int and it still failed.
Link is fixed, the parenthesis became part of the link.
I did try one submission where I used int instead of uint8_t and long int instead of uint64_t, but I still got a wrong answer. The only other thing I changed in that submission was that the "hhu" format string became a "d".
The following worked fine. Note that %lu isn't necessarily the correct format specifier for a uint64_t. I don't see much point in using the uintXX_t types when there is no way to specify a corresponding type in scanf/printf. Using the %j format specifier and uintmax_t would make sense, but Code Chef's C compiler doesn't have C99 enabled. (I'm glad to see they finally put up a C++11 option.)
I don't see much point in using the uintXX_t types when there is no way to specify a corresponding type in scanf/printf.
Each of those types comes with corresponding defines that expand to the correct conversion specifiers for printf and scanf. PRId8, SCNiLEAST64, PRIxPTR, etc - they are in <cinttypes> / <inttypes.h>