首页 > ACM题库 > HDU-杭电 > HDU 3082-Simplify The Circuit[解题报告]HOJ
2014
03-01

HDU 3082-Simplify The Circuit[解题报告]HOJ

Simplify The Circuit

问题描述 :

You may be puzzled by the complicated circuits ever. Now let us simplify them.
The task is: give you a parallel circuit(并联电路); you should calculate the resistance value (电阻)of it.
You may suppose the parallel circuit is made up of several simple series circuits(简单的串联电路).

输入:

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case starts with a line contains only one integer n (0 < n <= 20), indicating the number of simple series circuits. Next n lines are the descriptions of each simple series circuit. The wire is consisted by the character “-”. And resistances are show by positive integers, means their resistance values. The line’s length is shorter than 100 characters, and the count of resistance in each simple series circuit is no more than 20.
I assure all the calculations by integers using in this problem will not overflow the integer.

输出:

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case starts with a line contains only one integer n (0 < n <= 20), indicating the number of simple series circuits. Next n lines are the descriptions of each simple series circuit. The wire is consisted by the character “-”. And resistances are show by positive integers, means their resistance values. The line’s length is shorter than 100 characters, and the count of resistance in each simple series circuit is no more than 20.
I assure all the calculations by integers using in this problem will not overflow the integer.

样例输入:

2
1
---5----
2
----2----3-1----
---2---2-------2-

样例输出:

5.00
3.00

#include <iostream>
#include<string>
#include<vector>
#include <set>
#include <algorithm>
using namespace std;
double g(string s)
{
	double sum=0;
	for(int i=0;i<s.length();i++)
	 sum=sum*10+(s[i]-'0');
	return sum;
}



int main()
{
	
	
	
	cout.precision(2);
	int n;
	cin>>n;
	int m;
	for(int i=1;i<=n;i++)
	{
		cin>>m;
		string s;
		
	double sum=0;
		for(int j=1;j<=m;j++)
		{
			cin>>s;
			s=s+'-';vector<double>aa;
			double sum1=0;

			for(int k=0;k<s.length()-1;k++)
			{
				string t;int l;
				if(s[k]!='-')
				{
					t+=s[k];
					for(l=k;;l++)
					{
						if(s[l+1]=='-')
							break;
						else
						{
							t+=s[l+1];
						}
					}
				sum1+=g(t);
					k=l;
				}
				

					
			}
		double sum2=1/sum1;
			



 sum=sum+sum2;


		
		}
		
	
		cout<<fixed<<1/sum<<endl;


	}	


	return 0;
}