Posted: Mon Jan 09, 2006 5:01 pm
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.
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;
}
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;
}
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;
}
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;
}
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);
}
}
Code: Select all
14
11701
31316
20671
5786
12263
4313
24355
31185
20053
912
10808
1832
20945
4313
0
Code: Select all
2.833622
Code: Select all
if(i>0 && spaces[i]==spaces[i-1])
count--;
else if(j>0 && spaces[j]==spaces[j-1])
count--;