## 412 - Pi

Moderator: Board moderators

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
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.
Ami ekhono shopno dekhi...
HomePage

thomas1016
New poster
Posts: 19
Joined: Mon May 29, 2006 4:12 pm

### 412 WA

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;
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;
}``````

New poster
Posts: 45
Joined: Sun Jun 26, 2005 6:21 am
Contact:

### pi 412(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={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;
}``````

little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm
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.

New poster
Posts: 45
Joined: Sun Jun 26, 2005 6:21 am
Contact:

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={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;
}``````

payton1
New poster
Posts: 5
Joined: Mon Oct 02, 2006 10:31 pm

### Using c++ to write 412

Anybody can tell me in c++ how to show the '0' in 3.000000?

pankaj trivedi
New poster
Posts: 11
Joined: Wed Jun 21, 2006 5:11 pm
Contact:
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;
}
``````
Anything other than Accepted is irritating,even Presentation Error

http://acm.uva.es/problemset/usersjudge.php?user=40301

payton1
New poster
Posts: 5
Joined: Mon Oct 02, 2006 10:31 pm
thank you,it solve my problem

srivatsan r
New poster
Posts: 2
Joined: Sat Mar 17, 2007 8:38 am

### WA 412

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;
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;
}

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
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.
Ami ekhono shopno dekhi...
HomePage

srivatsan r
New poster
Posts: 2
Joined: Sat Mar 17, 2007 8:38 am
Thank u..now accepted.

stalf
New poster
Posts: 7
Joined: Fri Apr 06, 2007 7:57 pm
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

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Contact:
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.
Ami ekhono shopno dekhi...
HomePage

stalf
New poster
Posts: 7
Joined: Fri Apr 06, 2007 7:57 pm
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

stalf
New poster
Posts: 7
Joined: Fri Apr 06, 2007 7:57 pm
hey
I thought I had already submitted the code without these two lines, but I hadn't. Now got ac.
Thanks a lot.
Last edited by stalf on Sat Apr 21, 2007 8:33 pm, edited 1 time in total.