537 - Artificial Intelligence?

All about problems in Volume 5. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Fri Feb 21, 2003 10:09 am

Can your program handle input like:

Code: Select all

2
xxxxxxx xxxx xxxx I=0.0000000000000001MA yyy yyy U=1000000000000000.000000000000mV zzz
Can you prove P>1mW for I=1A and U=1V?

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

Post by deddy one » Fri Feb 21, 2003 8:15 pm

never mind , I've found the bug , and it's rather stupid really.
well , got Acc now :D

thank you everyone for your help

zzylhy
New poster
Posts: 6
Joined: Mon May 19, 2003 1:56 pm

537

Post by zzylhy » Tue May 20, 2003 1:19 pm

#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<math.h>

int main()
{
int n,count=0,k,len,fi,fp,fu,j;
long double p,u,i;
char s[100];
scanf("%d\n",&n);
while(n>0)
{
count++;
n--;
fi=fu=fp=0;
gets(s);
len=strlen(s);
k=0;
p=u=i=0;
while(k<len)
{
if((s[k]=='I')&&(s[k+1]=='='))
{
fi=1;
k+=2;
while(s[k]>='0'&&s[k]<='9')
{
i=i*10+s[k]-'0';
k++;
}
if(s[k]=='.')
{
j=-1;
k++;
while(s[k]>='0'&&s[k]<='9')
{
i=i+(s[k]-'0')*pow(10,j);
j--;
k++;
}
}
if(s[k]=='m')
i=i/1000;
if(s[k]=='M')
i=i*1000000;
if(s[k]=='k')
i=i*1000;
}
if((s[k]=='U')&&(s[k+1]=='='))
{
fu=1;
k+=2;
while(s[k]>='0'&&s[k]<='9')
{
u=u*10+s[k]-'0';
k++;
}
if(s[k]=='.')
{
j=-1;
k++;
while(s[k]>='0'&&s[k]<='9')
{
u=u+(s[k]-'0')*pow(10,j);
j--;
k++;
}
}
if(s[k]=='m')
u=u/1000;
if(s[k]=='M')
u=u*1000000;
if(s[k]=='k')
u=u*1000;
}
if((s[k]=='P')&&(s[k+1]=='='))
{
fp=1;
k+=2;
while(s[k]>='0'&&s[k]<='9')
{
p=p*10+s[k]-'0';
k++;
}
if(s[k]=='.')
{
j=-1;
k++;
while(s[k]>='0'&&s[k]<='9')
{
p=p+(s[k]-'0')*pow(10,j);
j--;
k++;
}
}
if(s[k]=='m')
p=p/1000;
if(s[k]=='M')
p=p*1000000;
if(s[k]=='k')
p=p*1000;
}
k++;
}
if(fi==1&&fu==1)
{
printf("Problem #%d\n",count);
printf("P=%.2lfW\n\n",i*u);
}
if(fi==1&&fp==1)
{
printf("Problem #%d\n",count);
printf("U=%.2lfV\n\n",p/i);
}
if(fu==1&&fp==1)
{
printf("Problem #%d\n",count);
printf("I=%.2lfA\n\n",p/u);
}
}
return 0;
}


Always WA.
Can you help me?

User avatar
the LA-Z-BOy
Learning poster
Posts: 94
Joined: Wed Jul 31, 2002 12:44 pm
Location: Dacca, Bangladesh
Contact:

Post by the LA-Z-BOy » Tue Jun 17, 2003 1:14 pm

hi zzyhly, no problem with your code... use float instead of long double you'll get accepted...
but if you want to get accepted with long double then you have to use printf %Lf, not %lf...

Code: Select all

		if(fi==1&&fu==1) 
		{ 
			printf("Problem #%d\n",count); 
			printf("P=%.2LfW\n\n",i*u); 
		} 
		if(fi==1&&fp==1) 
		{ 
			printf("Problem #%d\n",count); 
			printf("U=%.2LfV\n\n",p/i); 
		} 
		if(fu==1&&fp==1) 
		{
			printf("Problem #%d\n",count); 
			printf("I=%.2LfA\n\n",p/u); 
		}
Greetings
Istiaque Ahmed [the LA-Z-BOy]

jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US
Contact:

judge says compile error, but I'm sure it works.

Post by jjtse » Tue Aug 23, 2005 9:04 am

Hi,

I have this piece of code that the online judges keep on saying it doesn't compile. I wrote one in C and one in c++. They both work fine in my pc and in my school pc. I believe we both have gcc 3.2 Can someone compile the following 2 sets of code and confirm that it does compile?

The first code is in c++ and compiles by g++.
The second code is in C and compiles by gcc. Thanks

by the way, this is for the Aritificial intelligence problem v5/537

Code: Select all


#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

void readVal(int);        //reads converts, and stores numbers into p,u, or i.
void calculate();         //calculates the missing variable and outputs to screen

float p, u, i;
char used[2];             //2 characters to indicate the variables already obtained
int counter;              //keep track of how many variables obtained.
string line;              //holds the contents of the entire line

int main(){
  
  int cases, k, j;

  cin>>cases;
  getline(cin, line);

  for (k=0; k<cases; k++){
    cout<<"Problem #"<<k+1<<endl;
    getline(cin, line);

    j=0;
    counter = 0;
    p=0;
    i=0;
    u=0;


    //This takes into account if the first character in the line is a variable P,I, or U
    if (line[j]=='P' || line[j]=='I' || line[j]=='U'){
      if (line[j+1] == '='){
	readVal(j);
	j=j+3;
      }
    }

    while (counter< 2) {
      if (line[j] == ' '){
	j++;
	if (line[j]=='P' || line[j]=='I' || line[j]=='U'){
	  if (line[j+1] == '='){
	    readVal(j);
	    j=j+3;
	  }
	}
      }
      else
	j++;
    }//end while

    calculate();

  }//end for


  return 0;
}



void readVal(int k){

  char c[20];
  float num;
  int j;        //used to index char c[]

  if (counter == 0){
    used [0] = line[k];
  }
  else{
    used [1] = line[k];
  }

  counter++;
  k=k+2;
  j= 0;
  c[j] = line[k];

  while (c[j] != 'M' && c[j] != 'm' && c[j] != 'k' && c[j] != 'W' && c[j] != 'V' && c[j] != 'A'){
    j++;
    k++;
    c[j] = line[k];
  }//end while


  //takes into account the prefixes, if any
  num = atof(c);
  if (c[j] == 'M')
    num = num * 1000000;
  else if (c[j] == 'm')
    num = num / 1000;
  else if (c[j] == 'k')
    num = num * 1000;
  
  if (counter == 1){
    if (used[0] == 'P')
      p = num;
    else if(used[0] == 'I')
      i= num;
    else
      u=num;
  }
  else {
    if (used[1] == 'P')
      p = num;
    else if(used[1] == 'I')
      i= num;
    else
      u=num;
  }
}//end readVal;



void calculate(){

  if (used[0] !='P' && used[1] != 'P'){
    p=i*u;
    cout<<"P="<<fixed<<setprecision(2)<<p<<"W"<<endl<<endl;
  }
  else if (used[0] !='I' && used[1] != 'I'){
    i=p/u;
    cout<<"I="<<fixed<<setprecision(2)<<i<<"A"<<endl<<endl;
  }
  else{
    u=p/i;
    cout<<"U="<<fixed<<setprecision(2)<<u<<"V"<<endl<<endl;
  }
}


this one is written in C and uses gcc to compile

Code: Select all


#include <stdio.h>
#include <stdlib.h>


void readVal(int);        //reads converts, and stores numbers into p,u, or i.
void calculate();         //calculates the missing variable and outputs to screen

float p, u, i;
char used[2];             //2 characters to indicate the variables already obtained
int counter;              //keep track of how many variables obtained.
char line[200];              //holds the contents of the entire line

int main(){
  
  int cases, k, j;

  fgets(line, 200, stdin);
  cases = atoi (line);

  for (k=0; k<cases; k++){
    printf("%s%d\n", "Problem #" ,k+1);
    bzero(line, sizeof(line));
    fgets(line, 200, stdin);

    j=0;
    counter = 0;
    p=0;
    i=0;
    u=0;


    //This takes into account if the first character in the line is a variable P,I, or U
    if (line[j]=='P' || line[j]=='I' || line[j]=='U'){
      if (line[j+1] == '='){
	readVal(j);
	j=j+3;
      }
    }

    while (counter< 2) {
      if (line[j] == ' '){
	j++;
	if (line[j]=='P' || line[j]=='I' || line[j]=='U'){
	  if (line[j+1] == '='){
	    readVal(j);
	    j=j+3;
	  }
	}
      }
      else
	j++;
    }//end while

    calculate();


  }//end for

  return 0;
}



void readVal(int k){

  char c[20];
  float num;
  int j;

  if (counter == 0){
    used [0] = line[k];
  }
  else{
    used [1] = line[k];
  }

  counter++;
  k=k+2;
  j= 0;
  c[j] = line[k];

  while (c[j] != 'M' && c[j] != 'm' && c[j] != 'k' && c[j] != 'W' && c[j] != 'V' && c[j] != 'A'){
    j++;
    k++;
    c[j] = line[k];
  }


 
  num = atof(c);
  if (c[j] == 'M')
    num = num * 1000000;
  else if (c[j] == 'm')
    num = num / 1000;
  else if (c[j] == 'k')
    num = num * 1000;
  
  if (counter == 1){
    if (used[0] == 'P')
      p = num;
    else if(used[0] == 'I')
      i= num;
    else
      u=num;
  }
  else {
    if (used[1] == 'P')
      p = num;
    else if(used[1] == 'I')
      i= num;
    else
      u=num;
  }
}


void calculate(){


  if (used[0] !='P' && used[1] != 'P'){
    p=i*u;
    printf("%s%.2f%s\n\n", "P=",p,"W");
  }
  else if (used[0] !='I' && used[1] != 'I'){
    i=p/u;
    printf("%s%.2f%s\n\n", "I=",i,"A");
  }
  else{
    u=p/i;
    printf("%s%.2f%s\n\n", "U=",u,"V");
  }
}



The site said they'll e-mail any compile error messages, but I never received them. Then again, when I type in my ID to look at my status, someone else's name came up.

Thanks for your help.

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » Tue Aug 23, 2005 10:24 am

to the first code:
from (MSVC++)
x.cpp(20) : error C2065: 'getline' : undeclared identifier

to the second code:
in C comments like // are forbidden

So both codes are wrong :)
BTW. Use compiler option -ansi or something like that to check "is this ANSI C/C++ code" . Only such code will be compiled by judge software.

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US
Contact:

Post by jjtse » Tue Aug 23, 2005 6:41 pm

THanks for the reply.

As for the first code, there's nothing wrong with it. getline() is not an identifier. It calls a build in function to read the entire line of input. getline is commonly used. I don't know if it's part of the ansi version though, but when I compiled it with the -ansi, it didn't give me any error messages. When I tried to submit it, it still gave a compile error. I don't know what to do. Probably I can take out the getline function, but then it will make the job a lot more harder. Getline should've worked anyway.


as for the second code, I took out all the comments as suggested and compiled with -ansi . It gave no errors. I submitted it and was accepted. So I'm very happy about that. Thanks DM. You are very helpful. At least now I know it works in C, so I'll just program stuff in C next time.


JT

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » Tue Aug 23, 2005 10:06 pm

In C++ you must include proper header file, which defines used function. In my compiler getline() function wasn't defined, so I think that:
1. you use different set of headers
2. you don't set all options of compiler (I heard about something like gcc -wall -strict -ansi file.c to compile - check options of your compiler).
Maybe it helps you.

About C code. '//' comments are defined as comments in ANSI C++ not in ANSI C (I think that version of this standard is used by judge - I know that from my submissions). BUt I know from another thread that now ANSI C allow to use '//' comments in C code. Are you sure that you send C code as C, not C++ ?

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Wed Aug 24, 2005 11:00 am

Dominik Michniewski wrote: [snip]
2. you don't set all options of compiler (I heard about something like gcc -wall -strict -ansi file.c to compile - check options of your compiler).
Hey Dominik,

Its -Wall ;)
Also a few more things I'd like to add to your excellent suggestions:
+ if using gcc, use .c[c|p|xx|pp|PP|++|C] as the file extension
+ use g++ (they are almost the same...)
+ here's what I like as the command line options to gcc for C code:

gcc -std=c89 -Wall -ansi -pedantic

It sure is verbose!

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Re: judge says compile error, but I'm sure it works.

Post by sumankar » Wed Aug 24, 2005 11:04 am

jjtse wrote: [snip]

Code: Select all

#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

void readVal(int);        //reads converts, and stores numbers into p,u, or i.
void calculate();         //calculates the missing variable and outputs to screen

float p, u, i;
char used[2];             //2 characters to indicate the variables already obtained
int counter;              //keep track of how many variables obtained.
string line;              //holds the contents of the entire line

int main(){
  
  int cases, k, j;

  cin>>cases;
  getline(cin, line);

  for (k=0; k<cases; k++){
    cout<<"Problem #"<<k+1<<endl;
    getline(cin, line);

    j=0;
    counter = 0;
    p=0;
    i=0;
    u=0;


    //This takes into account if the first character in the line is a variable P,I, or U
    if (line[j]=='P' || line[j]=='I' || line[j]=='U'){
      if (line[j+1] == '='){
	readVal(j);
	j=j+3;
      }
    }

    while (counter< 2) {
      if (line[j] == ' '){
	j++;
	if (line[j]=='P' || line[j]=='I' || line[j]=='U'){
	  if (line[j+1] == '='){
	    readVal(j);
	    j=j+3;
	  }
	}
      }
      else
	j++;
    }//end while

    calculate();

  }//end for


  return 0;
}



void readVal(int k){

  char c[20];
  float num;
  int j;        //used to index char c[]

  if (counter == 0){
    used [0] = line[k];
  }
  else{
    used [1] = line[k];
  }

  counter++;
  k=k+2;
  j= 0;
  c[j] = line[k];

  while (c[j] != 'M' && c[j] != 'm' && c[j] != 'k' && c[j] != 'W' && c[j] != 'V' && c[j] != 'A'){
    j++;
    k++;
    c[j] = line[k];
  }//end while


  //takes into account the prefixes, if any
  num = atof(c);
  if (c[j] == 'M')
    num = num * 1000000;
  else if (c[j] == 'm')
    num = num / 1000;
  else if (c[j] == 'k')
    num = num * 1000;
  
  if (counter == 1){
    if (used[0] == 'P')
      p = num;
    else if(used[0] == 'I')
      i= num;
    else
      u=num;
  }
  else {
    if (used[1] == 'P')
      p = num;
    else if(used[1] == 'I')
      i= num;
    else
      u=num;
  }
}//end readVal;



void calculate(){

  if (used[0] !='P' && used[1] != 'P'){
    p=i*u;
    cout<<"P="<<fixed<<setprecision(2)<<p<<"W"<<endl<<endl;
  }
  else if (used[0] !='I' && used[1] != 'I'){
    i=p/u;
    cout<<"I="<<fixed<<setprecision(2)<<i<<"A"<<endl<<endl;
  }
  else{
    u=p/i;
    cout<<"U="<<fixed<<setprecision(2)<<u<<"V"<<endl<<endl;
  }
}

[snipped]
With my g++ 4.0.0 the above C++ did not compile. There were 2 errors:
+ `line' was not declared in this scope
+ no matching function for call to `getline(std::istream&, <type error>)'
I used g++ -Wall -ansi -pedantic.
HTH

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 » Wed Aug 24, 2005 1:21 pm

I can see one error - u forgot to #include <string> to use the string class.

Also, to use getline, I think u have to pass a char array instead of a string.

jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US
Contact:

Post by jjtse » Wed Aug 24, 2005 8:27 pm

guys check this out. This is very standard. I didn't use any strings this time. No getlines and stuff like that. Just the good'o cin>>. All I used are floats, ints, 2-D character arrays, cin and couts, a bunch of if statements and for loops. The judge still says it doesn't compile. I use
g++ -Wall -ansi -pedantic. No errors. My version compiler version is 3.2


Code: Select all


#include <iostream>
#include <iomanip>

using namespace std;

int main(){

  int segments, cases, numLines;
  float line[100][3];
  float temp;
  float axis[200];
  int i,j,k;
  int counter;
  float mid, trans;

  cin>>cases;

  for (i=0; i<cases; i++){
    cin>>numLines;
    for(j=0; j<numLines; j++){
      cin>>line[j][0];
      cin>>line[j][1];
      cin>>line[j][1];
      cin>>line[j][2];
      cin>>line[j][2];

      if (line[j][1] < line[j][0]){
	temp = line[j][1];
	line[j][1] = line[j][0];
	line[j][0] = temp;
      }

      axis[j*2] = line[j][0];
      axis[j*2+1] = line[j][1];
    }

    //bubble sort
    for (j=0; j<2*numLines; j++){
      for (k=j+1; k<2*numLines; k++){
	if (axis[j] > axis[k]){
	  temp = axis[j];
	  axis[j] = axis[k];
	  axis[k] = temp;
 	}
      }
    }

    segments = numLines*2+1;
    counter = 0;
    cout <<segments<<endl;
    for(j=0; j<segments; j++){
      if (j==0){
	cout<<"-inf"<<" "<<setprecision(3)<<fixed<<axis[counter]<<" "<<"1.000"<<endl;
	counter ++;
      }
      else if (j == segments -1){
	cout <<axis[counter-1]<<" +inf 1.000"<<endl;
      }
      else{
	cout<<axis[counter-1]<<" "<<axis[counter]<<" ";
	trans = 1.000;
	mid = axis[counter] - axis[counter-1];
	mid = mid/2;
	mid = mid + axis[counter -1];

	for (k=0; k<numLines; k++){
	  if (mid > line[k][0]  && mid < line[k][1]){
	    trans = trans * line[k][2];
	  }
	}
	counter ++;
	cout<<trans<<endl;
      }
    }
    cout<<endl;
  }//end for cases

  return 0;
}

User avatar
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey » Wed Aug 24, 2005 8:57 pm

This is what the judge returns:

Code: Select all

Here are the compiler error messages:

03865847_24.c: In function `int main()':
03865847_24.c:53: `fixed' undeclared (first use this function)
03865847_24.c:53: (Each undeclared identifier is reported only once
03865847_24.c:53: for each function it appears in.)
 
Don't you get email notifications?

jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US
Contact:

Post by jjtse » Wed Aug 24, 2005 9:23 pm

I wish I get e-mail notifications. I registered for a new account like 10 days ago. The account ID they assigned me doesn't even show up as mine! It's someone elses. So whenever I submit codes with that ID, I'm building up someone else's statistics. But I don't care. As long as I know my code works or not. The downside is that the e-mail notifications also go to that guy's e-mail.


I don't know why, but when I compiled it, fixed never came out as an error. In fact, fixed is used to control floating point numbers and tell it how many digits after the decimal point should be printed.

so if you have a number: 3.14159 and you have fixed<<setprecision(3),
then you're only printing 3.141.

Here's the page that I got all the information from:
http://www.fredosaurus.com/notes-cpp/io ... ators.html

I've been using fixed in conjunction with setprecision() for a long time already. Never did give me any problems.

Can you tell me what version compiler you're using and how you compiled the code and yielded this error?

Thanks for the help, I'll take out the "fixed" and resubmit the code

JT

jjtse
Learning poster
Posts: 80
Joined: Mon Aug 22, 2005 7:32 pm
Location: Nevada, US
Contact:

Post by jjtse » Wed Aug 24, 2005 10:49 pm

ahh...I got that corrected.

we still could use "fixed" but just in a different way.

the correct usage, is this:

cout<<setiosflags(ios::fixed | ios::showpoint);


that will give no compiling error.

Post Reply

Return to “Volume 5 (500-599)”