GYM

From Progteam

Revision as of 23:50, 19 June 2008 by Kerry (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Checkmark.jpg This problem has been solved by kerry.



GYM
Problem Number 2321
Sorter: kerry
Source: Unknown
Link: http://acm.pku.edu.cn/JudgeOnline/problem?id=2321



GYM is problem number 2321 on the Peking University ACM site.

Kerry's correct C answer. The old one looks so embaressing now!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main() {

int input[12][12], basket_count[12], line_count, i, j;
long double prob[12][12];
        for(i=0;i<12;i++) {
                basket_count[i]=0;
                for(j=0;j<12;j++) {
                        input[i][j]=0;
                        prob[i][j]=0.0;
                }
        }
        line_count=0;
        get_input(input, basket_count, &line_count);
        get_prob(input, basket_count, line_count, prob);
        calc_answer(prob, line_count);
}

int get_input(int input[12][12], int basket_total[12], int *line_count){
char a;
int i,j;
        a='a';
        i=j=0;
        while(1) {
                while(a!='\n') {
                        if(a==EOF)
                                return;
                        scanf("%d", &input[i][j]);
                        a=getchar();
                        basket_total[i]+=input[i][j];
                        j++;
                }
                *line_count+=1;
                i++;
                j=0;
                a='a';
        }
}

int get_prob(int input[12][12], int basket_total[12], int line_count, long double prob[12][12]) {
int i, j;

        for(i=0;i<line_count;i++) {
                for(j=0;j<12;j++) {
                        if(basket_total[i])
                                prob[i][j]=(float)input[i][j]/basket_total[i];
                }
        }
}

int calc_answer(long double prob[12][12], int line_count) {
long double answer[12][12];
int i, j, k;

        for(i=0;i<12;i++)
                for(j=0;j<12;j++)
                        answer[i][j]=0.0;
        answer[0][0]=1.0;

        for(i=1;i<10;i++) {
                for(j=0;j<line_count;j++) {
                        for(k=0;k<line_count;k++) {
                                answer[i][j]+=(answer[i-1][k]*prob[k][j]);
                        }
                        if(j==(line_count-1))
                                printf("%1.5Lf", answer[i-1][j]);
                        else
                                printf("%1.5Lf ", answer[i-1][j]);
                }
                printf("\n");
        }
        for(j=0;j<line_count;j++) {
                        if(j==(line_count-1))
                                printf("%1.5Lf\n", answer[9][j]);
                        else
                                printf("%1.5Lf ", answer[9][j]);
        }
}

Here's what I've got. PKU says Wrong Answer, but I can't figure out why. My input and out are below the code.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main() {

char *a, **b, *input[10];
int n, i, j, k, baskets[10][10]={0}, total[10]={0};
double prob[10][10]={0.0};
double answer[2][10]={{1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0},{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}};

        for (i=0;i<10;i++)
                input[i]=(char *)malloc(sizeof(char)*31);
        n=0;
        while (fgets(input[n++], 31,stdin)) {
        }

        n--;

        for (i=0;i<(n);i++) {
                a=input[i];
                for (j=0;j<(n);j++) {
                        baskets[i][j]=strtol(a, b, 10);
                        a=*b;
                        total[i]+=baskets[i][j];
                }
        }
        for(j=0;j<n;j++) {
                for(i=0;i<n;i++) {
                        if (total[i]!= 0)
                                prob[i][j] = (double) baskets[i][j]/total[i];

                }
                printf("%1.5f ", answer[0][j]);
        }
        printf("\n");
        for (k=0;k<9;k++) {
                for(i=0;i<n;i++) {
                        for(j=0;j<n;j++) {

                                answer[1][i]+=answer[0][j]*prob[j][i];
                        }
                        printf("%1.5f ", answer[1][i]);
                }
                for(i=0;i<n;i++) {
                        answer[0][i]=answer[1][i];
                        answer[1][i]=0;
                }
        printf("\n");
        }
}


Input:
1 5 7 10 2 6 2 1 2 0
1 0 1 2 9 1 10 5 9 0
1 0 0 0 1 1 1 1 1 1
1 1 1 1 9 3 2 4 5 4
1 5 7 10 2 6 2 1 2 0
1 0 1 2 9 1 10 9 9 0
1 0 0 0 1 1 1 1 1 1
1 1 1 1 3 3 2 3 9 3
1 0 0 0 1 1 1 1 1 1
1 1 1 1 9 3 1 4 5 4

Output:
1.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
0.02778 0.13889 0.19444 0.27778 0.05556 0.16667 0.05556 0.02778 0.05556 0.00000
0.06358 0.02156 0.03382 0.04838 0.20062 0.09513 0.14449 0.13888 0.17095 0.08258
0.06952 0.04615 0.06366 0.08851 0.14432 0.12513 0.10906 0.11314 0.15793 0.08258
0.06717 0.03950 0.05557 0.07759 0.15990 0.11646 0.11790 0.12106 0.16261 0.08224
0.06786 0.04127 0.05769 0.08043 0.15559 0.11885 0.11546 0.11890 0.16151 0.08244
0.06768 0.04078 0.05711 0.07965 0.15676 0.11820 0.11613 0.11950 0.16181 0.08239
0.06773 0.04091 0.05727 0.07986 0.15644 0.11838 0.11594 0.11933 0.16173 0.08240
0.06771 0.04088 0.05723 0.07980 0.15653 0.11833 0.11599 0.11938 0.16175 0.08240
0.06772 0.04089 0.05724 0.07982 0.15650 0.11835 0.11598 0.11937 0.16175 0.08240

Input:
2 1 2 0
1 0 1 2
1 0 0 0
1 1 1 1

Output:
1.00000 0.00000 0.00000 0.00000
0.40000 0.20000 0.40000 0.00000
0.61000 0.08000 0.21000 0.10000
0.49900 0.14700 0.28900 0.06500
0.54160 0.11605 0.25260 0.08975
0.52069 0.13076 0.26809 0.08046
0.52917 0.12425 0.26108 0.08549
0.52519 0.12721 0.26411 0.08350
0.52686 0.12591 0.26275 0.08448
0.52609 0.12649 0.26334 0.08408


Personal tools