Poj 1487 single-player games

Source: Internet
Author: User

The second question of Gaussian elimination, the coefficients of this multivariate one-time equations are floating-point numbers, and some modifications have been made to the Gaussian elimination template, but the main idea is that.


Question:

Give a number N, give a description of the First n letters, and ask the letter to get the expected value.


Solution:

According to the given description column equation. The main difficulty lies in string processing.


The following code is used:

# Include <set> # include <map> # include <queue> # include <math. h> # include <vector> # include <string> # include <stdio. h> # include <string. h> # include <stdlib. h >#include <iostream> # include <algorithm> # define EPS 1e-6 # define PI ACOs (-1.0) # define INF 107374182 # define inf64 1152921504606846976 # define LC l, m, tr <1 # define RC m + 1, R, TR <1 | 1 # define iabs (x)> 0? (X):-(x) # define clear1 (A, X, size) memset (A, X, sizeof (A [0]) * (size )) # define clearall (A, x) memset (A, X, sizeof (A) # define memcopy1 (A, X, size) memcpy (A, X, sizeof (X [0]) * (size) # define memcopyall (A, x) memcpy (A, X, sizeof (x) # define max (x, y) (x)> (y ))? (X): (y) # define min (x, y) (x) <(y ))? (X): (y) using namespace STD; struct node {double num [28]; // int CNT [28]; node () {clearall (Num, 0 ); // clearall (CNT, 0);} void clen () {clearall (Num, 0); // clearall (CNT, 0 );}}; struct node matrix [30]; int n, m, Len, P; bool free_x [305]; Double X [305]; inline double zero (Double X) {If (FABS (x)> EPS) return X; return 0;} void debug (void) {puts (""); int I, j; for (I = 0; I <m; I ++) {for (j = 0; j <n + 1; j ++) {Cout <matrix [I]. num [J] <";}cout <Endl ;}int guass () {int I, J, K, Col; clearall (X, 0); clearall (free_x, 1); // clears the solution set. All variables are marked as free variables // debug (); For (k = 0, Col = 0; k <M & Col <n; ++ K, ++ col) // enumerate the columns {int max_r = K; // find the row with the largest absolute value of the col column element and swap it with row K. (to reduce the error during Division) for (I = k + 1; I <m; ++ I) {If (iabs (Matrix [I]. num [col])> iabs (Matrix [max_r]. num [col]) max_r = I;} If (max_r! = K) // exchange {for (I = K; I <n + 1; ++ I) Swap (Matrix [K]. num [I], matrix [max_r]. num [I]);} If (iabs (Matrix [K]. num [col]) <EPS) // if all columns of the row should be 0, enumerate the next column {k --; continue;} for (I = k + 1; I <m; ++ I) // convert the col behind K to a step matrix {If (iabs (Matrix [I]. num [col])> EPS) {double D = matrix [I]. num [col]/matrix [K]. num [col]; for (j = Col; j <n + 1; ++ J) {matrix [I]. num [J] = matrix [I]. num [J]-D * matrix [K]. num [J] ;}}}// Deb UG (); // 1. Unsolvable: There are rows like (0, 0,..., A) in the simplified augmented array (! = 0). R ()! = R (A') unsolvable for (I = K; I <m; ++ I) {If (iabs (Matrix [I]. num [col])> EPS) Return-1;} // 2. infinite solution: in the N * (n + 1) augmented array (0, 0 ,..., 0) This indicates that no strict upper triangle array is formed. // The number of rows displayed is the number of free yuan. that is, R (A) = R (A') <N // printf ("% d \ n", k, n); If (k <n) {// The comments are free variables for multiple solutions. // first, there are n-k free variables, that is, there are at least n-k uncertain variables. int num = 0, freeidx; for (I = k-1; I> = 0; -- I) {num = 0; // determines the number of uncertain variable elements in the row, if there are more than one variable, it cannot be solved, and they are still variable elements of uncertainty. double TMP = ma Trix [I]. num [N]; // row I won't be (0, 0 ,..., 0), because the rows are from row K to Row M. // Similarly, row I will not be (0, 0 ,..., a),! If it is 0, there is no such solution. for (j = 0; j <n; ++ J) {If (iabs (Matrix [I]. num [J])> EPS & free_x [J]) {num ++; freeidx = J ;}} if (Num> 1) continue; // The fixed variable cannot be obtained. // there is only one uncertain variable free_index, so we can solve the variable and the variable is determined. TMP = matrix [I]. num [N]; for (j = 0; j <n; ++ J) {If (iabs (Matrix [I]. num [J])> EPS & J! = Freeidx) TMP-= matrix [I]. num [J] * X [J];} X [freeidx] = tmp/matrix [I]. num [freeidx]; free_x [freeidx] = 0;} return n-k;} // 3. unique Solution: a strict upper triangle array is formed in the N * (n + 1) augmented array. // calculate the Xn-1, Xn-2... x0.for (I = k-1; I> = 0; -- I) {double TMP = matrix [I]. num [N]; for (j = I + 1; j <n; ++ J) {TMP = TMP-matrix [I]. num [J] * X [J];} X [I] = tmp/matrix [I]. num [I];} return 0;} Char s [250]; void DFS (node & A) {node temp; I NT num, CNT = 0; while (s [p]! = ') {If (s [p]> = 'A' & S [p] <= 'Z') {. num [s [p]-'a'] + = 1; p ++; CNT ++;} else if (s [p] = '') P ++; else if (s [p] = '-') {P ++; num = 0; while (s [p]> = '0' & S [p] <= '9') {num * = 10; num + = s [p]-'0'; P ++;}. num [N]-= num; CNT ++;} else if (s [p]> = '0' & S [p] <= '9 ') {num = 0; while (s [p]> = '0' & S [p] <= '9') {num * = 10; num + = s [p]-'0'; P ++;}. num [N] + = num; CNT ++;} else if (s [p] = '(') {P ++; temp. clen (); DFS (temp); CNT ++; For (INT I = 0; I <= N; I ++) {. num [I] + = temp. num [I] ;}}}if (CNT) {for (INT I = 0; I <27; I ++) {. num [I]/= CNT ;}} P ++;/* printf ("% d \ n", CNT); For (INT I = 0; I <N; I ++) {for (Int J = 0; j <27; j ++) {If (J! = 0) printf (""); printf ("%. 2lf ",. num [J]);} puts ("");} */return;} int main () {// freopen ("data. out "," W ", stdout); int case1 = 1; while (scanf (" % d ", & N), n) {M = N; getchar (); clearall (matrix, 0); For (INT I = 0; I <n; I ++) {gets (s); Len = strlen (s); P = 0; while (s [p]! = '(') {P ++;} p ++; DFS (Matrix [I]); For (Int J = 0; j <n; j ++) {matrix [I]. num [J] =-matrix [I]. num [J];} matrix [I]. num [I] ++;} int sta = guass ();/* For (INT I = 0; I <n; I ++) {for (Int J = 0; j <= N; j ++) {If (J! = 0) printf (""); printf ("%. 2lf ", matrix [I]. num [J]);} puts ("");} puts (""); */printf ("game % d \ n", case1 ++ ); for (INT I = 0; I <n; I ++) {printf ("expected score for % C", I + 'A '); if (STA =-1) {puts ("undefined");} else {If (STA = 0) printf ("= %. 3lf \ n ", zero (X [I]); else if (free_x [I]) puts (" undefined "); else printf (" = %. 3lf \ n ", zero (X [I]) ;}} puts (" ");} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.