## 537 - Artificial Intelligence?

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 537 - Artificial Intelligence

Print a blank line at the end of the output.
Check input and AC output for thousands of problems on uDebug!

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Contact:

### Re: 537 - Artificial Intelligence

Guru,
It's not working.
I have test more with the following test cases:
Input:

Code: Select all

``````10
bla bla bla lightning strike I=2A bla bla bla P=2.5MW bla bla voltage?
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.
gU=200VI=4.5A.
gU=220VP=100.00W.
tP=2.5MWI=2A.
PU=200VUI=4.5A.5W
IP=100WPU=220VI.5A
bla bla bla P=lightning st200rike I=2eA 200bla bla bla P=2.5MW bla bl100a I=voltage?
P=1WU=1V
``````
Output:

Code: Select all

``````Problem #1
U=1250000.00V

Problem #2
P=900.00W

Problem #3
I=0.45A

Problem #4
P=900.00W

Problem #5
I=0.45A

Problem #6
U=1250000.00V

Problem #7
P=900.00W

Problem #8
I=0.45A

Problem #9
U=1250000.00V

Problem #10
I=1.00A

``````
I have made two program for this problem. But judge verdict give WA WA WA.
Program 1 with gets() function:

Code: Select all

``````Removed
``````
Program 2 with only string:

Code: Select all

``````#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
int T, cas=0;
//freopen("in-537.txt","r",stdin);
//freopen("out-537.txt","w",stdout);
double P=0.0, U=0.0, I=0.0, tmp=0.0;
scanf("%d\n",&T);
while(T)
{
char s, ch;
scanf("%s%c",s,&ch);
int ln=strlen(s), k=0, m=0, M=0, eq=0, PP=0, VV=0, II=0;
for(int i=0 ; i<ln ; i++)
{
if( (s[i]=='=') && (s[i-1]=='I' || s[i-1]=='P'|| s[i-1]=='U') && (s[i+1]>='0' && s[i+1]<='9') ) eq++;
}
float d=1.0;
if(eq>0)
{
long nm=0, dv=0, fag=0;
for(int i=0 ; i<ln ; i++)
{
if(s[i]=='m') m=1;
if(s[i]=='M') M=1;
if(s[i]=='k') k=1;
if(s[i]=='P' && s[i+1]=='=') PP=1;
if(s[i]=='U' && s[i+1]=='=') VV=1;
if(s[i]=='I' && s[i+1]=='=') II=1;

if( s[i]>='0' && s[i]<='9' )
{
if(fag==1){dv=dv*10; d=(float)dv;}
nm=nm*10+(int)s[i]-48;
}
if( s[i]=='.' )
{
dv=1;
fag=1;
}

if(s[i]=='A' || s[i]=='W' || s[i]=='V')
{
tmp=(float)nm/d;
//printf("%lf m=%d M=%d k=%d\n",tmp,m,M,k);
if(PP==1){
P=tmp;
if(m==1) P=P/1000.0;
else if(M==1) P=P*1000000.0;
else if(k==1) P=P*1000.0;
}
else if(VV==1){
U=tmp;
if(m==1) U=U/1000.0;
else if(M==1) U=U*1000000.0;
else if(k==1) U=U*1000.0;
}
else if(II==1){
I=tmp;
if(m==1) I=I/1000.0;
else if(M==1) I=I*1000000.0;
else if(k==1) I=I*1000.0;
}
PP=0; VV=0; II=0; k=0; m=0; M=0;
nm=0; dv=0; fag=0; d=1.0;
}
}
}

if(ch=='\n' || ln==0){
if(cas>0) printf("\n");
printf("Problem #%d\n",++cas);
if(P==0.0){
P=U*I;
printf("P=%.2lfW\n",P);
}
else if(U==0.0){
U=P/I;
printf("U=%.2lfV\n",U);
}
else if(I==0.0){
I=P/U;
printf("I=%.2lfA\n",I);
}
P=0.0; U=0.0; I=0.0; tmp=0.0;
T--;
}
}
return 0;
}

``````
I am frustrated.
Last edited by uvasarker on Tue Aug 07, 2012 8:36 am, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 537 - Artificial Intelligence

I only looked at your first program. Try input:
I=1A P=0W

Don't use floats, stick with doubles.
Check input and AC output for thousands of problems on uDebug!

uvasarker
Learning poster
Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm
Contact:

### Re: 537 - Artificial Intelligence

Thanks Guru,
Finally, I got accccc after 10 submissions,
using double.

lasia
New poster
Posts: 4
Joined: Tue Mar 05, 2013 11:13 am

Code: Select all

``````#include<stdio.h>
#include<string.h>
#define MAXN 500+2

char line1[MAXN];
double num = {0.0, 0.0};
char unit = {'0', '0'};
char prefix = {'0', '0'};

void dataProcess(char unit1, double num1, char unit2, double num2)
{
if(unit1 == 'W' && unit2 == 'V')
printf("I=%.2lfA\n", (1.0 * num1) / (1.0 * num2));
else if(unit1 == 'V' && unit2 == 'W')
printf("I=%.2lfA\n", (1.0 * num2) / (1.0 * num1));
else if(unit1 == 'W' && unit2 == 'A')
printf("U=%.2lfV\n", (1.0 * num1) / (1.0 * num2));
else if(unit1 == 'A' && unit2 == 'W')
printf("U=%.2lfV\n", (1.0 * num2) / (1.0 * num1));
else
printf("P=%.2lfW\n", 1.0 * num1 * num2);
}

double conversion(double num, char prefix)
{
switch(prefix){
case 'm':
num *= 0.001;
case 'k':
num *= 1000;
case 'M':
num *= 1000000;
}
return num;
}

int main()
{
int nTest = 0;
int i = 0, j = 1;
scanf("%d", &nTest);
getchar();
while(nTest--){
i = 0;
memset(num, 0.0, sizeof(num));
memset(unit, 0, sizeof(unit));
memset(prefix, 0.0, sizeof(prefix));

fgets(line1, MAXN, stdin);

char * pch;
pch = strpbrk (line1, "=");
while (pch != NULL)
{
if(unit[i] == 'm'|'k'|'M'){
sscanf(pch+1, "%lf %[mkM] %[VAW]", &num[i], &prefix[i], &unit[i]);
num[i] =  conversion(num[i], prefix[i]);
}
pch = strpbrk (pch+1,"=");
i++;
}

printf("Problem #%d\n", j++);
dataProcess(unit, num, unit, num);
if(nTest)
printf("\n");

}

return 0;
}``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Print a blank line after the last test case.
Check input and AC output for thousands of problems on uDebug!

lasia
New poster
Posts: 4
Joined: Tue Mar 05, 2013 11:13 am

brianfry713 wrote:Print a blank line after the last test case.
I did but still WA. Any other suggestion?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Your code doesn't match the sample I/O.
Check input and AC output for thousands of problems on uDebug!

lasia
New poster
Posts: 4
Joined: Tue Mar 05, 2013 11:13 am

brianfry713 wrote:Your code doesn't match the sample I/O.
It did match on my pc though. Would you please paste the output on your machine? Thank you.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

https://ideone.com/kHaBHL

Your output for the sample input:

Code: Select all

``````Problem #1
P=900.00W

Problem #2
P=22000.00W

Problem #3
P=5000000.00W``````
Check input and AC output for thousands of problems on uDebug!

kamia
New poster
Posts: 2
Joined: Thu Mar 14, 2013 3:03 pm

I have tried several test cases, but still fail to ac, any idea? Thanks a lot!! Code: Select all

``````#define RUN
#ifdef RUN

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <cctype>
#include <algorithm>
#include <utility>
#include <math.h>

using namespace std;

#define MAXN 105
string buf;

double getValue(int loc){
int i;
string val;
double c = 1;
for(i=loc+1; i<buf.length(); i++){
if(isdigit(buf[i]) || buf[i]=='.' || buf[i]=='-' || buf[i]=='e' ){
val += buf[i];
}
else if(buf[i] == 'm'){
c = 1e-3;
}
else if(buf[i] == 'k'){
c = 1e3;
}
else if(buf[i] == 'M'){
c = 1e6;
}
else{
break;
}
}

//cout << "val.c_str():" << val.c_str() << endl;
double ret = atof(val.c_str())*c;

if(ret == 0){
ret = 0;
}
return ret;
}

void play(){
//cout << buf << endl;

int loc1 = buf.find("=");
//cout << loc1 << endl;

int loc2 = buf.find("=", loc1+1);
//cout << loc2 << endl;

bool useU = false, useI = false, useP = false;
double U = 0, I = 0, P = 0;
switch (buf[loc1-1])
{
case 'U':
U = getValue(loc1);
useU = true;
break;
case 'I':
I = getValue(loc1);
useI = true;
break;
case 'P':
P = getValue(loc1);
useP = true;
break;
}

switch (buf[loc2-1])
{
case 'U':
U = getValue(loc2);
useU = true;
break;
case 'I':
I = getValue(loc2);
useI = true;
break;
case 'P':
P = getValue(loc2);
useP = true;
break;
}

if(!useU){
printf("U=%.2lfV\n", (P/I));
}
else if(!useI){
printf("I=%.2lfA\n", (P/U));
}
else if(!useP){
printf("P=%.2lfW\n", (U*I));
}
}

int main(){

#ifndef ONLINE_JUDGE
freopen("537.in", "r", stdin);
freopen("537.out", "w", stdout);
#endif

int n;
bool p = false;
scanf("%d", &n);
getchar();
for(int i=0; i<n; i++){
if(p){
printf("\n");
}
getline(cin, buf);
//cout << buf << endl;
printf("Problem #%d\n", i+1);
play();
p = true;
}

}

#endif``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Print a blank line after the last test case.
Check input and AC output for thousands of problems on uDebug!

kamia
New poster
Posts: 2
Joined: Thu Mar 14, 2013 3:03 pm

brianfry713 wrote:Print a blank line after the last test case.
Brilliant!! I thought I only need to print out only one blank line after line test case, but in fact, I need to print 2 blank lines!

Thanks again!

fubar
New poster
Posts: 2
Joined: Sat Mar 30, 2013 8:39 am

### 537 - Artificial Intelligence?

Hi Everyone, my program works well with the test cases in the UVA page but when I submit the problem I get WA, why? Thanks a lot.
Edit: AC, Thanks
Last edited by fubar on Mon Jul 28, 2014 8:26 am, edited 2 times in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: WA-537-Artificial Intelligence?

Doesn't match the sample I/O.
Check input and AC output for thousands of problems on uDebug!