10378 - Complex Numbers

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

Moderator: Board moderators

Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Post by Antonio Ocampo »

Please help me!!!!!!!!! This problem is driving me mad :(

Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Post by Antonio Ocampo »

At last, I got AC. :P

Just change this part of my code

Code: Select all


if(x.a> y.a )
...
by this

Code: Select all

if(1e-6 < x.a-y.a )
...
Hope it helps :wink:

Sederik
New poster
Posts: 3
Joined: Sat Dec 10, 2005 1:56 pm

Post by Sederik »

Help desparately needed. Everything works fine and output fits with the example, "-0.000" will not appear, but I'm still struggling with WA, so the problem is in something else. Here's the code:

Code: Select all

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

struct result
{
	double x, y;
};

int compare(const void *arg1, const void *arg2)
{
	result el1, el2;
	el1 = *(result*)arg1;
	el2 = *(result*)arg2;

/*	if (el1.x==el2.x)
		return (el1.y<el2.y);

	return (el1.x<el2.x);*/

	if (fabs(el1.x - el2.x)>1e-6)
	{
		if((1e-6) > (el1.x - el2.x)) return 1;
		if((-1e-6) < (el1.x - el2.x)) return -1;
	}
	if (fabs(el1.y - el2.y)<1e-6)
		return 0;
	if((1e-6) > (el1.y - el2.y)) return 1;
	if((-1e-6) < (el1.y - el2.y)) return -1;
	return 0;	
}

double fix(double v)
{
if (v>0) return v;
if (v>-0.0005) return 0.0;
return v;
} 

int main()
{

	int x, y, n, i = 0, k, k2;
	double r, f, r2, f2;
	result res[100];
	char string[1024], tmp[5];
	//Complex number: z=x+yi

	while(gets(string))
	{
		i++;
		k = 1;
		k2 = 0;
	
		tmp[0] = string[0];
		
		while ((string[k]!='+')&&(string[k]!='-'))
		{
			tmp[k] = string[k];
			k++;
		}
		tmp[k] = '\0';
		x = atoi(tmp);

		while (string[k]!='i')
		{
			tmp[k2] = string[k];
			k2++;
			k++;
		}
		tmp[k2] = '\0';
		y = atoi(tmp);

		k=k+2;
		k2 = 0;

		while (string[k]!='\0')
		{
			tmp[k2++] = string[k];
			k++;
		}
		
		tmp[k2] = '\0';
		n = atoi(tmp);

		//Trigonometric form: z=re^if
	
		r = sqrt(x*x+y*y);
		f = asin(y/r);

		printf("Case %d:\n", i);
		for (k = 0; k<n; k++)
		{
			r2 = pow(r, 1.0/n));
			f2 = (f+2*k*3.1415926535897932384626433832795)/n;
			res[k].x = cos(f2)*r2;
			res[k].y = sin(f2)*r2;
		}

		qsort(res, n, sizeof(res[0]), compare);
		
		for (k = 0; k<n; k++)
			printf("%.3lf%+.3lfi\n", res[k].x, res[k].y);
		printf("\n");
	}
	return 0;
}


kn
New poster
Posts: 28
Joined: Fri Apr 13, 2007 8:53 am

Post by kn »

I use STL to do... why wrong?

Code: Select all

Removed...

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Your code prints -0.000 in some cases. Just check your output carefully for the following input set.

Input:

Code: Select all

88+1i 50
2+56i 79
I used almost similar method, but my printing method was different. The idea is to first print the values in a string and then scan from the string.

Code: Select all

void print(double a,double b)
{
	char temp[50];

	sprintf(temp,"%.3lf %.3lf",a,b);
	sscanf(temp,"%lf %lf",&a,&b);
	printf("%.3lf%+.3lfi\n",a+1e-6,b+1e-6);
}
Hope these help.
Ami ekhono shopno dekhi...
HomePage

kn
New poster
Posts: 28
Joined: Fri Apr 13, 2007 8:53 am

Post by kn »

Jan wrote:Your code prints -0.000 in some cases. Just check your output carefully for the following input set.

Input:

Code: Select all

88+1i 50
2+56i 79
I used almost similar method, but my printing method was different. The idea is to first print the values in a string and then scan from the string.

Code: Select all

void print(double a,double b)
{
	char temp[50];

	sprintf(temp,"%.3lf %.3lf",a,b);
	sscanf(temp,"%lf %lf",&a,&b);
	printf("%.3lf%+.3lfi\n",a+1e-6,b+1e-6);
}
Hope these help.
I helps...!THX:D:D:D:D

But I have a quesiton... how should we choose our EPSILON?
And, must this method work in problem dealing with floating point numbers...?

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

kn wrote:But I have a quesiton...
1. how should we choose our EPSILON?
2. And, must this method work in problem dealing with floating point numbers...?
Answers:
1. No absolute reasoning actually. I normally try 1e-6 to 1e-13. 1e-6 to 1e-8 works well.
2. Yes, this method works for other problems with floating point numbers. I used this idea in many problems and worked well.

P.S. Dont forget to remove your code.
Ami ekhono shopno dekhi...
HomePage

Carunty
New poster
Posts: 18
Joined: Sat Jul 08, 2006 2:40 am

Post by Carunty »

I also try many time in this question, but all WA.
I know that it is a precision problem after read these post.

Can anyone help me improve the precision?

Code: Select all

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
using namespace std;

struct Complex{
  double x,y;
  bool operator<(const Complex &c)const{
    if(abs(x-c.x)>0.000001) return(x>c.x);
    else                    return(y>c.y);
  }
};

int main(){
  int a,b,n,t=0; double r,theta,temp,pi=acos(0)*2; char c,d;
  while(cin>>a>>c>>b>>d>>n){
    ++t; Complex complex[n];
    if(c=='-') b=-b;
    r=sqrt(a*a+b*b);
    theta=atan(1.0*b/a);
    r=pow(r,1.0/n);
    for(int i=0; i<n; ++i){
      temp=(theta+2*i*pi)/n;
      complex[i].x=r*cos(temp);
      complex[i].y=r*sin(temp);
    }
    sort(complex,complex+sizeof(complex)/sizeof(Complex));
    printf("Case %d:\n",t);
    for(int i=0; i<n; ++i){
      int h=int(complex[i].x*10000),A,B;
      if(h<=-10) {printf("-"); h=-h;}
      if(h%10>4) h=h/10+1; else h/=10;
      A=h/1000; B=h%1000;
      printf("%d.",A);
      if(B<10) printf("0");
      if(B<100) printf("0");
      printf("%d",B);
      h=int(complex[i].y*10000);
      if(h<=-10) {printf("-"); h=-h;}
      else       printf("+");
      if(h%10>4) h=h/10+1; else h/=10;
      A=h/1000; B=h%1000;
      printf("%d.",A);
      if(B<10) printf("0");
      if(B<100) printf("0");
      printf("%di\n",B);
    }
    puts("");
  }
}
[Edited by Jan: Next time use code tags]

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Try the cases.

Input:

Code: Select all

-59+76i 15
69-26i 5
Output:

Code: Select all

Case 1:
1.341+0.201i
1.307-0.362i
1.143+0.729i
1.047-0.862i
0.748+1.131i
0.605-1.213i
0.223+1.337i
0.060-1.355i
-0.340+1.313i
-0.496-1.262i
-0.844+1.061i
-0.967-0.951i
-1.203+0.626i
-1.270-0.475i
-1.353+0.082i

Case 2:
2.357-0.170i
0.890+2.189i
0.567-2.294i
-1.807+1.523i
-2.007-1.248i
Hope these help.
Ami ekhono shopno dekhi...
HomePage

Saswat2603
New poster
Posts: 15
Joined: Tue Feb 12, 2008 10:42 am
Location: Bhubaneswar, Orissa, India

uvatoolkit.com [wrong] output

Post by Saswat2603 »

Guys just check this--
Input:

Code: Select all

88+1i 100
Output

Code: Select all

Case 1:
1.046+0.000i
1.044-0.066i
1.044+0.066i
1.038-0.131i
1.038+0.131i
1.027-0.196i
1.027+0.196i
1.013-0.260i
1.013+0.260i
0.995-0.323i
0.995+0.323i
0.972-0.385i
0.972+0.385i
0.946-0.445i
0.946+0.445i
0.916-0.504i
0.916+0.504i
0.883-0.560i
0.883+0.560i
0.846-0.615i
0.846+0.615i
0.806-0.667i
0.806+0.667i
0.762-0.716i
0.762+0.716i
0.716-0.762i
0.716+0.762i
0.667-0.806i
0.667+0.806i
0.615-0.846i
0.615+0.846i
0.560-0.883i
0.560+0.883i
0.504-0.916i
0.504+0.916i
0.445-0.946i
0.445+0.946i
0.385-0.972i
0.385+0.972i
0.323-0.995i
0.323+0.995i
0.260-1.013i
0.260+1.013i
0.196-1.027i
0.196+1.027i
0.131-1.038i
0.131+1.038i
0.066-1.044i
0.066+1.044i
0.000-1.046i
0.000+1.046i
-0.066-1.044i
-0.066+1.044i
-0.131-1.038i
-0.131+1.038i
-0.196-1.027i
-0.196+1.027i
-0.260-1.013i
-0.260+1.013i
-0.323-0.995i
-0.323+0.995i
-0.385-0.972i
-0.385+0.972i
-0.445-0.946i
-0.445+0.946i
-0.504-0.916i
-0.504+0.916i
-0.560-0.883i
-0.560+0.883i
-0.615-0.846i
-0.615+0.846i
-0.667-0.806i
-0.667+0.806i
-0.716-0.762i
-0.716+0.762i
-0.762-0.716i
-0.762+0.716i
-0.806-0.667i
-0.806+0.667i
-0.846-0.615i
-0.846+0.615i
-0.883-0.560i
-0.883+0.560i
-0.916-0.504i
-0.916+0.504i
-0.946-0.445i
-0.946+0.445i
-0.972-0.385i
-0.972+0.385i
-0.995-0.323i
-0.995+0.323i
-1.013-0.260i
-1.013+0.260i
-1.027-0.196i
-1.027+0.196i
-1.038-0.131i
-1.038+0.131i
-1.044-0.066i
-1.044+0.066i
-1.046+0.000i
The question says the complex nos. should be ordered descending by their real parts and then imaginary parts. But this output orders-- real parts descending and imaginary parts ascending. Visit http://www.uvatoolkit.com and check for yourself.
I get WA for this question.
Can someone post some good testcases.
Thanx
Saswat Padhi - lost in for(;;);

Sultan007
New poster
Posts: 2
Joined: Fri Feb 18, 2011 9:00 am

Re: 10378 - Complex Numbers

Post by Sultan007 »

I am trying this problem again and again .But I cannot find error in this code .Why WA ?? Please help ...

#include<iostream>
#include<cstdio>
#include<cmath>
#include <algorithm>
using namespace std;

#define PI 3.14159265


class CM
{
public:
double x,y;
bool operator<(const CM &c)const
{
if(fabs(x-c.x)>0.000001)
return(x>c.x);
else
return(y>c.y);
}
};



int main ()
{
int a,b,m,test=1,i;
char ch,ch1;
double modulus,argument,a2,a1,m1;
CM COMPLEX[2000];
while(cin>>a>>ch>>b>>ch1>>m)
{

modulus = sqrt(a*a+b*b);
argument = atan (b/double(a)) * 180.0 / PI;
if(ch=='-')
argument*=-1;
printf("Case %d:\n",test);
modulus=pow(modulus,1/double(m));

for(i=0;i<m;i++)
{
a2=(2*i*180+argument)/m;
a1=modulus*cos(a2*PI/180);
m1=modulus*sin(a2*PI/180);

if(fabs(a1)<0.0005)
a1=0;
if(fabs(m1)<0.0005)
m1=0;

COMPLEX.x=a1;
COMPLEX.y=m1;
}
sort(COMPLEX,COMPLEX+sizeof(COMPLEX)/sizeof(COMPLEX));
for(i=0;i<m;i++)
{
if(COMPLEX.y<0)
printf("%.3lf%.3lfi\n",COMPLEX.x,COMPLEX.y);
else
printf("%.3lf+%.3lfi\n",COMPLEX.x,COMPLEX.y);
}

printf("\n");
++test;
}
return 0;
}

Post Reply

Return to “Volume 103 (10300-10399)”