要注意一下題目的換行是要在兩個output之間 , 最後一個output就不能送出 endline ..
Archive for the ‘ACM’ tag
[ACM] 3n+1
太久沒有寫code了 , 想說來寫寫練習一下 , 剛好看到有人的3n+1的code , 想說來寫個比較簡短的版本 , 直接recursive 下去做真的是方便時很多呢!
BTW , 我發現我真的很愛用三元運算子= =” , 清楚又短~
#include <stdio.h>
void cal(int,int);
int m_cal(int,int);
int main (int argc, const char * argv[]) {
int input1,input2;
while(scanf("%d%d",&input1,&input2)!=-1){
cal(input1,input2);
}
return 0;
}
int m_cal(int a,int length){
if(a==1){
return length;
}else if(a%2==1){
return m_cal(a*3+1,length+1);
}else{
return m_cal(a/2,length+1);
}
}
void cal(int a,int b){
int i=a>b?b:a,j=a>b?a:b;
int temp,memory=0;
for(i;i<=j;i++){
temp = m_cal(i,1);
memory = temp>memory ? temp:memory;
}
printf("%d %d %dn",a,b,memory);
}
[ACM]412 Pi
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;
}
[ACM] 最近在解ACM..
好久沒有解ACM了 , 我發現現在我要解一些題目 , 就感覺比較不會那麼的困擾了 …
現在是用ZeroJudge的線上解題系統來解題的 , 以後如果有解出的題目 , 除了一些
很簡單的不會放外 (像是什麼物理子彈那種= =) 其他的都會放上來一下:D