## 537 - Artificial Intelligence?

Moderator: Board moderators

wyvmak
Experienced poster
Posts: 110
Joined: Thu Dec 13, 2001 2:00 am
frustrated in WA. any tricks on that? will a test case takes more than one line? can anyone supply some tricky sample input?

Guru
Posts: 724
Joined: Wed Dec 19, 2001 2:00 am
Location: Germany
Test Input:
Fractal values for voltage U=1.111kV and current I=1.111kA are leading to what power?

wyvmak
Experienced poster
Posts: 110
Joined: Thu Dec 13, 2001 2:00 am
so is the output 0.00W?

Ilham Kurnia
New poster
Posts: 31
Joined: Sat Nov 17, 2001 2:00 am
Contact:
Nope.

P=1.00W

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan

### 537

It alway gets WA. But I don't know where the bugs are.
[c]
#include<stdio.h>
#include<string.h>
#define YES 1
#define NO 0
void main(void)
{
int amp,volt,power,ampcnt,voltcnt,powercnt,n,x,y,point;
double temp,v,p,a,b;
char buffer[10000];
scanf("%d",&n);
for(x=0;x<n;x++)
{
fflush(stdin);
fgets(buffer,10000,stdin);
buffer[strlen(buffer)-1]='\0';
amp=volt=power=ampcnt=voltcnt=powercnt=point=NO;
for(y=0,temp=0;buffer[y]!='\0';y++)
{
if(buffer[y]=='I' && y<strlen(buffer)-1)
if(buffer[y+1]=='=')
ampcnt=amp=YES;
if(buffer[y]=='U' && y<strlen(buffer)-1)
if(buffer[y+1]=='=')
voltcnt=volt=YES;
if(buffer[y]=='P' && y<strlen(buffer)-1)
if(buffer[y+1]=='=')
powercnt=power=YES;
if(ampcnt)
if(buffer[y]>='0' && buffer[y]<='9')
{
temp=temp*10+buffer[y]-'0';
if(point)
temp/=10;
}
else if(buffer[y]=='m')
temp/=1000;
else if(buffer[y]=='k')
temp*=1000;
else if(buffer[y]=='M')
temp*=1000000;
else if(buffer[y]=='.')
point=YES;
else if(buffer[y]=='A')
{
a=temp;
temp=0;
ampcnt=NO;
point=NO;
}
if(voltcnt)
if(buffer[y]>='0' && buffer[y]<='9')
{
temp=temp*10+buffer[y]-'0';
if(point)
temp/=10;
}
else if(buffer[y]=='m')
temp/=1000;
else if(buffer[y]=='k')
temp*=1000;
else if(buffer[y]=='M')
temp*=1000000;
else if(buffer[y]=='.')
point=YES;
else if(buffer[y]=='V')
{
v=temp;
temp=0;
voltcnt=NO;
point=NO;
}
if(powercnt)
if(buffer[y]>='0' && buffer[y]<='9')
{
temp=temp*10+buffer[y]-'0';
if(point)
temp/=10;
}
else if(buffer[y]=='m')
temp/=1000;
else if(buffer[y]=='k')
temp*=1000;
else if(buffer[y]=='M')
temp*=1000000;
else if(buffer[y]=='.')
point=YES;
else if(buffer[y]=='W')
{
p=temp;
temp=0;
powercnt=NO;
point=NO;
}
}
printf("Problem #%d\n",x+1);
if(amp && volt)
printf("P=%.2lfW\n\n",a*v);
if(amp && power)
printf("U=%.2lfV\n\n",p/a);
if(volt && power)
printf("I=%.2lfA\n\n",p/v);
}
}
[/c]

htl
Experienced poster
Posts: 185
Joined: Fri Jun 28, 2002 12:05 pm
Location: Taipei, Taiwan
But my program replies "P=1690000.00W"
I think the answer should be 1111*1111=1234321
Maybe it's the accuracy problem.

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:
Htl, you have right, answer in this case is 1234321

mie
New poster
Posts: 3
Joined: Sun Jan 05, 2003 1:45 pm

### 537 - Artificial Intelligence

why do i always get the compile error report when i send my source code..?

Here are the compiler error messages:

/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o: In function
`_start':
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.text+0x18):
undefined reference to `main'
collect2: ld returned 1 exit status

does any one can help me plz? thx

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:
are you sure, that your main function is called "main" (lower case) ?

Dominik

PS. If you wish, try to send me your code, or put it here ) If you wish - cut unnecessary functions ))

mie
New poster
Posts: 3
Joined: Sun Jan 05, 2003 1:45 pm

### emm..

here are the source code.. plz help me thxxxx..
@begin_of_source_code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* @JUDGE_ID: 27057TE 537 C */
#define INPUTFILE "AI.IN"
#define OUTPUTFILE "AI.OUT"

FILE *finput;
FILE *foutput;

void main() {
char input[100];
char num[100];
char ch[2];
char f=0;
char finished=0;
int i,j;
double P,I,U;
char vp,vi,vu;
char *pv;
double *p;
int problem=0;

printf("Artificial Intelligence\n");
printf("=======================\n\n\n");

printf("Open input file %s...\n",INPUTFILE);
finput = fopen(INPUTFILE,"rt");
if (finput == NULL) {
printf("Open File Fail.\n");
exit(EXIT_FAILURE);
}
printf("Open File Success.\n\n");

printf("Open output file %s...\n",INPUTFILE);
foutput = fopen(OUTPUTFILE,"wt");
if (foutput == NULL) {
printf("Open File Fail.\n");
exit(EXIT_FAILURE);
}
printf("Open File Success.\n\n");

printf("Processing input file...\n");

input[0]='\0';
i=0;
f=0;
finished=0;
vi=vp=vi=0;
while (finished==0) {
if (fgets(ch,2,finput)==NULL) {
finished = 1;
}
if (ch[0] == ' ' || ch[0] == ',' || ch[0] == '\n' || (finished)) {
input = '\0';
if (f==1 && strlen(input) > 0) {
printf("%s\n",input);

if (input[0]=='P') {
pv = &vp;
p = &P;
}else if (input[0] == 'U') {
pv = &vu;
p = &U;
}else {
pv = &vi;
p = &I;
}

*pv = 1;
for (i=0,j=0; i<strlen(input); i++) {
if (input >= '0' && input <= '9') {
num[j] = input;
j++;
}
}
num[j]='\0';
if (strlen(num)>0) { *p = strtod(num,NULL);

printf("P = %f, U = %f, I = %f\n",P,U,I);

}
}

if (ch[0] == '\n' || (finished)) {
problem++;
fprintf(foutput,"Problem #%d\n",problem);
if (vp == 0) {
P = U * I;
fprintf(foutput,"P = %lfW\n\n",P);
}else if (vu == 0) {
U = P / I;
fprintf(foutput,"U = %lfA\n\n",U);
}else {
I = P / U;
fprintf(foutput,"I = %lfV\n\n",I);
}
P=U=I=0;
vp=vu=vi=0;
}

i=-1;
f=0;
} else {
if (ch[0]=='=') {f=1;}
input=ch[0];
}

i++;
}

printf("Processing completed\n");
fclose(finput);
fclose(foutput);

}
@end_of_source_code

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:
I think, that your problem is using fopen, fclose and some other input/output functions ....

This function are not allowed on OJ
Try to avoid it - read from standard input and write to standard output )

if you want to test your progra under Dos/Windows/Unix run it in this way:

program_name <data.in >data.out
data.in, data.out - your name of files which you want to use to test solution
program_name - executable file name (example: 537.exe, a.out )

Try this
Dominik

Shahriar Nirjon
New poster
Posts: 16
Joined: Tue Dec 03, 2002 9:44 pm
You can try this. But..

3
I=-.002A. U=10V
I=.00A U=-0.00V
I=-1A U=2V

Shahriar Nirjon
New poster
Posts: 16
Joined: Tue Dec 03, 2002 9:44 pm

### AC

I got AC now :

* input may be like: I=-1e-2A U=2e3V
* donot print minus zero ie, -0.00 : print 0.00

deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

### 537 A.I pls help , I'm getting WA

someone pls give me input test case for this problem

one more question ==> is P,U,and I always be in uppercase
or it can be written in lowercase too ????

deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm
this is so frustrating, I already match your sample I/O but still got
WA