codeforces怎么提交,codeforces 664B B. Rebus(亂搞題)

 2023-11-18 阅读 19 评论 0

摘要:題目鏈接: B. Rebus time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a rebus of form?? + ? - ? + ? = n, consisting of only question marks, separated by arithmetic

題目鏈接:

B. Rebus

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a rebus of form?? + ? - ? + ? = n, consisting of only question marks, separated by arithmetic operation '+' and '-', equality and positive integer?n. The goal is to replace each question mark with some positive integer from?1?to?n, such that equality holds.

?

Input

The only line of the input contains a rebus. It's guaranteed that it contains no more than?100?question marks, integer?n?is positive and doesn't exceed?1?000?000, all letters and integers are separated by spaces, arithmetic operations are located only between question marks.

?

Output

codeforces怎么提交、The first line of the output should contain "Possible" (without quotes) if rebus has a solution and "Impossible" (without quotes) otherwise.

If the answer exists, the second line should contain any valid rebus with question marks replaced by integers from?1?to?n. Follow the format given in the samples.

?

Examples
input
? + ? - ? + ? + ? = 42
output
Possible
9 + 13 - 39 + 28 + 31 = 42
input
? - ? = 1
output
Impossible
input
? = 1000000
output
Possible
1000000 = 1000000

題意:

給一個式子,讓你選[1,n]中間的數把問號替換掉,使這個式子成立;

思路:

分分情況亂搞亂搞就行了,一次A不掉多搞幾次就A了;

AC代碼:

/*2014300227    664B - 14    GNU C++11    Accepted    15 ms    2036 KB*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+5;
typedef long long ll;
const int mod=1e9+7;
char str[110];
int ans[110],an[110];
int main()
{int cnt=0;int num1=0,num2=0,n;char s;while(1){scanf("%c",&s);if(s=='+'){num1++;str[cnt++]=s;}else if(s=='-'){num2++;str[cnt++]=s;}else if(s=='=')break;}num1++;scanf("%d",&n);int l=num1-num2*n;int r=num1*n-num2;if(n>=l&&n<=r){printf("Possible\n");if(num1-num2>n){for(int i=0;i<num1;i++){ans[i]=1;}int number=(num1-n)/num2;for(int i=0;i<num2;i++){if(i<(num1-n)%num2)an[i]=number+1;else an[i]=number;}}else if(num2-num1>n){for(int i=0;i<num2;i++){an[i]=1;}int number=(num2+n)/num1;for(int i=0;i<num1;i++){if(i<(num2+n)%num1)ans[i]=number+1;else ans[i]=number;}}else{int number=(n+num2)/num1;for(int i=0;i<num1;i++){if(i<(n+num2)%num1)ans[i]=number+1;else ans[i]=number;}for(int i=0;i<num2;i++){an[i]=1;}}printf("%d ",ans[0]);int cut=1,cu=0;for(int i=0;i<cnt;i++){printf("%c ",str[i]);if(str[i]=='+'){printf("%d ",ans[cut++]);}else printf("%d ",an[cu++]);}printf("= %d",n);}else printf("Impossible\n");return 0;
}

?

轉載于:https://www.cnblogs.com/zhangchengc919/p/5400692.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/174744.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息