392 - Polynomial Showdown

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

Moderator: Board moderators

claire yan
New poster
Posts: 6
Joined: Thu Oct 02, 2003 7:02 am

392 polynomial showdown

Post by claire yan » Thu Oct 02, 2003 7:11 am

i don't kown why i got WA and WA. i have tried and tried.All the topics about the articles i have read . could sb help me ?? please give me some input.thanks a lot
///////////////////////////////////////////////[cpp]
#include <iostream>
//#include <fstream>
using namespace std;

void findnotzero(int*,int[]);
void show(int,int[]);

int main()
{
int num[9];
// ifstream infile;
// ofstream outfile;
int i ;
int notzero = 8;


// infile.open("c://data.txt");

while(!cin.eof())
{
for(i = 8; i >= 0;i--)
cin>>num;
cin.ignore(1000, '\n');
findnotzero(&notzero,num);
show(notzero,num);
cout<<" "<<endl;
}
// infile.close();
return 0;
}
////////////////////////////////////
void findnotzero(int* notzero,int num[])
{
int i = 8;
int zero = 1;

while (zero)
{
if(num != 0)
{
zero = 0;
*notzero = i;
}
else
i--;
}
if (zero==1)
*notzero = -1;

}
////////////////////////////////////////
void show(int notzero,int num[])
{
int i;

if (notzero == -1)
{
cout<<"0"<<endl;
}
else if(notzero == 0)
{
if(num[0] > 0 )
{
if (num[0] != 1)
cout<<num[0];
else
cout<<"1";
}
else if(num[0] < 0)
{
if (num[0] != -1)
cout<<num[0];
else
cout<<"-1";
}
//cout<<" "<<endl;
}
else if (notzero == 1)
{
if (num[1] > 0)
{
if(num[1] != 1)
cout<<num[1]<<"x";
else
cout<<"x";
}
if (num[1] < 0)
{
if(num[1] != -1)
cout<<num[1]<<"x";
else
cout<<"-x";
}
if (num[0] > 0)
{
if(num[0] != 1)
cout<<" + "<<num[1];
else
cout<<" + 1";
}
if (num[0] < 0)
{
if(num[0] != -1)
cout<<" - "<<0 - num[0];
else
cout<<" - 1";
}
// cout<<" "<<endl;
}
else
{
if (num[notzero] == 1)
cout<<"x^"<<notzero;
else if (num[notzero] == -1)
cout<<"-x^"<<notzero;
else
cout<<num[notzero]<<"x^"<<notzero;
for(i = notzero-1 ; i >= 2 ; i--)
{
if(num > 0 )
{
if (num != 1)
cout<<" + "<<num<<"x^"<<i;
else
cout<<" + x^"<<i;
}
else if(num < 0)
{
if (num != -1)
cout<<" - "<<0-num<<"x^"<<i;
else
cout<<" - x^"<<i;
}
}
if(num[1] > 0 )
{
if (num[1] != 1)
cout<<" + "<<num[1]<<"x";
else
cout<<" + x";
}
else if(num[1] < 0)
{
if (num[1] != -1)
cout<<" - "<<num[1]<<"x";
else
cout<<" - x";
}
if(num[0] > 0 )
{
if (num[0] != 1)
cout<<" + "<<num[0];
else
cout<<" + 1";
}
else if(num[0] < 0)
{
if (num[0] != -1)
cout<<" - "<<num[0];
else
cout<<" - 1";
}
//cout<<" "<<endl;
}
}[/cpp]

claire yan
New poster
Posts: 6
Joined: Thu Oct 02, 2003 7:02 am

Post by claire yan » Thu Oct 02, 2003 7:33 am

i changed my code as follows:
and i find that the last line will be show twice
i don't where the problem is:
[cpp]
#include <iostream>
//#include <fstream>
using namespace std;

void findnotzero(int*,int[]);
void show(int,int[]);

int main()
{
int num[9];
// ifstream infile;
// ofstream outfile;
int i ;
int notzero = 8;


// infile.open("c://data.txt");

while(!cin.eof())
{
for(i = 8; i >= 0;i--)
cin>>num;
cin.ignore(1000, '\n');
findnotzero(&notzero,num);
show(notzero,num);
cout<<" "<<endl;
}
// infile.close();
return 0;
}
////////////////////////////////////
void findnotzero(int* notzero,int num[])
{
int i = 8;
int zero = 1;

while (zero)
{
if(num != 0)
{
zero = 0;
*notzero = i;
}
else
i--;
}
if (zero==1)
*notzero = -1;

}
////////////////////////////////////////
void show(int notzero,int num[])
{
int i;

if (notzero == -1)
{
cout<<"0"<<endl;
}
else if(notzero == 0)
{
if(num[0] > 0 )
{
if (num[0] != 1)
cout<<num[0];
else
cout<<"1";
}
else if(num[0] < 0)
{
if (num[0] != -1)
cout<<num[0];
else
cout<<"-1";
}
//cout<<" "<<endl;
}
else if (notzero == 1)
{
if (num[1] > 0)
{
if(num[1] != 1)
cout<<num[1]<<"x";
else
cout<<"x";
}
if (num[1] < 0)
{
if(num[1] != -1)
cout<<num[1]<<"x";
else
cout<<"-x";
}
if (num[0] > 0)
{
if(num[0] != 1)
cout<<" + "<<num[1];
else
cout<<" + 1";
}
if (num[0] < 0)
{
if(num[0] != -1)
cout<<" - "<<0 - num[0];
else
cout<<" - 1";
}
// cout<<" "<<endl;
}
else
{
if (num[notzero] == 1)
cout<<"x^"<<notzero;
else if (num[notzero] == -1)
cout<<"-x^"<<notzero;
else
cout<<num[notzero]<<"x^"<<notzero;
for(i = notzero-1 ; i >= 2 ; i--)
{
if(num > 0 )
{
if (num != 1)
cout<<" + "<<num<<"x^"<<i;
else
cout<<" + x^"<<i;
}
else if(num < 0)
{
if (num != -1)
cout<<" - "<<0-num<<"x^"<<i;
else
cout<<" - x^"<<i;
}
}
if(num[1] > 0 )
{
if (num[1] != 1)
cout<<" + "<<num[1]<<"x";
else
cout<<" + x";
}
else if(num[1] < 0)
{
if (num[1] != -1)
cout<<" - "<<0-num[1]<<"x";
else
cout<<" - x";
}
if(num[0] > 0 )
{
if (num[0] != 1)
cout<<" + "<<num[0];
else
cout<<" + 1";
}
else if(num[0] < 0)
{
if (num[0] != -1)
cout<<" - "<<0-num[0];
else
cout<<" - 1";
}
//cout<<" "<<endl;
}
}[/cpp]

claire yan
New poster
Posts: 6
Joined: Thu Oct 02, 2003 7:02 am

the same question~~

Post by claire yan » Thu Oct 02, 2003 12:15 pm

i have tried and tried,but i still got WA.
do you think that the input will be less than 9 in line?
plzzzz help me ,thanks a lot.

claire yan
New poster
Posts: 6
Joined: Thu Oct 02, 2003 7:02 am

Post by claire yan » Fri Oct 03, 2003 6:57 am

i got AC......

Amir Faisal
New poster
Posts: 4
Joined: Wed Aug 13, 2003 2:47 pm
Location: Bangladesh

392--WA

Post by Amir Faisal » Sat Nov 15, 2003 9:57 am

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

void main()
{
int i,j,k,l,x[9],exp[9],flag;

while((scanf("%d", &x[0]))==1)
{
for(i=1;i<9;i++)
scanf("%d", &x);

flag=0;

for(i=0,l=8;i<8;i++,l--)
{
if(x<0)
{
k=i+1;
j=l-1;

if(fabs(x)==1&&l!=1)
printf("-x^%d",l);
else if(fabs(x)!=1&&l!=1)
printf("%dx^%d", x,l);
else
printf("-x");

flag=1;

break;
}
else if(x>0)
{
k=i+1;
j=l-1;

if(x==1&&l!=1)
printf("x^%d", l);
else if(x!=1&&l!=1)
printf("%dx^%d",x,l);
else
printf("x");
flag=1;

break;
}

}

for(i=k,l=j;i<8;i++,l--)
{
if(x<0)
{
if(fabs(x[i])!=1&&l!=1)
printf(" - %dx^%d", (int)fabs(x[i]),l);
else if(l!=1)
printf(" - x^%d", l);
else if(fabs(x[i])!=1&&l==1)
printf(" - %dx", (int)fabs(x[i]));
else
printf(" - x");

flag=1;
}
else if(x[i])
{
if(x[i]!=1&&l!=1)
printf(" + %dx^%d", x[i],l);
else if(l!=1)
printf(" + x^%d", l);
else if(x[i]!=1&&l==1)
printf(" + %dx", x[i]);
else
printf(" + x");

flag=1;
}
}

if(x[8]>0&&flag)
printf(" + %d", x[8]);
else if(x[8]<0&&flag)
printf(" - %d", (int)fabs(x[8]));
else if((x[8]>=0||x[8]<0)&&!flag)
printf("%d", x[8]);


printf("\n");
}
}
I love my mother.

gcp
New poster
Posts: 6
Joined: Sat Nov 22, 2003 5:38 am
Location: Brazil
Contact:

392

Post by gcp » Sun Nov 23, 2003 4:57 am

I've got Runtime Error (SIGSEGV)..
Why?

[cpp]

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

void main()
{ int vetor[9], primeiro;

while (cin >>vetor[1])
{ for (int i=2; i<=9; i++) cin >> vetor;
primeiro=0;
for (int i=1; i<=9; i++)
{ if (vetor!=0)
{ if (vetor<0)
cout << "-";
else
if (primeiro==1)
cout << "+";
if (primeiro==1)
cout << " ";
if (abs(vetor)!=1 || i==9)
cout << abs(vetor);
if (i<9)
{ cout << "x";
if (i!=8)
cout << "^" << 9-i;
cout << " ";
}
primeiro=1;
}
}
if (primeiro==0) cout << vetor[9];
cout << endl;
}
}

[/cpp]

Thanks
VIVA LEOPARDO SC!!

Subeen
Experienced poster
Posts: 127
Joined: Tue Nov 06, 2001 2:00 am
Location: Bangladesh
Contact:

Post by Subeen » Sun Nov 23, 2003 8:36 am

increase the size of your vector

u declared vector[9]
and used loop for(i=2; i<=9; i++).
that's the problem.

pavelph
Learning poster
Posts: 57
Joined: Wed Dec 10, 2003 7:32 pm
Location: Russia, Saint-Petersburg

Where BUG???

Post by pavelph » Thu Dec 18, 2003 5:08 pm

I even read all posts about 392 but it`s all the same can`t understand whre bug in my code. Here some inputs and my outputs for this problem:
INPUT

Code: Select all

0 0 0 1 22 -333 0 1 -1 
-1 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 -7 30 66 
0 0 0 0 0 0 1 -3 0 
0 0 0 0 0 -1 1 3 -1 
-5 0 0 0 -243 0 0 0 -9 
-0 -1 -1 -1 -1 -1 -1 -1 -1 
-1999999999 -978456 0 0 0 0 56 -89 8
0 0 0 0 0 0 0 0 -1 
0 0 0 0 0 0 0 0  1 
0 0 0 0 0 0 0 1 0 
0 0 0 0 0 0 0 -1 0
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 -1 1
0 0 0 0 0 0 0 -1 -1
0 0 0 0 0 0 0 1 -1
0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 
0 0 -1 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 2
0 0 0 0 0 0 0 0 -10
0 0 0 0 0 0 0 10 0
0 0 0 0 0 0 0 -2 0
0 0 0 0 0 0 0 10 -2
OUTPUT

Code: Select all

x^5 + 22x^4 - 333x^3 + x - 1
-x^8
0
-7x^2 + 30x + 66
x^2 - 3x
-x^3 + x^2 + 3x - 1
-5x^8 - 243x^4 - 9
-x^7 - x^6 - x^5 - x^4 - x^3 - x^2 - x - 1
-1999999999x^8 - 978456x^7 + 56x^2 - 89x + 8
-1
1
x
-x
x + 1
-x + 1
-x - 1
x - 1
1
x^8
x^7
-x^6
2x^8
2
-10
10x
-2x
10x - 2
Here my program on Pascal:
[pascal]program acm392;
var i: longint;
k: array [0..8] of longint;

begin
while not eof do begin
readln(k[8], k[7], k[6], k[5], k[4], k[3], k[2], k[1], k[0]);
i:=8;
while k=0 do dec(i);
if i=-1 then begin writeln('0'); continue; end;
if (k=1) and (i>1) then write('x^', i);
if (k=1) and (i=1) then write('x');
if (k=1) and (i=0) then write('1');
if (k=-1) and (i>1) then write('-x^', i);
if (k=-1) and (i=1) then write('-x');
if (k=-1) and (i=0) then write('-1');
if (abs(k)<>1) and (i>1) then write(k, 'x^', i);
if (abs(k)<>1) and (i=1) then write(k[i], 'x');
if (abs(k[i])<>1) and (i=0) then write(k[i]);
dec(i);
while i>=0 do begin
if k[i]=0 then begin dec(i); continue; end;
if k[i]>0 then write(' + ');
if k[i]<0 then write(' - ');
if (i>1) and (abs(k[i])<>1) then write(abs(k[i]), 'x^', i);
if (i=1) and (abs(k[i])<>1) then write(abs(k[i]), 'x');
if (i=0) and (abs(k[i])<>1) then write(abs(k[i]));
if (i>1) and (abs(k[i])=1) then write('x^', i);
if (i=1) and (abs(k[i])=1) then write('x');
if (i=0) and (abs(k[i])=1) then write('1');
dec(i)
end;
writeln;
end;
end.
[/pascal]
Help me please!!!

midra
Experienced poster
Posts: 119
Joined: Fri Feb 13, 2004 7:20 am
Contact:

392...Polynomial Showdown

Post by midra » Fri Jun 04, 2004 5:52 am

I have test it again and again and I can't figure out what is wrong with my code...
Does someone know a possible input/output where my code gives a wrong answer...

[c]
#include <stdio.h>
#include <math.h>
int main()
{
int c[13]; /*Coeficients*/
int i; /*counter*/
int n; /*Numero para saber si ya hizo el 1er coef.*/

empieza:
while(scanf("%d%d%d%d%d%d%d%d%d",&c[9],&c[8],&c[7],&c[6],&c[5],&c[4],&c[3],&c[2],&c[1])==9)
{
n=0;
if (c[1]==0 && c[2]==1 && c[3]==0 && c[4]==0 && c[5]==0 && c[6]==0 && c[7]==0 && c[8]==0 && c[9]==0){
printf("x\n");
goto empieza;
}
else if (c[1]==0 && c[2]==-1 && c[3]==0 && c[4]==0 && c[5]==0 && c[6]==0 && c[7]==0 && c[8]==0 && c[9]==0){
printf("-x\n");
goto empieza;
}
for (i=9; i>=1; i--)
{
if (i==1 && n!=0)
{
if (c>0)
printf("+ %d",c);
else if (c<0)
printf("- %d",abs(c));
printf("\n");
goto empieza;
}
else if (i==1 && n==0)
{
printf("%d",c);
printf("\n");
goto empieza;
}

else if (c==0)
continue;
else if (i==2)
{
if (c>1)
printf("+ %dx ",c);
else if (c<-1)
printf("- %dx",abs(c));
else if (c[i]==-1)
printf("- x ");
else if (c[i]==1)
printf("+ x ");
}
else if (n==0 && abs(c[i])!=1)
{
printf("%dx^%d ",c[i],i-1);
n++;
}
else if (c[i]==-1 && n==0)
{
printf("-x^%d ",i-1);
n++;
}
else if (c[i]==1 && n==0)
{
printf("x^%d ",i-1);
n++;
}
else if (c[i]>1)
printf("+ %dx^%d ",c[i],i-1);
else if (c[i]<-1)
printf("- %dx^%d ",abs(c[i]),i-1);
else if (c[i]==1)
printf("+ x^%d ",i-1);
else if (c[i]==-1)
printf("- x^%d ",i-1);
}
}
return 0;
}
[/c]

User avatar
cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike » Mon Jun 07, 2004 1:39 am

try these two~~

Code: Select all

0 -12 -5 0 0 0 0 0 0

0 0 0 0 0 0 -1 -1 -1
good luck!

midra
Experienced poster
Posts: 119
Joined: Fri Feb 13, 2004 7:20 am
Contact:

Post by midra » Mon Jun 07, 2004 4:48 am

it gives me the correct answer....
thanks anyway

User avatar
cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike » Mon Jun 07, 2004 11:16 am

midra wrote:it gives me the correct answer....
thanks anyway
oh reli?
i use g++ to compile ur code and got some out of bound values???

midra
Experienced poster
Posts: 119
Joined: Fri Feb 13, 2004 7:20 am
Contact:

Post by midra » Tue Jun 08, 2004 5:03 am

cytmike:
I have win98s, and I compile in Dev-cpp...
for the input you give me it answer me:

Input:
0 -12 -5 0 0 0 0 0 0

0 0 0 0 0 0 -1 -1 -1
My Output:
-12x^7 - 5x^6

-x^2 -x - 1
Maybe there is some incompatibility between the compilers of Linux and windows or something like that....do you know what's the possible reason????
thanks![/c]

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Tue Jun 08, 2004 6:54 am

0 0 0 0 0 0 0 1 1

Kamanashish
New poster
Posts: 10
Joined: Wed Dec 17, 2003 3:12 pm
Location: Dhaka
Contact:

392-Anybody help me?

Post by Kamanashish » Sat Jun 12, 2004 2:12 pm

I have got WA.


#include<stdio.h>
void main ()
{
int a[9],i,first,position;
while(scanf("%d",&a[0])!=EOF)
{
for(i=1;i<9;++i)
scanf("%d",&a);

first=0;
position=8;
for(i=0;i<9;++i)
{
if(a==0){position--;continue;}
if(a!=0)
{
if(first==0)
{
if(position==0){printf("%d",a); continue;}
if(a!=1 && a!=-1)printf("%dx^%d",a,position);
if(a==1)printf("x^%d",position);
if(a==-1)printf("-x^%d",position);
--position;
++first;
continue;
}

if(a>0)
{
printf(" + ");
}
if(a[i]<0)
{
printf(" - ");
}
if(a[i]==1 || a[i]==-1)
{
if(a[i]<1)a[i]=a[i]*(-1);
if(position==0)printf("%d",a[i]);
else printf("x");
}
if(a[i]!=1 && a[i]!=-1)
{
if(a[i]<1)a[i]=a[i]*(-1);
if(position==0)printf("%d",a[i]);
else printf("%dx",a[i]);
}
if(position>1)
{
printf("^%d",position);
}
position--;
}


}
printf("\n");

}
}
Last edited by Kamanashish on Sun Jun 13, 2004 10:43 am, edited 1 time in total.
Work hard.

Post Reply

Return to “Volume 3 (300-399)”