UVA 537 人工智能
来源:互联网 发布:公司网络整改方案 编辑:程序博客网 时间:2024/06/05 01:53
要点:
从字符串或者字符数组中读取 小数
采用sscanf可以实现 sscanf(const char *, format, &)
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <string>using namespace std;///#define INF 0xffffff7#define MAXN 200char problem[MAXN];char pp[MAXN];char uu[MAXN];char II[MAXN];double prefix[] = {0.001, 1000, 1000000};double P, U, I;void readin(char num[], int pos){int i, j;j = 0;for (i = pos; problem[i] != 'W' && problem[i] != 'A' && problem[i] != 'V'; i++){num[j++] = problem[i];}}double transfer(char num[]){int i, j;double mul = 1;double temp;int len = strlen(num);if (num[len - 1] == 'm'){mul = prefix[0];num[len - 1] = '\0';}else if (num[len - 1] == 'k'){mul = prefix[1];num[len - 1] = '\0';}else if (num[len - 1] == 'M'){mul = prefix[2];num[len - 1] = '\0';}sscanf(num, "%lf", &temp);temp *= mul;return temp;}int main(){///int i, j, th;int nCases;scanf("%d", &nCases);getchar();th = 1;while (nCases--){memset(pp, 0, sizeof(pp));memset(uu, 0, sizeof(uu));memset(II, 0, sizeof(II));gets(problem);int len = strlen(problem);P = U = I = 0;for (i = 0; i < len; i++){if (problem[i] == '='){switch(problem[i - 1]){case 'P':readin(pp, i + 1);break;case 'U':readin(uu, i + 1);break;case 'I':readin(II, i + 1);break;}}}printf("Problem #%d\n", th);if (strlen(pp) == 0){U = transfer(uu);I = transfer(II);P = U * I;printf("P=%.2lfW\n", P);}else if (strlen(uu) == 0){P = transfer(pp);I = transfer(II);U = P / I;printf("U=%.2lfV\n", U);}else if (strlen(II) == 0){P = transfer(pp);U = transfer(uu);I = P / U;printf("I=%.2lfA\n", I);}printf("\n");th++;} /// return 0;}
再贴一个
采用C++ isringstream 的方法,也比较好
#include<iostream>#include<sstream>#include<string>#include<cstdio>using namespace std;int main(){ /* freopen("data.in","r",stdin); freopen("data.out","w",stdout); //*/ int T; cin>>T;getchar(); for(int case_num=1;case_num<=T;case_num++) { string line; getline(cin,line); double P(-1.0),U(-1.0),I(-1.0); istringstream sin(line); char ch; while(sin>>ch) { if(ch=='P') { char t; sin>>t; if(t!='=') continue; sin>>P; sin>>t; if(t=='m') P/=1000; else if(t=='k') P*=1000; else if(t=='M') P*=1000000; } else if(ch=='U') { char t; sin>>t; if(t!='=') continue; sin>>U; sin>>t; if(t=='m') U/=1000; else if(t=='k') U*=1000; else if(t=='M') U*=1000000; } else if(ch=='I') { char t; sin>>t; if(t!='=') continue; sin>>I; sin>>t; if(t=='m') I/=1000; else if(t=='k') I*=1000; else if(t=='M') I*=1000000; } } cout<<"Problem #"<<case_num<<endl; if(P==-1.0) printf("P=%.2fW\n",U*I); else if(U==-1.0) printf("U=%.2fV\n",P/I); else printf("I=%.2fA\n",P/U); printf("\n"); } return 0;}
- UVA 537 人工智能
- UVa 537 人工智能
- UVa OJ 537 人工智能
- uva 537 人工智能
- 《人工智能》
- 人工智能!
- 人工智能
- 人工智能
- 人工智能
- 人工智能
- 人工智能
- 人工智能
- 人工智能
- 人工智能
- 人工智能
- 人工智能
- 人工智能
- 人工智能
- 变量转换二
- 非常实用的Ubuntu命令大全
- Codeforces Round 134 div 2 C题
- hdu 1754
- 单链表之约瑟夫环(Josephus)
- UVA 537 人工智能
- dedecms织梦系统不提取第一张图片为缩略图怎么办
- 将水晶报表移植到另一个项目时遇到的问题的解决办法
- 数论中的基础知识
- Android开源项目-Jamendo音乐播放器研究与优化-开篇
- poj 2365 Find a multiple
- 数论之因子的个数
- LDA主题模型简介
- 记录mini-task--代理邮件发收系统的开发过程(一)