412 - Pi
Moderator: Board moderators
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
HomePage
-
- 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[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)
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;
}
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
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
Anybody can tell me in c++ how to show the '0' in 3.000000?
-
- 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
http://acm.uva.es/problemset/usersjudge.php?user=40301
-
- 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[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;
}
#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;
}
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.
Hope these help.
Ami ekhono shopno dekhi...
HomePage
HomePage
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..
any help is appreciated
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);
}
}
Try the set below
Input:
Output:
Hope it helps.
Input:
Code: Select all
14
11701
31316
20671
5786
12263
4313
24355
31185
20053
912
10808
1832
20945
4313
0
Code: Select all
2.833622
Ami ekhono shopno dekhi...
HomePage
HomePage
hi there,
i got the output you said if i comment the following lines:
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
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--;
Anyways, I tried to submit it without these lines and also got wa.
Thanks for the help