Page 5 of 7

Posted: Mon Jan 09, 2006 5:01 pm
by Jan
I think the problem number is wrong. It should be 412 - PI. However, if you are trying to solve 412, you can use gcd method.

412 WA

Posted: Sat Jun 10, 2006 9:38 am
by thomas1016
It seems alright but I got WA.

Code: Select all

#include <iostream>
#include <cmath>
//#include <ctype>
#include <iomanip>
using namespace std;
int gcd(int,int);
int main(void){
int a[50];
int k,u,j,t,m;
while(cin>>k){
              if(k==0){
                       break;
                       }
               m=0;
              t=0;
          for(u=0;u<k;cin>>a[u],u++);
         // for(u=0;u<k;cout<<a[u]<<" ",u++);
          for(u=0;u<k-1;u++){
             for(j=u+1;j<k;j++){
                               m++;
                           //     cout<<"   u="<<u<<"   j="<<j<<endl;
                            //    cout<<" a[]="<<a[u]<<"  a[]="<<a[j]<<endl;
                              //  cout<<"    gcd"<<gcd(a[u],a[j])<<endl;
                                if(gcd(a[u],a[j])==1){
                                                     t++;
                                                      }
                                }
             
             
         
                                        }
             // cout<<endl<<m<<"  "<<t<<endl;
            // cout<<sqrt(6*m/t)<<endl;
            if(t==0){
                     cout<<"No estimate for this data set."<<endl;
                     continue;
                     }
          cout.setf(ios::fixed);
          cout.precision(6);
                cout<<sqrt(6*m/t)<<endl;
              }




   // system("pause");
    return 0;
    }
int gcd(int a,int b){
int tmp;
if(a>=b){tmp=a;
         a=b;
         b=tmp;
         }
 while(b%a!=0){
       //  cout<<"                a="<<a<<"   b="<<b<<endl;
         
         b=b%a;
         tmp=a;
         a=b;
         b=tmp;
         if(a>=b){tmp=a;
         a=b;
         b=tmp;
         
         }
         
      //   cout<<"after              a="<<a<<"   b="<<b<<endl;
         }
         return a;   
}

pi 412(WA)

Posted: Thu Sep 28, 2006 9:03 am
by marcadian
I have read other thread,try the i/o and it still WA

Code: Select all

#include <stdio.h>
#include <cmath>
//using namespace std;
int gcd(int m,int n)
{
	int t;
	if (m<n)
	{
		t=m;
		m=n;
		n=t;
	}

	while (n!=0)
	{
		t=m;
		m=n;
		n=t%n;
	}
	return m;
}

int main()
{
	int n,i,j,angka[100]={0},counter,counter2;
	scanf("%d",&n);

	while (n!=0)
	{
		counter = counter2 = 0;
		for (i=1;i<=n;i++)
			scanf("%d",&angka[i]);
		for (i=1;i<=n-1;i++)
		{
			for (j=i+1;j<=n;j++)
			{
				counter2++;
				if (gcd(angka[i],angka[j])==1)
				{
					counter++;
				//	printf("%d %d\n",angka[i],angka[j]);

				}
			}
		}
	//	printf("%d %d\n",counter,counter2);
		if (counter==0) puts("No estimate for this data set.");
		else
			printf("%.6Lf\n",(sqrt(6.0*counter2/counter)));
		scanf("%d",&n);
	}
	return 0;
}

Posted: Thu Sep 28, 2006 10:54 am
by little joey
You say you've read the other threads for this problem? So why don't you post in on of them, but create yet another one?
To keep the forum useable for everyone, the number of threads per problem should be as low as possible. If there are tens of threads per problem, nobody can find anything.

Posted: Fri Sep 29, 2006 5:21 pm
by marcadian
I have read other thread,try the i/o and it still WA

Code: Select all

#include <stdio.h>
#include <cmath>
//using namespace std;
int gcd(int m,int n)
{
   int t;
   if (m<n)
   {
      t=m;
      m=n;
      n=t;
   }

   while (n!=0)
   {
      t=m;
      m=n;
      n=t%n;
   }
   return m;
}

int main()
{
   int n,i,j,angka[100]={0},counter,counter2;
   scanf("%d",&n);

   while (n!=0)
   {
      counter = counter2 = 0;
      for (i=1;i<=n;i++)
         scanf("%d",&angka[i]);
      for (i=1;i<=n-1;i++)
      {
         for (j=i+1;j<=n;j++)
         {
            counter2++;
            if (gcd(angka[i],angka[j])==1)
            {
               counter++;
            //   printf("%d %d\n",angka[i],angka[j]);

            }
         }
      }
   //   printf("%d %d\n",counter,counter2);
      if (counter==0) puts("No estimate for this data set.");
      else
         printf("%.6Lf\n",(sqrt(6.0*counter2/counter)));
      scanf("%d",&n);
   }
   return 0;
}

Using c++ to write 412

Posted: Mon Oct 02, 2006 10:36 pm
by payton1
Anybody can tell me in c++ how to show the '0' in 3.000000?

Posted: Tue Oct 03, 2006 4:51 pm
by pankaj trivedi
Though i am not very sure about your question. Are you looking for this

Code: Select all

#include<iostream>
#include<iomanip>
using namespace std;
main()
{

float x=3;
cout.setf(ios::fixed, ios::floatfield);
cout.precision(7);
cout<<x;
}

Posted: Tue Oct 03, 2006 6:01 pm
by payton1
thank you,it solve my problem

WA 412

Posted: Sat Mar 17, 2007 8:49 am
by srivatsan r
im getting the correct output for all the test cases given in the question..but still im getting a WA..pls help me with my code..

#include<iostream.h>
#include<stdlib.h>
#include<math.h>
int cf(int a,int b)
{
while(a!=0&&b!=0)
{
if(a>b)
a=a%b;
else
b=b%a;
if(a==1||b==1)
return 0;
}
return 1;
}
int main()
{
int n;
int i,j;
double count=0,c=0;
int a[50];
double pi,y,z;
while(cin>>n)
{
if(n==0)
exit(0);
if(n>1&&n<=50)
{
for(i=0;i<n;i++)
cin>>a;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
c++;
if(!cf(a,a[j]))
count++;
}
}
if(count>0)
{
z=(c/count);
y=6*z;
pi=sqrt(y);
cout<<pi<<"\n";
}
else
cout<<"No estimate for this data set.\n";
}
else
exit(0);
n=0;
i=0;
j=0;
count=0.0;
c=0.0;
pi=0.0;
}
return 0;
}

Posted: Sat Mar 17, 2007 11:51 am
by Jan
Your code doesnot even pass the samples! And you are printing using 'cout' (without any precision setting). So, if the output is '3.000000', your code will prodce '3'.

Hope these help.

Posted: Tue Mar 20, 2007 3:35 pm
by srivatsan r
Thank u..now accepted.

Posted: Sat Apr 21, 2007 6:53 pm
by stalf
i'm getting wa for this code...
also tried what they say on other posts like not counting repeated numbers and using double. Still don't work..

Code: Select all

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

using namespace std;

int mdc(int a,int b){
	if(b>a)
		return mdc(b,a);
	if(a%b == 0)
		return b;
	return mdc(b,a%b);	
}

int main(){
	int n,m,count;
	double pi;
	bool first = true;
	while(cin>>n){
		if(n==0)
			break;
		if(first)
			first = false;
		else
			cout<<endl;
		int spaces[n];
		count = 0;
		for(int i=0;i<n;i++){
			cin>>spaces[i];
		}
		int size = sizeof(spaces)/sizeof(int);
		sort(spaces, spaces + size);
		for(int i=0;i<n;i++){
			for(int j=i+1;j<n;j++){
				if(mdc(spaces[i],spaces[j]) == 1){
					count ++;
					if(i>0 && spaces[i]==spaces[i-1])
						count--;
					else if(j>0 && spaces[j]==spaces[j-1])
						count--;
				}
			}	
		}
		pi = 0;
		if(count != 0){
			double pair = n*(n-1)/2;
			pi = sqrt(6*pair/count);
		}
		if(pi==0)
			printf("No estimate for this data set.");
		else
			printf("%.6lf",pi);
	}
}

any help is appreciated

Posted: Sat Apr 21, 2007 7:55 pm
by Jan
Try the set below

Input:

Code: Select all

14
11701
31316
20671
5786
12263
4313
24355
31185
20053
912
10808
1832
20945
4313
0
Output:

Code: Select all

2.833622
Hope it helps.

Posted: Sat Apr 21, 2007 8:03 pm
by stalf
hi there,
i got the output you said if i comment the following lines:

Code: Select all

if(i>0 && spaces[i]==spaces[i-1])
   count--;
else if(j>0 && spaces[j]==spaces[j-1])
   count--;
but this way, if the set is 2 2 3 4 5, i'll consider combinations like 2,5 twice... Is this right?
Anyways, I tried to submit it without these lines and also got wa.
Thanks for the help

Posted: Sat Apr 21, 2007 8:06 pm
by stalf
hey
I thought I had already submitted the code without these two lines, but I hadn't. Now got ac.
Thanks a lot.