UVa-10317 - Equating Equations

一个即将高一的网友问的我的题。汗,我怎么大一才接触编程!

用STL写的,当时UVa挂了,没有去测试。之后发现TLE了,把vector改成数组依然如此。一时没想到怎么解决,先这样吧,以后再看看。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<sstream>
 6 using namespace std;
 7 const int maxx=20;
 8 int main()
 9 {
10     //freopen("in.txt","r",stdin);
11     //freopen("out.txt","w",stdout);
12     string s;
13     while(getline(cin,s))
14     {
15         stringstream ss(s);
16         int v1[maxx]={};
17         char v2[maxx]={};
18         int p1=0,p2=0;
19         bool flag=0;
20         int sum=0;
21         while(1)
22         {
23             flag=!flag;
24             if(flag)
25             {
26                 int t;
27                 ss>>t;
28                 if(ss.fail()) break;
29                 v1[p1++]=t;
30                 sum+=t;
31             }
32             else
33             {
34                 char t;
35                 ss>>t;
36                 if(ss.fail()) break;
37                 v2[p2++]=t;
38             }
39         }
40         while(1)
41         {
42             int num=0;
43             bool f1=0,f2=0;
44             int i=0,j=0;
45             num+=v1[i++];
46             for(;i<p1;i++,j++)
47             {
48                 if(v2[j]==‘+‘) f2=0;
49                 else f2=1;
50                 if(v2[j]==‘=‘)
51                 {
52                     f1=1;
53                     f2=0;
54                 }
55                 if(f1) f2=!f2;
56                 if(!f2) num+=v1[i];
57                 else num-=v1[i];
58             }
59             if(num==0)
60             {
61                 i=0;
62                 j=0;
63                 printf("%d",v1[i++]);
64                 for(;i<p1;i++,j++)
65                     printf(" %c %d",v2[j],v1[i]);
66                 break;
67             }
68             else
69             {
70                 if(!next_permutation(v1,v1+p1))
71                 {
72                     printf("no solution");
73                     break;
74                 }
75             }
76         }
77         printf("\n");
78     }
79 }
时间: 2024-10-11 16:46:27

UVa-10317 - Equating Equations的相关文章

UVA 10317 - Equating Equations 贪心 dfs

UVA 10317 - Equating Equations 贪心 dfs ACM 题目地址:UVA 10317 - Equating Equations 题意: 给一个等式,但是这个等式不一定是正确的,要你对等式中的数字重新排序,使得等式成立.等式只有+和-,数字个数小于16. 分析: 以a + b - c = d - e为例子. 1. 我们把等式右边的各项都换到左边,a + b - c - d + e = 0 2. 把+项和-项放一起,就变成(a + b + e) - (c + d) = 0

【UVA】10317 - Equating Equations(dfs + 剪枝)

真郁闷,一道普通的搜索题 我拿dp的方法去做,结果一直TLE和WA 如果所有数的和为奇数,肯定没有正解. 14133454 10317 Equating Equations Accepted C++ 0.102 2014-09-02 09:01:23 #include <iostream> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #incl

uva 103 Stacking Boxes(最长上升子序列)

Description  Stacking Boxes  Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions

uva 103 Stacking Boxes (DAG)

uva 103 Stacking Boxes Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when extended to arbitrary dimensions. Consider solving differential equations in several dimensions and a

UVA - 103 - Stacking Boxes (动态规划)

UVA - 103 Stacking Boxes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background Some concepts in Mathematics and Computer Science are simple in one or two dimensions but become more complex when

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED