This issue has been discussed a lot in this mailing list.
You're using pow(), which is a floating point function. It doesn't give
you the correct result for large input.
Trying to stick with integer will save your life :)
Seedrick wrote:
> my code is the following. it does pretty much the same thing as
> described in the contest analysis. but i am getting incorrect outputs
> for some cases.
>
> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> #include<stdio.h>
> #include<stdlib.h>
> #include<math.h>
>
> #define REP(i,a,b) for(i=a;i<=b;i++)
> #define MAX(a,b) ((a)>(b)?(a):(b))
> #define MIN(a,b) ((a)<(b)?(a):(b))
> #define ABS(a) (a<0?a*-1:a)
> #define MALOC(type,n) ((type*)malloc(sizeof(type)*n))
>
> char in[100];
> char ar[] = {'1', '0', '2', '3', '4', '5', '6', '7', '8', '9', 'a',
> 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
> 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C',
> 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
> 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
> int l;
> char out[100];
> int counter;
>
> int convert(char c) {
> if (c >= '0' && c <= '9')
> return((c - '0'));
> else if (c >= 'a' && c <= 'z')
> return((c - 'a' + 10));
> else if (c >= 'A' && c <= 'Z')
> return((c - 'A' + 36));
> }
>
> int main() {
>
> FILE *fp = fopen("C:\\t3.in", "r");
> FILE *fp2 = fopen("C:\\t_res.out", "w");
>
> int i, j, k;
> int base;
>
> int noofinputs;
> fscanf(fp,"%d", &noofinputs);
> int no = noofinputs;
>
> double result;
>
> while (no--) {
> result = 0.0;
> counter = 0;
> fscanf(fp,"%s", in);
> strcpy(out, in);
> l = strlen(in);
> for (i = 0; i < l; i++) {
> for (j = 0; j < i; j++) {
> if (in[j] == in[i]) {
> out[i] = out[j];
> break;
> }
> }
> if (j == i || i == 0) {
> out[i] = ar[counter++];
> }
> }
>
>
>
> if (ar[counter] >= '0' && ar[counter] <= '9'){
> base = (ar[counter] - '0');
> }
> else if (ar[counter] >= 'a' && ar[counter] <= 'z')
> base = (ar[counter] - 'a' + 10);
> else if (ar[counter] >= 'A' && ar[counter] <= 'Z')
> base = (ar[counter] - 'A' + 36);
> if(base<=1) base=2;
>
>
> for (i = l - 1; i >= 0; i--) {
> result = result + (pow(base, l - 1 - i) * convert(out
> [i]));
> }
>
>
>
> fprintf(fp2,"Case #%d: %.0lf\n", noofinputs - no, result);
> }
> fclose(fp);
> fclose(fp2);
> return 0;
> }
>
> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> this image http://imgur.com/SMoEm.jpg shows the differences between
> the correct solution and my solution. [left hand side is correct
> solution]
>
> Does somebody know what the problem is?
> >
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"google-codejam" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-code?hl=en
-~----------~----~----~----~------~----~------~--~---