• UVA 10317


    Problem F

    Equating Equations

    Input: standard input

    Output: standard output

    Time Limit: 6 seconds

    Memory Limit: 32 MB

    Read equations with up to 16 terms and + and  operators (not unary) and reorder the terms (but not the operators) so that the equations hold. For example

     
       1 + 2 = 4 - 5 + 6

    is not a correct equation but the terms can be rearranged thus so it is:

     
       6 + 2 = 4 - 1 + 5

    Input

    Standard input consists of several pseudo-equations, one per line. Each pseudo-equation has up to 16 integer terms and each term is less than 100. Adjacent terms are separated by one operator, and spaces always appear surrounding the terms. There is exactly one = operator in the equation

    Output

    Your output will consist of one line per input line, with the same operators and the same terms. The order of the operators must be preserved, but the terms can be reordered so the equation holds. Any ordering such that the equation holds is correct. If there is more than one ordering any one of the orderings will do. If no ordering exists, a line containing

       no solution

    should be printed. One space should appear on either side of each operator and there should be no other spaces.

    Sample Input

    1 + 2 = 4 - 5 + 6
    1 + 5 = 6 + 7

    Sample Output

    6 + 2 = 4 - 1 + 5
    no solution

    (The Decider Contest, Source: Waterloo ACM Programming Contest)

    题意:给一个式子,符号位置不能变,数字位置可以变,求转换成正确等式。

    思路:由于只有加减,所以左边减号相当于右边加号,右边加号相当于左边减号,如此一来左右两边相等,为sum / 2, 然后去背包求sum / 2 的组成方法,然后按加减输出即可。

    代码:

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    
    const int N = 20;
    
    int num[N], numn, opern, Min, sum, jian, jia, jianz, jiaz, dp[N][N * 100], path[N][N * 100][N], pathn[N][N * 100], path2[N], pathn2, vis[105];;
    char oper[N], c, str[105];
    
    void init() {
        int Num = 0, flag = 0;
        numn = opern = Min = sum = jian = jia = jianz = jiaz = 0;
        for (int i = 0; i <= strlen(str); i ++) {
    	if (str[i] == '+' || str[i] == '=' || str[i] == '-') {
    	    if (str[i] == '+') {
    		jiaz ++;
    		jia ++;
    	    }
    	    if (str[i] == '-') {
    		jian ++;
    		jianz ++;
    	    }
    	    if (str[i] == '=') {
    		jia = 0;
    		jian = 0;
    	    }
    	    oper[opern++] = str[i];
    	}
    	else if (str[i] >= '0' && str[i] <= '9') {
    	    Num = Num * 10 + (str[i] - '0');
    	    flag = 0;
    	}
    	else {
    	    if (flag == 0) {
    		num[numn++] = Num;
    		sum += Num;
    		Num = 0;
    		flag = 1;
    	    }
    	}
        }
        Min = jiaz - jia + jian + 1;
    }
    
    void DP() {
        memset(dp, 0, sizeof(dp));
        memset(pathn, 0, sizeof(pathn));
        dp[0][0] = 1;
        for (int k = 0; k < numn; k ++) {
    	for (int i = Min; i >= 1; i --) {
    	    for (int j = sum; j >= num[k]; j --) {
    		if (dp[i - 1][j - num[k]] == 1) {
    		    dp[i][j] = 1;
    		    for (int l = 0; l < i - 1; l ++)
    		    path[i][j][l] = path[i - 1][j - num[k]][l];
    		    path[i][j][i - 1] = num[k]; pathn[i][j] = i;
    		}
    	    }
    	}
        }
    }
    
    void print() {
        pathn2 = 0;
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < numn; i ++)
    	vis[num[i]] ++;
        for (int i = 0; i < pathn[Min][sum]; i ++) {
    	vis[path[Min][sum][i]] --;
        }
        for (int i = 0; i < numn; i ++) {
    	if (vis[num[i]]) {
    	    path2[pathn2++] = num[i];
    	    vis[num[i]] --;
    	}
        }
        printf("%d", path[Min][sum][0]);
        int flag = 0; int s1 = 1, s2 = 0;
        for (int i = 0; i < opern; i ++) {
    	char c = oper[i];
    	if (flag == 0) {
    	    if (c == '+')
    		printf(" %c %d", c, path[Min][sum][s1++]);
    	    if (c == '-')
    		printf(" %c %d", c, path2[s2++]);
    	    if (c == '=') {
    		flag = 1;
    		printf(" %c %d", c, path2[s2++]);
    	    }
    	}
    	else {
    	    if (c == '+')
    		printf(" %c %d", c, path2[s2++]);
    	    if (c == '-')
    		printf(" %c %d", c, path[Min][sum][s1++]);
    	}
        }
        printf("
    ");
    }
    
    int main() {
        while (gets(str) != NULL) {
    	init();
    	if (sum % 2) printf("no solution
    ");
    	else {
    	    sum /= 2;
    	    DP();
    	    if (dp[Min][sum])
    		print();
    	    else printf("no solution
    ");
    	}
        }
        return 0;
    }


  • 相关阅读:
    运算符优先级(图表)
    VNC+SSH相关应用
    VNC常用操作及常见问题解决办法汇总
    操作系统(二): 进程与线程
    Java实现 蓝桥杯 算法提高 01背包
    Java实现 蓝桥杯 算法提高 01背包
    Java实现 蓝桥杯 算法提高 01背包
    Java实现 蓝桥杯 算法提高 判断名次
    Java实现 蓝桥杯 算法提高 判断名次
    Java实现 蓝桥杯 算法提高 日期计算
  • 原文地址:https://www.cnblogs.com/riasky/p/3473491.html
Copyright © 2020-2023  润新知