Codeforces Round #231 (Div. 2)
2014-02-21 15:40
204 查看
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;
}
}
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;
}
}
相关文章推荐
- 超级简单的DIV布局
- cf 127 div .1 Fragile Bridges(dp 好题)
- Codeforces Round #216 (Div. 2) C. Valera and Elections (树形dp)
- 怎么确定要对DIV设置什么CSS属性样式
- Codeforces Round #310 (Div. 2)
- Codeforces Round #340 (Div. 2)C. Watering Flowers(暴力)
- Topcoder SRM 543 DIV2 1000 EllysThreeRivers & DIV1 500 EllysRivers
- div中元素垂直居中的问题
- Codeforces Round #345 (Div. 2) D. Image Preview 前缀和
- DIV+CSS的叫法是不准确的
- 实现DIV层内的文字垂直居中
- Codeforces Round #216 (Div. 2) (又是线段树)
- div自适应宽度方法
- 选择器$("parent > child")、$("A B")、$("div#test")、$("div,#test")、$(".test","div")
- Codeforces Round #345 (Div. 2) B. Beautiful Paintings
- Codeforces Round #371 (Div. 2) C. Sonya and Queries (01trie树)
- JS使用cookie实现DIV提示框只显示一次的方法
- Codeforces Round #441 (Div. 1, by Moscow Team Olympiad) E. Delivery Club
- 不要滥用div,保持代码的整洁-----Coding Clean and Semantic Templates
- odeforces Round #144 (Div. 2) B