C++语言: Codee#25706
001 /*
002 +++++++++++++++++++++++++++++++++++++++
003 author: chm
004 +++++++++++++++++++++++++++++++++++++++
005 */
006
007
008 #include <map>
009 #include <set>
010 #include <list>
011 #include <queue>
012 #include <cmath>
013 #include <stack>
014 #include <bitset>
015 #include <cstdio>
016 #include <cctype>
017 #include <vector>
018 #include <cstdlib>
019 #include <cstring>
020 #include <fstream>
021 #include <sstream>
022 #include <iomanip>
023 #include <iostream>
024 #include <algorithm>
025
026 using namespace std;
027
028 FILE* fin = stdin;
029 FILE* fout = stdout;
030 const int max_size = 10086;
031
032 double run(char* str, double& prefix)
033 {
034 char* pend;
035 double p = strtod(str, &pend);
036
037 if(*pend == 'm')
038 prefix = 0.001;
039 else if( *pend == 'M')
040 prefix = 1000000;
041 else if(*pend == 'k')
042 prefix = 1000;
043 return p;
044 }
045
046 int main()
047 {
048 #ifndef ONLINE_JUDGE
049 freopen("c:\\in.txt", "r", stdin);
050 fout = fopen("c:\\garage\\out.txt", "w");
051 #endif
052
053 /*
054 search P U I
055 get prefix
056 use formular
057 */
058 char str[max_size];
059 int n;
060 char* ptrp;
061 char* ptru;
062 char* ptri;
063 double prefixp;
064 double prefixu;
065 double prefixi;
066 double p, u, i;
067
068 scanf("%d\n", &n);
069 for(int j = 1; j <= n; ++j)
070 {
071 fgets(str, sizeof(str), stdin);
072 ptrp = strstr(str, "P=");
073 ptru = strstr(str, "U=");
074 ptri = strstr(str, "I=");
075 prefixp = prefixu = prefixi = 1.0;
076 fprintf(fout, "Problem #%d\n", j);
077 if(ptrp)
078 {
079 p = run(ptrp + 2, prefixp);
080 if(ptru) // i = p / u
081 {
082 u = run(ptru + 2, prefixu);
083 fprintf(fout, "I=%.2lfA\n\n", (prefixp / prefixu)* p / u);
084 }
085 else // u = p / i
086 {
087 i = run(ptri + 2, prefixi);
088 fprintf(fout, "U=%.2lfV\n\n", (prefixp / prefixi) * p / i);
089 }
090 }
091 else // p = u * i
092 {
093 u = run(ptru + 2, prefixu);
094 i = run(ptri + 2, prefixi);
095 fprintf(fout, "P=%.2lfW\n\n", i * u * prefixi * prefixu);
096 }
097 }
098
099 #ifndef ONLINE_JUDGE
100 fclose(fout);
101 system("c:\\garage\\check.exe");
102 #endif
103 return 0;
104 }
002 +++++++++++++++++++++++++++++++++++++++
003 author: chm
004 +++++++++++++++++++++++++++++++++++++++
005 */
006
007
008 #include <map>
009 #include <set>
010 #include <list>
011 #include <queue>
012 #include <cmath>
013 #include <stack>
014 #include <bitset>
015 #include <cstdio>
016 #include <cctype>
017 #include <vector>
018 #include <cstdlib>
019 #include <cstring>
020 #include <fstream>
021 #include <sstream>
022 #include <iomanip>
023 #include <iostream>
024 #include <algorithm>
025
026 using namespace std;
027
028 FILE* fin = stdin;
029 FILE* fout = stdout;
030 const int max_size = 10086;
031
032 double run(char* str, double& prefix)
033 {
034 char* pend;
035 double p = strtod(str, &pend);
036
037 if(*pend == 'm')
038 prefix = 0.001;
039 else if( *pend == 'M')
040 prefix = 1000000;
041 else if(*pend == 'k')
042 prefix = 1000;
043 return p;
044 }
045
046 int main()
047 {
048 #ifndef ONLINE_JUDGE
049 freopen("c:\\in.txt", "r", stdin);
050 fout = fopen("c:\\garage\\out.txt", "w");
051 #endif
052
053 /*
054 search P U I
055 get prefix
056 use formular
057 */
058 char str[max_size];
059 int n;
060 char* ptrp;
061 char* ptru;
062 char* ptri;
063 double prefixp;
064 double prefixu;
065 double prefixi;
066 double p, u, i;
067
068 scanf("%d\n", &n);
069 for(int j = 1; j <= n; ++j)
070 {
071 fgets(str, sizeof(str), stdin);
072 ptrp = strstr(str, "P=");
073 ptru = strstr(str, "U=");
074 ptri = strstr(str, "I=");
075 prefixp = prefixu = prefixi = 1.0;
076 fprintf(fout, "Problem #%d\n", j);
077 if(ptrp)
078 {
079 p = run(ptrp + 2, prefixp);
080 if(ptru) // i = p / u
081 {
082 u = run(ptru + 2, prefixu);
083 fprintf(fout, "I=%.2lfA\n\n", (prefixp / prefixu)* p / u);
084 }
085 else // u = p / i
086 {
087 i = run(ptri + 2, prefixi);
088 fprintf(fout, "U=%.2lfV\n\n", (prefixp / prefixi) * p / i);
089 }
090 }
091 else // p = u * i
092 {
093 u = run(ptru + 2, prefixu);
094 i = run(ptri + 2, prefixi);
095 fprintf(fout, "P=%.2lfW\n\n", i * u * prefixi * prefixu);
096 }
097 }
098
099 #ifndef ONLINE_JUDGE
100 fclose(fout);
101 system("c:\\garage\\check.exe");
102 #endif
103 return 0;
104 }