codeforces Round #347 (Div. 2) B

这种题代码都写残。。。。

思路就是先把每个数都记为1,统计正负数的和sum,如果sum大于n,则负数和q减1,加的时候类似。注意限制条件

比较水的题。。。。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<iostream>
 5 #include<queue>
 6 #include<stack>
 7 #include<cmath>
 8 #include<algorithm>
 9 #include<malloc.h>
10 using namespace std;
11 #define clc(a,b) memset(a,b,sizeof(a))
12 #define inf 0x3f3f3f3f
13 const int N=10010;
14 #define LL long long
15 const double eps = 1e-5;
16 const double pi = acos(-1);
17 // inline int r(){
18 //     int x=0,f=1;char ch=getchar();
19 //     while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
20 //     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
21 //     return x*f;
22 // }
23 char s[2];
24 int a[1010];
25 int v[1010];
26 int main(){
27     clc(a,0);
28     clc(v,0);
29     scanf("%s",s);
30     scanf("%s",s);
31     int p=1;
32     int q=0;
33     v[1]=1;
34     int k=1;
35     while(s[0]!='='){
36         if(s[0]=='+'){
37             v[++k]=1;
38             p++;
39         }
40         if(s[0]=='-'){
41             v[++k]=-1;
42             q++;
43         }
44         scanf("%s",s);
45         scanf("%s",s);
46     }
47     int n;
48     scanf("%d",&n);
49     for(int i=1;i<=k;i++){
50         a[i]=1;
51     }
52     int sum=p-q;
53     for(int i=1;i<=k;i++){
54         while(sum<n&&v[i]==1&&a[i]<n){
55             a[i]++,sum++;
56         }
57         while(sum>n&&v[i]==-1&&a[i]<n){
58             a[i]++,sum--;
59         }
60     }
61     if(sum!=n){
62         printf("Impossible
");
63         return 0;
64     }
65     printf("Possible
");
66     printf("%d ",a[1]);
67     for(int i=2;i<=k;i++){
68         if(v[i]==1)
69             printf("+ ");
70         else if(v[i]==-1){
71             printf("- ");
72         }
73        printf("%d ",a[i]);
74     }
75     printf("= %d
",n);
76     return 0;
77 }
View Code
原文地址:https://www.cnblogs.com/ITUPC/p/5414181.html