编程介的小学生 2017-06-04 03:01 采纳率: 20.5%
浏览 741
已采纳

Series / Parallel Resistor Circuits

Problem Description
A series/parallel resistor circuit is shown here.

The resistance value is given next to each resistor. Connection points (wires connecting two or more resistors together, are denoted by an uppercase letter. A and Z are reserved for the names of the connection points which are the endpoints of the circuit. Our goal is to calculate the equivalent resistance of the circuit (i.e., the equivalent resistance between A and Z).

Within the circuit, a resistor can be specified by a triple consisting of the connection points at either endpoint, and the resistance. The resistor labelled ``9" could be specified as either (C, D, 9) or (D, C, 9). A circuit specification is the set of all resistor specifications.

A pair of resistors is in series if one of either of their endpoints have a common connection point that is not use by any other resistor (e.g., the resistors labelled 6" and9" are both connected to C, which is not connected to anything else). Two series resistors can be replaced by an equivalent single resistor whose resistance is the sum of the replaced resistors (15, in the previous example).

A pair of resistors is in parallel if both their endpoints have common connection points (e.g., the resistors labelled 3" and10" above are both connected to R and D). Two parallel resistors can be replaced by an equivalent single resistor whose resistance is the inverse of the sum of the inverses of the two resistors ( (1/3 + 1/10)-1 = 2.307692 , in the previous example).

The equivalent resistance of a well-formed series-parallel resistor circuit can be determined by successively replacing a series or parallel resistor pair by the single equivalent resistor, until only one is left.

Not all circuits can be decomposed into series and parallel components. The Wheatstone Bridge, shown here, is a classic example of a circuit that is not considered a well-formed series-parallel resistor circuit.

Input
There will be multiple circuit specifications. The first input line for each circuit specification is an integer N (N < 1000 ), the number of resistors in the circuit. This is followed by N lines, each being a resistor specification in the form: X Y r , where X and Y are uppercase characters, and r is a positive integer resistance (r < 100 ). The equivalent resistance is guaranteed never to be greater than 100.

A circuit with N = 0 indicates the last circuit, and should not be processed.

Output
For each circuit, if the circuit is well-formed and reduces to a single equivalent resistance between A and Z, print the equivalent resistance of the circuit from A to Z, rounded to (and displayed to) 3 decimal places. If the circuit is not well formed, or if there is no equivalent resistance between A and Z, simply print the number `-1.000'. There should be no blank lines between outputs.

Sample Input
8
N R 2
D R 3
R N 2
R D 10
Z R 7
C D 9
N C 6
A N 4
2
A Z 3
Z A 10
2
P A 6
P Z 9
5
A B 1
B Z 4
A C 8
C Z 19
B C 12
0

Sample Output
11.945
2.308
15.000
-1.000

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-06-24 15:55
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)