Friday, September 11, 2009

最大公因數(GCD)

最大公因數(Greatest Common Divisor,簡寫為G.C.D.;或Highest Common Factor,簡寫為H.C.F.)
這是很多程式的入門練習題,尤其是使用遞迴呼叫(recursive call)的方式。

如何找出兩個數的最大公因數呢?
最常見的方式是輾轉相除法---輾轉相除法,又名歐幾里得演算法(Euclidean algorithm)乃求兩個正整數之最大公因數的演算法。
這是已知最古老的演算法, 其可追溯至前300年。首次出現於歐幾里得的《幾何原本》(第VII卷,命題i和ii)中,而在中國則可以追溯至東漢出現的《九章算術》。這演算法並不需要把二數作質因數分解。

以下分別以C++實作這兩個方法:

《九章算術》
計算最大公因數
請輸入兩個數字:
1st: 96
2nd: 72
96>72, 96-72=24
24<72, 72-24=48
24<48, 48-24=24
24=24, 最大公因數為:24
請按任意鍵繼續 . . .

#include <iostream>
using namespace std;
int fun(int a, int b)
{
if(a>b)
{
cout << a << ">" << b << ", " << a << "-" << b << "=" << a-b << endl;
return fun(a-b,b);
}
else if(a<b)
{
cout << a << "<" << b << ", " << b << "-" << a << "=" << b-a << endl;
return fun(a,b-a);
}
else
{
cout << a << "=" << b << ", ";
return (a);
}
}
int main(){
int x,y;
cout << "計算最大公因數\n" << "請輸入兩個數字:\n";
cout << "1st: ";
cin >> x;
cout << "2nd: ";
cin >> y;
cout << "最大公因數為:" << fun(x,y) << endl;
system("Pause");
return 0;
}


《幾何原本》
計算最大公因數
請輸入兩個數字:
1st: 96
2nd: 72
96>72, 96%72=24
72%24=0, 最大公因數為:24
請按任意鍵繼續 . . .

#include<iostream>
using namespace std;
int fun(int a, int b){
if(a%b){
cout<<a<<">"<<b<<", "<<a<<"%"<<b<<"="<<a%b<<endl;
return fun(b, a%b);
}
else{
cout<<a<<"%"<<b<<"=0, ";
return (b);
}
}
int main(){
int x,y;
cout<<"計算最大公因數\n"<<"請輸入兩個數字:\n";
cout<<"1st: ";
cin>>x;
cout<<"2nd: ";
cin>>y;
if(x>y)
cout<<"最大公因數為:"<<fun(x,y)<<endl;
else if(x<y)
cout<<"最大公因數為:"<<fun(y,x)<<endl;
else
cout<<"最大公因數為:"<<x<<endl;
system("Pause");
return 0;
}

Sunday, August 02, 2009

調整Vista Auto-Tuning功能

由於買了新的NB搭載新的作業系統Vista,所以讓我開始對他的各項功能感到興趣。其中"改善Vista網路連線",這個詞一直在Google上出現Vista相關的議題。原來是vista新功能"Auto-Tuning"。

什麼是"Auto-Tuning"?
他是能根據網路流量調整接收資料量的緩衝區大小,在Vista中共有Disabled、HighlyRestricted、Restricted、Normal、Experimental等五種設定,後面會在提到他們的意義與指令。
  
如何調整調整Vista Auto-Tuning功能
1.點選"開始",在指令列打上cmd後,將ctrl+shift+enter同時按以取得使用者最高權限。


2.檢查現在的狀況輸入:
netsh interface tcp show global
P.S:我的設定已經是關閉的狀態。

3.關掉Auto-Tuning功能
netsh interface tcp set global autotuninglevel=disabled



附錄:Auto-Tuning功能共有五種參數可供選擇設定---
1. 設為 Disabled    
Disable the autotunning feature in Vista completely、and fit and lock the RWIN receive
window to default value 65,536 bytes.
輸入:
netsh interface tcp set global autotuninglevel=disabled
   
2. 設為HighlyRestricted    
Allow for the receive window to grow beyond the default value、but do so very conservatively.
In this mode、Vista will by default use RWIN of 16,384 bytes with a scale factor of 2.
輸入:
   netsh interface tcp set global autotuninglevel=highlyrestricted
3. 設為Restricted
Allow the receive window to grow beyond its default value、but limit such growth in
some scenarios.
輸入:
   netsh interface tcp set global autotuninglevel=restricted
4.設為 Normal (這是Windows 7,Server 2008,及Vista的預設值)   
Allow for the receive window to grow to accommodate almost all scenarios. The default  
setting in Vista. Specifying this command mean you want to turn back on AutoTuning feature.
輸入:
   netsh interface tcp set global autotuninglevel=normal
5.設為Experimental    
Allow for the receive window to grow to accommodate extreme scenarios. Note The  
experimental value can decrease performance in common scenarios. This value should be
used only for research purposes.
輸入:
   netsh interface tcp set global autotuninglevel=experimental

Saturday, January 05, 2008

VSFTPD在Fedora core 8上安裝全記錄

  • 先檢查是否有安裝vsftpd
[root@localhost ~]# rpm -qa|grep vsftpd
vsftpd-2.0.5-19.fc8

如果沒有,先使用yum安裝吧!
[root@localhost ~]# yum -y install vsftpd

  • 啟動vsftpd
[root@localhost ~]# /etc/init.d/vsftpd start
正在啟動 vsftpd 中的 vsftpd: [ 確定 ]

如果要在開機時就開啟,那先去鍵入「ntsysv」設定吧!

  • 將防火牆中vsftpd所使用的port打開
[root@localhost ~]# iptables -I RH-Firewall-1-INPUT 1 -p tcp --dport 21 -i eth0 -j ACCEPT
[root@localhost ~]# iptables -L -n
......
Chain RH-Firewall-1-INPUT (1 references)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:21
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
......
儲存剛剛插入的chain中的規則
[root@localhost ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 確定 ]
規則將會儲存在 /etc/sysconfig/iptables 檔案中,會在服務啟動時生效,包括重新開機之後。

Sunday, December 30, 2007

Ubuntu panic : cpu too old for this kernal無法啟動解決方法

由於Ubuntu Server版的Kernel在虛擬主機上執行時會出現無法開機的狀況,因此需要將Kernel改成linux-386的版本!
以下簡述在VMware5.5.3版的解決過程:
先設定BIOS改由CD開機。
重新開機後進入Rescue a broken system模式
進入救援模式後會有許多初始化的設定,但都是很簡單的選擇,在此便不再贅述!以下是掛載救援模式的畫面。

選擇在剛剛掛載好的救援系統執行shell才能獲得完整的指令系統!
然後在進行Kernel的更新!在執行以下指令前,先確保網路是暢通的。
sudo apt-get update
sudo apt-get install linux-386
sudo apt-get remove linux-server

參考資料:
panic : cpu too old for this kernal無法啟動,http://www.ubuntu.org.tw/modules/newbb/viewtopic.php?topic_id=7481

Sunday, October 07, 2007

在Linux下新增Winodws字型

對於習慣Windows的人,在轉換到Linux上最大的不便,我想因該是字型吧?!
因為Office系列的普遍使用,造成轉換時字體變得很怪異,尤其是中文的部份尤甚!
如果習慣於在雙系統下打滾的人,更是體會良多啊!

以下這個方式可以將windows的字型那去Linux上用:
複製/WINDOWS/Fonts下的所以有字型到Linux的/usr/share/fonts下便可使用了!

這真是個方便又省時的方法,不過別忘囉!windows的字型是有版權的,可不能任意發佈喔!

參考資料
Fedora 7下常用软件及字体的安装方法介绍,http://soft.zdnet.com.cn/software_zone/2007/0927/526894.shtml