求并联电阻值

求并联电阻值

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

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

Sample Output

5.00
3.00

----------------------------------------------------------------------------------------------------------------------------
解题思路:
  1.主要输入以字符串形式输入,将字符数字转换成整型数据时关键;
  2.'4' - '0' = 4;(将单个数字字符减去0字符,得到数字;
  3.还有多位数的判断;
 
  
 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                 else
26                 {
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         else
37         printf("%.2lf\n",1.0/sum2);
38 
39     }
40 }

 



转载于:https://www.cnblogs.com/yeshadow937/p/3876403.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值