A. Lever
来源:互联网 发布:keynote有windows版吗 编辑:程序博客网 时间:2024/05/23 22:49
You have a description of a lever as string s. We'll represent the string length as record |s|, then the lever looks as a horizontal bar with weights of length |s| - 1 with exactly one pivot. We will assume that the bar is a segment on the Ox axis between points 0 and |s| - 1.
The decoding of the lever description is given below.
- If the i-th>^", that means that at coordinate i there is the pivot under the bar.
- If the i-th>=", that means that at coordinate i there is nothing lying on the bar.
- If the i-th character of the string equals digit c (1-9), that means that at coordinate i there is a weight of mass c on the bar.
Your task is, given the lever description, print if it will be in balance or not. Assume that the bar doesn't weight anything. Assume that the bar initially is in balance then all weights are simultaneously put on it. After that the bar either tilts to the left, or tilts to the right, or is in balance.
The first line contains the lever description as a non-empty string s (3 ≤ |s| ≤ 106), consisting of digits (1-9) and characters "^" and "=". It is guaranteed that the line contains exactly one character "^". It is guaranteed that the pivot of the lever isn't located in any end of the lever bar.
To solve the problem you may need 64-bit integer numbers. Please, do not forget to use them in your programs.
Print "left" if the given lever tilts to the left, "right" if it tilts to the right and "balance",>
=^==
balance
9===^==1
left
2==^7==
right
41^52==
balance
As you solve the problem, you may find the following link useful to better understand how a lever functions:http://en.wikipedia.org/wiki/Lever.
The pictures to the examples:
解题说明:此题为一道物理天平题,题目中标记为^的为支撑架所在的位置,=代表天平的杆,左右的数字代表天平上每个砝码的重量,学过物理的都知道天平两端力矩乘上砝码质量加和相等天平就平衡,否则按照加和大的那一侧倾斜。反应到程序中,就判断加和即可。
#include<iostream>#include<cstdio>#include<cmath>using namespace std;int main(){char s[1000008];int i, j, L1 = 1, L2 = 1;long long a1 = 0, a2 = 0;scanf("%s", s);for (i = 0; s[i] != '\n'; i++){if (s[i] == '^'){break;}}j = i;for (i = j - 1; i >= 0; i--) {if (s[i] != '='){a1 += (s[i] - 48)*L1;}L1++;}for (i = j + 1; s[i] != '\0'; i++) {if (s[i] != '='){a2 += (s[i] - 48)*L2;}L2++;}if (a1 > a2){printf("left\n");}else if (a1 < a2){printf("right\n");}else{printf("balance\n");}return 0;}
- A. Lever
- codeforces--12.24--A. Lever
- CF 221div2 A. Lever
- Lever
- Codeforces Round #221 (Div. 2) A. Lever
- B - Lever
- JDE报表-lever break
- 四级时间管理(Four Lever Time Manage)
- Performance Comparison Studies With Real-Life Scenarios in an Experimental Data Taking Context Lever
- a
- a
- a
- a
- a
- a
- a
- a
- a
- 黑马程序员---------------------在打印语句中如何打印这3个x变量?
- linux 下编译安装php mongodb扩展(佚名)
- 黑马程序员---------------------编写一个延迟加载的单例设计模式
- 黑马程序员_CSS
- poj 3259 Wormholes
- A. Lever
- 黑马程序员---------------------编程打印所有的3位质数 质数特点:只能被1和其本身整除
- 当您尝试查看 Web 页作为宿主的 IIS 6.0 上时出现错误消息: 页面无法显示"
- linux 下的mongodb安装
- 黑马程序员_java面向对象总结【4】(内部类、异常处理、Package)
- db2 insert时非原子批处理出现故障
- MySQL配置出现error nr. 1130问题的解决方法
- 黑马程序员---------------------数组去重
- 折半查找的迭代与递归实现