[ACM]412 Pi
2009/01/22
1.先把π的公式由數學式導出來
2.要用到數學的排列組合(C X取2這樣)
3.算互質的個數
4.格式化輸出(精確到小數點下第六位)
心得:在算最後一步的時候(求π時) , 要小心那個double的地方 , 因為int
在算的時候會有拾去的情況 , 最好是都用同個型態去算 , 比較不會出問題
#include <iostream> #include <stdio.h> #include <math.h> using namespace std; int c_num(int i){ return (i*(i-1))/2; } void sort(int arr[],int num){ int i,j; for(i=0;i<num-1;i++){ for(j=i+1;j<num;j++){ if(arr[i]<arr[j]){ swap(arr[i],arr[j]); } } } } void swap(int* a,int* b){ int temp; temp = *a; *a = *b; *b = temp; } int judge(int a,int b){ int max = (a<b)?a:b; int min = (a<b)?a:b; int reminder,quotient; reminder = max%min; quotient = max/min; while(reminder!=0){ max = min; min = reminder; quotient = max/min; reminder = max%min; } return min; } int main(){
int i,j,k; int arr[200],count; while(cin >> i){ count = 0; if(i==0){ return 0; }else{ for(j=0;j<i;j++){ cin >> arr[j]; } sort(arr,i); for(j=0;j<i-1;j++){ for(k=j+1;k<i;k++){ if(judge(arr[k],arr[j])==1){ count++; } } } if(count==0){ cout << "No estimate for this data set." << endl; }else{ printf("%.6lfn",sqrt(((double)(6*c_num(i))/count))); } } } system("pause"); return 0; }