现在的位置: 首页 > 综合 > 正文

Codeforces Round #231 (Div. 2)

2014年10月31日 ⁄ 综合 ⁄ 共 2385字 ⁄ 字号 评论关闭

 A. Counting Sticks

When new students come to the Specialized Educational and Scientific Centre (SESC) they need to start many things from the beginning. Sometimes the teachers say (not always unfairly) that we cannot even count. So our teachers decided to teach us arithmetics
from the start. And what is the best way to teach students add and subtract? — That's right, using counting sticks! An here's our new task:

An expression of counting sticks is an expression of type:

A sticks][sign +][B sticks][sign =][C sticks] (1 ≤ A, B, C).

Sign + consists of two crossed sticks: one vertical and one horizontal. Sign = consists
of two horizontal sticks. The expression is arithmetically correct if A + B = C.

We've got an expression that looks like A + B = C given by counting sticks. Our task is to shift at most one stick (or we can shift nothing)
so that the expression became arithmetically correct. Note that we cannot remove the sticks from the expression, also we cannot shift the sticks from the signs + and =.

We really aren't fabulous at arithmetics. Can you help us?

Input

The single line contains the initial expression. It is guaranteed that the expression looks like A + B = C, where 1 ≤ A, B, C ≤ 100.

Output

If there isn't a way to shift the stick so the expression becomes correct, print on a single line "Impossible" (without the quotes). If there is a way, print
the resulting expression. Follow the format of the output from the test samples. Don't print extra space characters.

If there are multiple correct answers, print any of them. For clarifications, you are recommended to see the test samples.

题解:

加数棍子,只能移动一个,不可以移除出等式,使加法式子成立。

代码:

#include <stdio.h>
#include <string.h>

int main()
{
	char str[200000];
	int i,len, fir_num=0, sec_num=0, thr_num=0, tmp=0;
//	freopen("code.in", "r", stdin);
//	freopen("code.out", "w", stdout);
	memset(str, '\0', sizeof(str));
	scanf("%s", &str);
	len = strlen(str);
	for (i=0; i<len; i++)
	{
		if (str[i] == '+')
			fir_num = i;
		if (str[i] == '=')
			sec_num = i - fir_num - 1;
	}
	
	thr_num = len - (sec_num  + fir_num + 1)-1;
	// 满足正常的加法,输出
	if (fir_num + sec_num == thr_num)
	{
		printf("%s\n", str);
		return 0;
	}
	//加数比和小了2,和加一,加数减一
	else if (fir_num + sec_num == (thr_num-2))
	{
		thr_num = thr_num -1;
		fir_num = fir_num +1;
		for (i=0; i<fir_num; i++)
			printf("|");
		printf("+");
		for (i=0;i<sec_num; i++)
			printf("|");
		printf("=");
		for(i=0;i<thr_num; i++)
			printf("|");
		printf("\n");
		return 0;
	}
	// 加数比和大了2,和减一,加数加一 
	else if (thr_num == (sec_num + fir_num - 2))
	{
		if (fir_num >1 )
			fir_num = fir_num - 1;
		else
			sec_num = sec_num - 1;
		thr_num = thr_num + 1;
		for (i=0; i<fir_num; i++)
			printf("|");
		printf("+");
		for (i=0;i<sec_num; i++)
			printf("|");
		printf("=");
		for(i=0;i<thr_num; i++)
			printf("|");
		printf("\n");
		return 0;		
	}
	// 其他情况不可能
	else
	{
		printf("Impossible\n");
		return 0;
	}
}

抱歉!评论已关闭.