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

mak(cse_DU)
Learning poster
Posts: 72
Joined: Tue May 30, 2006 5:57 pm
Location: bangladesh

last space

Post by mak(cse_DU) »

your code is-

Code: Select all

#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 
long int index,i,a,num,x,sum,res[10000]={0},dum,tot,flag; 
char ish[10000],temp[10000]; 
void main() 
{ 
   //   freopen("h:\\392.txt","r",stdin); 
     //   freopen("h:\\out.txt","w",stdout); 
   while(gets(ish)) 
   { 
      x=strlen(ish); 
      i=-1;index=-1;dum=0; 
      while(i<x) 
      { 
         i++; 
         sum=-1; 
         while(ish[i]!=' ' && i<x) 
         { 
            while(ish[i]>=48 && ish[i]<=57) 
            { 
               temp[sum=sum+1]=ish[i]; 
               i++; 
            } 
            if(ish[i]!='-') 
            { 
               temp[sum=sum+1]=NULL; 
               sum=-1; 
               tot=atol(temp); 
               if(ish[a]!='-') 
                  res[index=index+1]=tot; 
               if(ish[a]=='-') 
               { 
                  res[index=index+1]=-tot; 
                  a=-1; 
               } 

            } 
            if(ish[i]=='-') 
            { 
               a=i; 
               i++; 
            } 
         } 
      } 
      flag=0; 
      for(i=0;i<=index;i++) 
      { 
         if(res[i]>0) 
         { 
            if(i>0 && flag==1) 
               printf("+ "); 
            if(res[i]!=1) 
            { 
               printf("%ld",res[i]); 
               flag=1; 
            } 
            if(index-i!=0) 
            { 
               printf("x"); 
               if(index-i>1) 
               { 
                  printf("^%ld",index-i); 
                  if(i<index) 
                     printf(" "); 
               } 
               if(index-i==1) 
               { 
                  if(i<index) 
                     printf(" "); 
               } 
               flag=1; 
            } 
            if(index-i==0 && res[i]==1) 
            { 
               printf("1"); 
               if(i<index) 
                  printf(" "); // ----confusion for extra space for end of line
               flag=1; 
            } 
         } 
         if(res[i]<0) 
         { 
            if(flag==1) 
               printf("- "); 
            if(flag==0) 
               printf("-"); 
            res[i]=-res[i]; 
            if(res[i]!=1) 
               printf("%ld",res[i]); 
            if(index-i!=0) 
            { 
               printf("x"); 
               if(index-i>1) 
               { 
                  printf("^%ld",index-i); 
                  if(i<index) 
                     printf(" "); //--confusion for extra space for end of line
               } 
               if(index-i==1) 
               { 
                  if(i<index) 
                     printf(" "); //--confusion for extra space for end of line
               } 
               flag=1; 
            } 
            if(index-i==0 && res[i]==1) 
            { 
               printf("1"); 
               if(i<index) 
                  printf(" "); // --confusion for extra space for end of line
            } 
            flag=1; 
         } 
         if(res[i]==0) 
         { 
            dum++; 

            if(dum==index+1) 
            { 
               printf("0"); 
               flag=1; 
               break; 
            } 
         } 
      } 
      printf("\n"); 
   } 
} 
please check this confiused line
when output is end ex...5x^2(no space acceptable)for this out put input is 0 0 0 0 0 0 5 0 0
ishtiaq ahmed
Learning poster
Posts: 53
Joined: Sat Jul 29, 2006 7:33 am
Location: (CSE,DU), Dhaka,Bangladesh

i am facing still PE

Post by ishtiaq ahmed »

Code: Select all

the code is removed after ac.
thanks to shamim and mak
can anybody help me?please try to help.
No venture no gain

with best regards
------------------------
ishtiaq ahmed
scott1991
New poster
Posts: 28
Joined: Fri Dec 07, 2007 11:41 pm

Post by scott1991 »

I got a wa...can anyone help? the code is a bit long winded but it was the best i could think of. Help with shortening it would be cool too. Thanks. Scot1991

Code: Select all

// Polynomial Showdown - 392.cpp : Defines the entry point for the console application.
#include <iostream>

int main()
{
	int d8,d7, d6, d5, d4, d3, d2, d1, c; 
//a:
while(scanf("%d%d%d%d%d%d%d%d%d",&d8,&d7,&d6,&d5,&d4,&d3,&d2,&d1,&c))
{

		if(d8!=0 && d8!= 1 && d8!= -1)
		{printf("%dx^8 ",d8);}
		if(d8 == 1)
		{printf("x^8 ");}
		if(d8 == -1)
		{printf("-x^8 ");}
		
		if(d7<0 && d7!= -1 && d8!=0)
		{printf("- %dx^7 ",d7*-1);}
		if(d7<0 && d7!= -1 && d8==0)
		{printf("%dx^7 ",d7);}
		if(d7 == -1 && d8 !=0)
		{printf("- x^7 ");}
		if(d7 == -1 && d8 ==0)
		{printf("-x^7 ");}
		if(d7>0 && d7!=1 && d8!=0)
		{printf("+ %dx^7 ",d7);}
		if(d7>0 && d7!=1 &&d8 ==0)
		{printf("%dx^7 ",d7);}
		if(d7 == 1 && d8!=0)
		{printf("+ x^7 ");}
		if(d7 == 1 && d8==0)
		{printf("x^7 ");}

		if(d6<0 && d6!= -1 && d7 !=0)
		{printf("- %dx^6 ",d6*-1);}
		if(d6<0 && d6!= -1 && d8==0 && d7 ==0)
		{printf("%dx^6 ",d6);}
		if(d6 == -1 && d7 !=0)
		{printf("- x^6 ");}
		if(d6 == -1 && d7 ==0&&d8==0)
		{printf("-x^6 ");}
		if(d6>0 && d6!=1&&d7!=0)
		{printf("+ %dx^6 ",d6);}
		if(d6>0 && d6!=1&&d7==0&&d8==0)
		{printf("%dx^6 ",66);}
		if(d6 == 1&&d7!=0)
		{printf("+ x^6 ");}
		if(d6 == 1&&d7==0&&d8==0)
		{printf("x^6 ");}
		
		if(d5<0 && d5!= -1 && d6!=0)
		{printf("- %dx^5 ",d5*-1);}
		if(d5<0 && d5!= -1 && d6==0&&d7==0&&d8==0)
		{printf("%dx^5 ",d5);}
		if(d5 == -1 && d6 != 0)
		{printf("- x^5 ");}
		if(d5 == -1 && d6 == 0&&d7==0&&d8==0)
		{printf("-x^5 ");}
		if(d5>0 && d5!=1&&d6!=0)
		{printf("+ %dx^5 ",d5);}
		if(d5>0 && d5!=1&&d6==0&&d7==0&&d8==0)
		{printf("%dx^5 ",d5);}
		if(d5 == 1&&d6!=0)
		{printf("+ x^5 ");}
		if(d5 == 1&&d6==0&&d7==0&&d8==0)
		{printf("x^5 ");}
		
		if(d4<0 && d4!= -1&&d5!=0)
		{printf("- %dx^4 ",d4*-1);}
		if(d4<0 && d4!= -1&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("%dx^4 ",d4);}
		if(d4 == -1&&d5!=0)
		{printf("- x^4 ");}
		if(d4 == -1&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("-x^4 ");}
		if(d4>0 && d4!=1&&d5!=0)
		{printf("+ %dx^4 ",d4);}
		if(d4>0 && d4!=1&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("%dx^4 ",d4);}
		if(d4 == 1&&d5!=0)
		{printf("+ x^4 ");}
		if(d4 == 1&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("x^4 ");}
		
		if(d3<0 && d3!= -1&&d4!=0)
		{printf("- %dx^3 ",d3*-1);}
		if(d3<0 && d3!= -1&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("%dx^3 ",d3);}
		if(d3 == -1&&d4!=0)
		{printf("- x^3 ");}
		if(d3 == -1&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("-x^3 ");}
		if(d3>0 && d3!=1&&d4!=0)
		{printf("+ %dx^3 ",d3);}
		if(d3>0 && d3==1&&d4!=0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("%dx^3 ",d3);}
		if(d3 == 1&&d4!=0)
		{printf("+ x^3 ");}
		if(d3 == 1&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("x^3 ");}
		
		if(d2<0 && d2!= -1&&d3!=0)
		{printf("- %dx^2 ",d2*-1);}
		if(d2<0 && d2!= -1&&d3==0&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("%dx^2 ",d2);}
		if(d2 == -1&&d3!=0)
		{printf("- x^2 ");}
		if(d2 == -1&&d3==0&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("-x^2 ");}
		if(d2>0 && d2!=1&&d3!=0)
		{printf("+ %dx^2 ",d2);}
		if(d2>0 && d2!=1&&d3==0&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("%dx^2 ",d2);}
		if(d2 == 1&&d3!=0)
		{printf("+ x^2 ");}
		if(d2 == 1&&d3==0&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("x^2 ");}
		
		if(d1<0 &&d1 != -1&&d2!=0)
		{printf("- %dx ",d1*-1);}
		if(d1<0 &&d1 != -1&&d2==0&&d3==0&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("%dx ",d1);}
		if(d1 == -1&&d2!=0)
		{printf("- x ");}
		if(d1 == -1&&d2==0&&d3==0&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("-x ");}
		if(d1>0 && d1!=1&&d2!=0)
		{printf("+ %dx ",d1);}
		if(d1>0 && d1!=1&&d2==0&&d3==0&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("%dx ",d1);}
		if(d1 == 1&&d2!=0)
		{printf("+ x ");}
		if(d1 == 1&&d2==0&&d3==0&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("x ");}
		
		if (c<0&&d1!=0)
		{printf("- %d",c*-1);}
		if (c<0&&d1==0&&d2==0&&d3==0&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("%d",c);}
		if (c>0&&d1!=0)
		{printf("+ %d",c);}
		if (c>0&&d1==0&&d2==0&&d3==0&&d4==0&&d5==0&&d6==0&&d7==0&&d8==0)
		{printf("%d",c);}
		printf("\n");
		system("PAUSE");
}
	return 0;
}
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Use

Code: Select all

while(scanf("%d%d%d%d%d%d%d%d%d",&d8,&d7,&d6,&d5,&d4,&d3,&d2,&d1,&c)==9)
remove

Code: Select all

system("PAUSE");
And check the cases.
Input:

Code: Select all

21 92 80 74 28 0 -71 -96 -21
44 -56 16 3 -57 -74 0 -10 50
Output:

Code: Select all

21x^8 + 92x^7 + 80x^6 + 74x^5 + 28x^4 - 71x^2 - 96x - 21
44x^8 - 56x^7 + 16x^6 + 3x^5 - 57x^4 - 74x^3 - 10x + 50
Ami ekhono shopno dekhi...
HomePage
scott1991
New poster
Posts: 28
Joined: Fri Dec 07, 2007 11:41 pm

Post by scott1991 »

I cant get the program to even process 9 ones. It just closes. Does anyone know why? code is the same as above with the system("PAUSE") removed and ==9 added just before the last bracket of the while(where jan said). Cheers. Scott
deadangelx
New poster
Posts: 32
Joined: Tue Feb 13, 2007 1:31 pm

Post by deadangelx »

some input and output may help

input

Code: Select all

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 1
0 0 0 0 0 0 0 1 -1
0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 1 2 -1
1 0 0 0 0 0 1 2 -1
-1 0 0 0 0 0 1 2 -1
2 0 0 0 0 0 1 2 -1
-2 0 0 0 0 0 1 2 -1
-2 0 0 0 0 0 2 2 -1
-2 0 0 0 0 0 2 2 -2
-2 0 0 0 0 0 2 2 2
-2 0 0 0 0 0 -2 2 2
0 0 0 0 0 0 -2 2 2
0 0 0 0 0 0 -1 2 2
0 0 0 0 0 0 -1 2 0
-2 0 0 0 0 0 -1 2 2
output

Code: Select all

1
-1
x + 1
x - 1
x^2 + x + 1
x^2 + 2x - 1
x^8 + x^2 + 2x - 1
-x^8 + x^2 + 2x - 1
2x^8 + x^2 + 2x - 1
-2x^8 + x^2 + 2x - 1
-2x^8 + 2x^2 + 2x - 1
-2x^8 + 2x^2 + 2x - 2
-2x^8 + 2x^2 + 2x + 2
-2x^8 - 2x^2 + 2x + 2
-2x^2 + 2x + 2
-x^2 + 2x + 2
-x^2 + 2x
-2x^8 - x^2 + 2x + 2
zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

Re: 392...Polynomial Showdown

Post by zyxw »

Hi midra...
I hope you would have solved it by now... Anyway i ran your code on some i/o.
Your code gave wrong answers :oops:

In the first term of output (whichever it is), there should be no spaces between '+/-' sign and the term.

These are the INPUT:

Code: Select all

 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 -1
0 0 0 0 0 0 0 -1 0
0 0 0 0 0 0 -1 0 0
0 0 0 0 0 -1 0 0 0
0 0 0 0 -1 0 0 0 0
0 0 0 -1 0 0 0 0 0
0 0 -1 0 0 0 0 0 0
0 -1 0 0 0 0 0 0 0
-1 0 0 0 0 0 0 0 0
999 999 999 999 999 999 999 999 999
-999 -999 -999 -999 -999 -999 -999 -999 -999
These are your OUTPUT:

Code: Select all

0
1
x
x^2
x^3
x^4
x^5
x^6
x^7
x^8
-1
-x
- x^2
- x^3
- x^4
- x^5
- x^6
- x^7
- x^8
999x^8 + 999x^7 + 999x^6 + 999x^5 + 999x^4 + 999x^3 + 999x^2 + 999x + 999
-999x^8 - 999x^7 - 999x^6 - 999x^5 - 999x^4 - 999x^3 - 999x^2 - 999x- 999

And these are OUTPUT from my AC code:

Code: Select all

0
1
x
x^2
x^3
x^4
x^5
x^6
x^7
x^8
-1
-x
-x^2
-x^3
-x^4
-x^5
-x^6
-x^7
-x^8
999x^8 + 999x^7 + 999x^6 + 999x^5 + 999x^4 + 999x^3 + 999x^2 + 999x + 999
-999x^8 - 999x^7 - 999x^6 - 999x^5 - 999x^4 - 999x^3 - 999x^2 - 999x - 999
Hope it helps.

P.S: Forgive me if i had said something wrong :wink:
I am not totally useless, because I can still be used as a bad example :P
zyxw
New poster
Posts: 24
Joined: Sat Mar 22, 2008 5:49 am
Location: Chennai
Contact:

Re: 392 Polynomial Showdown Plz Help

Post by zyxw »

Hi triker... I think this reply is too late :o

Anyway i ran your code on some i/o.
Your code gave wrong answers :oops:


These are the INPUT:

Code: Select all

 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 -1
0 0 0 0 0 0 0 -1 0
0 0 0 0 0 0 -1 0 0
0 0 0 0 0 -1 0 0 0
0 0 0 0 -1 0 0 0 0
0 0 0 -1 0 0 0 0 0
0 0 -1 0 0 0 0 0 0
0 -1 0 0 0 0 0 0 0
-1 0 0 0 0 0 0 0 0
999 999 999 999 999 999 999 999 999
-999 -999 -999 -999 -999 -999 -999 -999 -999
These are your OUTPUT:

Code: Select all

01xx^2x^3x^4x^5x^6x^7x^8-1-x-x^2-x^3-x^4-x^5-x^6-x^7-x^8999x^8 + 999x^7 + 999x^6 + 999x^5 + 999x^4 + 999x^3 + 999x^2 + 999x + 999-999x^8 - 999x^7 - 999x^6 - 999x^5 - 999x^4 - 999x^3 - 999x^2 - 999x - 999
I think you had forgotten '\n' :roll:


And these are OUTPUT from my AC code:

Code: Select all

0
1
x
x^2
x^3
x^4
x^5
x^6
x^7
x^8
-1
-x
-x^2
-x^3
-x^4
-x^5
-x^6
-x^7
-x^8
999x^8 + 999x^7 + 999x^6 + 999x^5 + 999x^4 + 999x^3 + 999x^2 + 999x + 999
-999x^8 - 999x^7 - 999x^6 - 999x^5 - 999x^4 - 999x^3 - 999x^2 - 999x - 999
Hope it helps :wink:
I am not totally useless, because I can still be used as a bad example :P
Rushow
New poster
Posts: 14
Joined: Sat Oct 14, 2006 4:09 pm
Location: Dhaka,Bangladesh

392 - Polynomial Showdown PE -- plz help

Post by Rushow »

I have tried many I/O,but for all I got the correct ans
but still PE . can anyone help me? Here's my code

Code: Select all

/* p392 */

/* Polynomial Showdown */

#include<stdio.h>

int main(void)
{
	int pol[12], i, j, flag, flag_1st, sum;

	//freopen("input.in", "r", stdin);

	
	while(scanf("%d%d%d%d%d%d%d%d%d", &pol[0], &pol[1], &pol[2], &pol[3], &pol[4], &pol[5], &pol[6], &pol[7], &pol[8]) == 9 )
	{
			
		flag = 0; flag_1st = 0; sum = 0;
		
		for( i = 0, j = 8; i < 9; i++, j--)
		{
			if(pol[i] < 0)
				sum = sum + (pol[i] * (-1));
			else
				sum = sum + pol[i];
			if(pol[i] != 0) 
			{
				flag_1st++;
				if( pol[i] < 0)
				{
					printf("-");
					if( flag == 1 )
						printf(" ");
	
					pol[i] = pol[i] * -1;
					flag_1st++; 
				}
				else if( pol[i] > 0 )
				{
					if(flag_1st != 1)
					{
						printf("+ ");
					}
				}
				
				
				flag_1st++;	
				if( pol[i] == 1)
				{
					if( i == 7 )
						printf("x ");
					else if( i == 8 )
						printf("1");
					else
						printf("x^%d ", j);
					
					flag = 1;
				}
				else if( pol[i] > 1 )
				{
					if( i == 7 )
						printf("%dx ", pol[i]);
					else if( i == 8 )
						printf("%d", pol[i]);
					else
						printf("%dx^%d ", pol[i], j);
	
					flag = 1;
				}
	
	
				
			}
			if(i == 8 && sum == 0)
				printf("0");
	
			
		}//end for
		
		printf("\n");
	}//end while
	
			
	return 0;
}//end main
kbr_iut
Experienced poster
Posts: 103
Joined: Tue Mar 25, 2008 11:00 pm
Location: IUT-OIC, DHAKA, BANGLADESH
Contact:

Re: 392 - Polynomial Showdown PE -- plz help

Post by kbr_iut »

warning::::dont open a new thread while there r lot of threads.

for this input

Code: Select all

0 0 0 0 0 0 1 0 0
ur program gives

Code: Select all

x^2_
here I replace space character with '_';
so ur program gives an extra space .
think about that.
hople it helps.
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................
Rushow
New poster
Posts: 14
Joined: Sat Oct 14, 2006 4:09 pm
Location: Dhaka,Bangladesh

Re: 392 - Polynomial Showdown PE -- plz help

Post by Rushow »

thnx I got it
Get AC :D :D
crystaltanvir
New poster
Posts: 6
Joined: Mon Oct 18, 2010 8:13 pm
Location: Bangladesh

392 - Polynomial Showdown (Presentation Error)

Post by crystaltanvir »

I'm getting presentation error for this problem... I can't find where's the problem... Somebody please help me...

Code: Select all

#include<cmath>
#include<iostream>

using namespace std;

int main()
{
    int p[9];
    while(cin >> p[8] >> p[7] >> p[6] >> p[5] >> p[4] >> p[3] >> p[2] >> p[1] >> p[0])
    {
        int c = 0;
        if(p[8] != 0)
        {
            if(p[8] == 1) cout << "x^8" ;
            else if(p[8] == -1) cout << "-x^8" ;
            else if(p[8] > 0) cout << p[8] << "x^8" ;
            else cout << " -" << fabs(p[8]) << "x^8" ;
            c++;
        }
        if(p[7] != 0)
        {
            if(c != 0)
            {
                if(p[7] == 1) cout << " + x^7" ;
                else if(p[7] == -1) cout << " - x^7" ;
                else if(p[7] > 0) cout << " + " << p[7] << "x^7" ;
                else cout << " - " << fabs(p[7]) << "x^7" ;
            }
            else
            {
                if(p[7] == 1) cout << "x^7" ;
                else if(p[7] == -1) cout << "-x^7" ;
                else if(p[7] > 0) cout << p[7] << "x^7" ;
                else cout << "-" << fabs(p[7]) << "x^7" ;
            }
            c++;
        }
        if(p[6] != 0)
        {
            if(c != 0)
            {
                if(p[6] == 1) cout << " + x^6" ;
                else if(p[6] == -1) cout << " - x^6" ;
                else if(p[6] > 0) cout << " + " << p[6] << "x^6" ;
                else cout << " - " << fabs(p[6]) << "x^6" ;
            }
            else
            {
                if(p[6] == 1) cout << "x^6" ;
                else if(p[6] == -1) cout << "-x^6" ;
                else if(p[6] > 0) cout << p[6] << "x^6" ;
                else cout << "-" << fabs(p[6]) << "x^6" ;
            }
            c++;
        }
        if(p[5] != 0)
        {
            if(c != 0)
            {
                if(p[5] == 1) cout << " + x^5" ;
                else if(p[5] == -1) cout << " - x^5" ;
                else if(p[5] > 0) cout << " + " << p[5] << "x^5" ;
                else cout << " - " << fabs(p[5]) << "x^5" ;
            }
            else
            {
                if(p[5] == 1) cout << "x^5" ;
                else if(p[5] == -1) cout << "-x^5" ;
                else if(p[5] > 0) cout << p[5] << "x^5" ;
                else cout << "-" << fabs(p[5]) << "x^5" ;
            }
            c++;
        }
        if(p[4] != 0)
        {
            if(c != 0)
            {
                if(p[4] == 1) cout << " + x^4" ;
                else if(p[4] == -1) cout << " - x^4" ;
                else if(p[4] > 0) cout << " + " << p[4] << "x^4" ;
                else cout << " - " << fabs(p[4]) << "x^4" ;
            }
            else
            {
                if(p[4] == 1) cout << "x^4" ;
                else if(p[4] == -1) cout << "-x^4" ;
                else if(p[4] > 0) cout << p[4] << "x^4" ;
                else cout << "-" << fabs(p[4]) << "x^4" ;
            }
            c++;
        }
        if(p[3] != 0)
        {
            if(c != 0)
            {
                if(p[3] == 1) cout << " + x^3" ;
                else if(p[3] == -1) cout << " - x^3" ;
                else if(p[3] > 0) cout << " + " << p[3] << "x^3" ;
                else cout << " - " << fabs(p[3]) << "x^3" ;
            }
            else
            {
                if(p[3] == 1) cout << "x^3" ;
                else if(p[3] == -1) cout << "-x^3" ;
                else if(p[3] > 0) cout << p[3] << "x^3" ;
                else cout << "-" << fabs(p[3]) << "x^3" ;
            }
            c++;
        }
        if(p[2] != 0)
        {
            if(c != 0)
            {
                if(p[2] == 1) cout << " + x^2" ;
                else if(p[2] == -1) cout << " - x^2" ;
                else if(p[2] > 0) cout << " + " << p[2] << "x^2" ;
                else cout << " - " << fabs(p[2]) << "x^2" ;
            }
            else
            {
                if(p[2] == 1) cout << "x^2" ;
                else if(p[2] == -1) cout << "-x^2" ;
                else if(p[2] > 0) cout << p[2] << "x^2" ;
                else cout << "-" << fabs(p[2]) << "x^2" ;
            }
            c++;
        }
        if(p[1] != 0)
        {
            if(c != 0)
            {
                if(p[1] == 1) cout << " + x" ;
                else if(p[1] == -1) cout << " - x" ;
                else if(p[1] > 0) cout << " + " << p[1] << "x" ;
                else cout << " - " << fabs(p[1]) << "x" ;
            }
            else
            {
                if(p[1] == 1) cout << "x" ;
                else if(p[1] == -1) cout << "-x" ;
                else if(p[1] > 0) cout << p[1] << "x" ;
                else cout << "-" << fabs(p[1]) << "x" ;
            }
            c++;
        }
        if(p[0] != 0)
        {
            if(c != 0)
            {
                if(p[0] == 1) cout << " + 1" ;
                else if(p[0] == -1) cout << " - 1" ;
                else if(p[0] > 0) cout << " + " << p[0] ;
                else cout << " - " << fabs(p[0]) ;
            }
            else
            {
                if(p[0] == 1) cout << "1" ;
                else if(p[0] == -1) cout << "-1" ;
                else if(p[0] > 0) cout << p[0] ;
                else cout << "-" << fabs(p[0]) ;
            }
            c++;
        }
        if(c == 0) cout << 0;
        cout << endl;
    }
}

TANVIR
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Re: 392 - Polynomial Showdown (Presentation Error)

Post by sohel »

Code: Select all

if(p[8] != 0)
{
   if(p[8] == 1) cout << "x^8" ;
   else if(p[8] == -1) cout << "-x^8" ;
   else if(p[8] > 0) cout << p[8] << "x^8" ;
   else cout << " -" << fabs(p[8]) << "x^8" ;
   c++;
}
last else - cout << " -"
shouldn't it be cout << "-"

And don't create a new thread for a problem whose discussions already exist. Make your post in an existing thread of 392.
crystaltanvir
New poster
Posts: 6
Joined: Mon Oct 18, 2010 8:13 pm
Location: Bangladesh

Re: 392 - Polynomial Showdown (Presentation Error)

Post by crystaltanvir »

Thanx bro... It was a silly mistake... Its accepted now... Very vey thanx...
TANVIR
Nilpoddo
New poster
Posts: 1
Joined: Fri Jan 07, 2011 1:10 am

392 WA

Post by Nilpoddo »

why I'm getting wrong answer???????

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




int main()
{ int t[10],a,b,m,n,c,d,i,j,k;


while(1)

{
m=0;

i=1;
while(i<=9)
{

if(scanf("%d",&t)==-1)

{m=1;
break;
}
i++;

}
if(m==1)
break;

c=0;
for(i=1;i<=9;i++)

{if(t!=0)
{c++;
k=i;
break;

}

}
if(c==0)
printf("0");

else
{ if(t[k]<0)
printf("-");

if(k<8)

{
if(t[k]!=1&&t[k]!=-1)
printf("%dx^%d",abs(t[k]),9-k);

else
printf("x^%d",9-k);
}

else if(k==8)
{
if(t[k]!=1&&t[k]!=-1)
printf("%dx",abs(t[k]));

else
printf("x");


}
else
printf("%d",abs(t[9]));


}



for(i=k+1;i<=9;i++)
{
if(t!=0)
{ if(t>0)
printf(" + ");
else
printf(" - ");
if(i<8)
{if(t!=1&&t[k]!=-1)
printf("%dx^%d",abs(t),9-i);
else
printf("x^%d",9-i);

}

else if(i==8)
{if(t[8]==1||t[8]==-1)
printf("x");
else
printf("%dx",abs(t[8]));
}
else
printf("%d",abs(t[9]));
}
}
printf("\n");

}

return 0;
}
Post Reply

Return to “Volume 3 (300-399)”