首页 > 代码库 > 求并联电阻值

求并联电阻值

求并联电阻值

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 28   Accepted Submission(s) : 15

Font: Times New Roman | Verdana | Georgia 

Font Size: ← →

Problem Description

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(简单的串联电路).

Input

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.

Output

For each case, please output the resistance value of the parallel circuit.

Sample Input

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

Sample Output

5.003.00

---------------------------------------------------------------------------------------------------------------------------------------------------------
解题思路:
  主要输入以字符串形式输入,将字符数字转换成整型数据时关键;
  ‘4‘ - ‘0‘ = 4;(将单个数字字符减去0字符,得到数字;
  主要还有多位数的判断;
 1 #include<stdio.h> 2 #include<string.h> 3 main() 4 { 5     int T,t,len,i,j,a; 6     double sum1,sum2,m; 7     char n[1000]; 8     scanf("%d",&T); 9     while(T--)10     {11         sum2=0;12         scanf("%d",&t);13         for(i=0;i<t;i++)14         {15             sum1=0;a=0;16             scanf("%s",&n);17             len=strlen(n);18             for(j=0;j<len;j++)19             {20                 if(n[j]!=-)21                 {22                     m = n[j]-0;      //将数字字符转换成纯数字;23                     a = a*10 + m;      //如果是相邻两个数字,要进行转换;24                 }25                 else26                 {27                     sum1 = sum1 + a;28                     a=0;29                 }30             }31             sum1 = sum1 + a;    //当最后一个数不是‘-’时,要加进sum1 ,否则会将最后一个数字漏掉32             sum2=sum2+1.0/sum1;33         }34         if(t==1)35         printf("%.2lf\n",sum1);36         else37         printf("%.2lf\n",1.0/sum2);38 39     }40 }