字符串系列——自动物理题
2013-03-07 23:14
393 查看
Artificial Intelligence? |
Input
The first line of the input file will contain the number of test cases.Each test case will consist of one line containing exactly two data fields and some additional arbitrary words. A data field will be of the form I=xA, U=xV or P=xW, where x isa real number.Directly before the unit (A, V or W) one of the prefixes m (milli), k (kilo) and M (Mega) may also occur. To summarize it: Data fields adhere to the following grammar:DataField ::= Concept '=' RealNumber [Prefix] Unit Concept ::= 'P' | 'U' | 'I' Prefix ::= 'm' | 'k' | 'M' Unit ::= 'W' | 'V' | 'A'Additional assertions:The equal sign (`=') will never occur in an other context than within a data field.There is no whitespace (tabs,blanks) inside a data field.Either P and U, P and I, or U and I will be given.
Output
For each test case, print three lines:a line saying ``Problem #k" where k is the number of the test casea line giving the solution (voltage, power or current, dependent on what was given), written without a prefix and with two decimal places as shown in the sample outputa blank lineSample Input
3 If the voltage is U=200V and the current is I=4.5A, which power is generated? A light-bulb yields P=100W and the voltage is U=220V. Compute the current, please. bla bla bla lightning strike I=2A bla bla bla P=2.5MW bla bla voltage?
Sample Output
Problem #1 P=900.00W Problem #2 I=0.45A Problem #3 U=1250000.00V
很水的一题,却WA了几次,跑了好多博客找别人的代码来测试都觉得没问题,最后。。。
竟然是题目看错了!
我戳m竟然是毫,我一直以为m和M是一样都是百万。。。
终于AC。。。
代码:
#include<stdio.h>#include<string.h>int main(){int n, i, j;char ch[1000], temp;scanf("%d", &n);getchar();for (j = 0; j < n; j ++){double u = 0, I = 0, p = 0, num = 0;printf("Problem #%d\n", j + 1);for(i = 0; (ch[i] = getchar()) != '='; i ++);scanf("%lf", &num);scanf("%c", &temp);if (temp == 'M')num *= 1000000;if (temp == 'm')num *= .001;if (temp == 'k')num *= 1000;switch(ch[i - 1]){case 'U':u = num;break;case 'I':I = num;break;case 'P':p = num;break;}//second//printf("u = %lf i = %lf p = %lf\n", u, I, p);for(i = 0;(ch[i] = getchar()) != '='; i ++);scanf("%lf", &num);scanf("%c", &temp);if (temp == 'M')num *= 1000000;if (temp == 'm')num *= .001;if (temp == 'k')num *= 1000;switch(ch[i - 1]){case 'U':u = num;break;case 'I':I = num;break;case 'P':p = num;break;}//printf("u = %lf i = %lf p = %lf\n", u, I, p);//judge and calculateif (u > 0 && I > 0 && p == 0)printf("P=%.2lfW\n", u * I);if (p > 0 && I > 0 && u == 0)printf("U=%.2lfV\n", p / I);if (u > 0 && p > 0 && I == 0)printf("I=%.2lfA\n", p / u);printf("\n");}return 0;}[/code]
代码有点长,因为我录入时,用=判断了两次都是直接复制的。。。
相关文章推荐
- 字符串系列——自动物理题
- 字符串系列——uva10361 - 自动作诗机
- 字符串系列——uva10361 - 自动作诗机
- JAVA面试题解惑系列(六)——字符串(String)杂谈 转载自javaeye
- Linux基本操作系列(二):在CentOS 6.8系统上安装Tomcat并配置自动启动
- Delphi结构中使用String时遇到的内存泄露问题(没有利用String的引用计数自动销毁字符串的功能)
- 数字转字符串前面自动补0的实现
- Android学习系列(2)--App自动更新之通知栏下载
- QT 十六进制整数变为字符串自动补0 && 十进制补零
- 【我解C语言面试题系列】013 以单词为单位的翻转字符串
- 自动生成剩余编号字符串
- C/C++面试之算法系列--几个最大子字符串的算法题
- Spring3系列7-自动扫描组件或Bean
- 使用GSON来解析和生成JSON基础,通过传入List集合自动生成json字符串(一)
- char varchar nchar nvarchar 四者的区别是什么(为何SQL Server自动给字符串末尾加空格)
- 数据库改名系列(数据库名,逻辑名,物理文件名)
- 自动识别输入的字符串并转换成可单击的超链接
- 自动生成一个不重复的字符串
- jquery中focus()函数实现当对象获得焦点后自动把光标移到内容最后 //获取焦点后光标在字符串后 //其原理就是获得焦点后重新把自己复制粘帖一下 var t=$("#"+id).val()
- Android 系列 3.9使用崩溃报告自动获取错误报告