# 命令提示建樹狀索引

```2010.07.10

TREE D:\大學上課資料 /F > d:data.txt

TREE d:\MUSIC /F > d:M.TXT

TREE d:\MUSIC /F > d:M.DOC

TREE D:\Music\已備份\MusicCD1 /F > D:\Music\已備份\MusicCD1.txt

TREE H:\ /F > D:\Music\已備份\MusicCD2.txt

C:\temp> cd d:\Data
C:\temp> d:```

# nAmB Version 1.0~

## Version 1.0

```[50240] 14 4 6 b15      0A1B
[50241] a15 b10 8 5     1A1B
[50242] 14 20 b3 6      0A1B
[50243] 8 17 b3 16      0A1B
[50244] a15 a9 a10 a3   4A0B
============================Conclusion============================
Guess 50244 times
Ans= 15 9 10 3
range=20 , amount=4
carry=123803 , efficiency=59.4162 %
Running Time--------> 45.578 seconds```
```//2012.06.21
//Version 1.0
//1.nAmB
//3.Random guess number
//4.Time and Count
//5.Carry=(a[0]a[1]a[2])range → (a[0]a[1]a[2])10
//6.Test the efficiency of guess

#include <cstdlib>
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;

int main()
{
clock_t tstart,tend;
int range,amount,i,j,A=0,B=0,count=0;
double efficiency=0,order=1,carry=0;
cout <<"input range=?\n";
cin >>range;
cout <<"input amount=?\n";
cin >>amount;
while(amount>range)
{
cout <<"amount <= range !!!\n";
cout <<"input amount=?\n";
cin >>amount;
}
vector<int>ans(amount);
vector<int>Tans(range+1);	//Test answer did not repeat
vector<int>guess(amount);
vector<int>Tguess(range+1);	//Test guess did not repeat
cout <<"input Answer Number=? "<<amount<<" Numbers  Range=1~"<<range<<endl;
for(i=0;i<amount;i++)
{
cin >>ans[i];
while(ans[i]<1)
{
cout <<"Answer Numbers > 0 !!!\n";
cin >>ans[i];
}
while(ans[i]>range || Tans[ans[i]]>0)
{
if(ans[i]>range)
cout <<"Answer Numbers <= range !!!\n";
else if(Tans[ans[i]]>0)
cout <<"Answer Numbers of repeat !!!\n";
cin >>ans[i];
}
}

tstart = clock();
//carry=(a[0]a[1]a[2])range → (a[0]a[1]a[2])10
for(i=0;i<amount;i++)
{
for(j=amount-1-i;j>0;j--)
order*=range;
carry+=ans[i]*order;
order=1;
}

srand(time(NULL));
while(1)
{
cout <<"["<<count+1<<"] ";
for(i=0;i<amount;i++)
{
guess[i]=rand()%range+1;
while(Tguess[guess[i]]>0)
guess[i]=rand()%range+1;
Tguess[guess[i]]++;		//Record already choose the Guess
if(Tguess[guess[i]]==Tans[guess[i]])
{
if(guess[i]==ans[i])
{
A++;	//The same location & number → A++
cout <<"a"<<guess[i]<<" ";
}
else
{
B++;	//The same location → B++
cout <<"b"<<guess[i]<<" ";
}
}
else
cout <<guess[i]<<" ";
}
cout<<"\t"<<A<<"A"<<B<<"B"<<endl;
for(i=0;i<range+1;i++)
Tguess[i]=0;
count++;
if(A==amount)
break;
B=0;
A=0;
}
efficiency=(1-(count/carry))*100;
cout<<"============================Conclusion============================\n";
cout<<"Guess "<<count<<" times\nAns= ";
for(i=0;i<amount;i++)
cout<<ans[i]<<" ";
cout<<"\nrange="<<range<<" , amount="<<amount<<endl;
cout<<"carry="<<carry<<" , efficiency="<<efficiency<<" %"<<endl;
tend = clock();
cout<<"Running Time--------> "<< double(tend-tstart)/CLOCKS_PER_SEC<<" seconds\n";

system("pause");
return 0;
}```

# Lotto Version 1.0~4.0

## Version 4.0

```=========================Bets & Profit=========================
OpenStar=4
☆=1 , Bingo=527678 , \$=0
☆=2 , Bingo=257715 , \$=0
☆=3 , Bingo=56004 , \$=0
★=4 , Bingo=5728 , \$=304.762
★=5 , Bingo=257 , \$=7619.05
★=6 , Bingo=2 , \$=274286
★=7 , Bingo=0 , \$=1.344e+007
Bet Numbers=3  19  24  37  46  7  11   , RandomRate=1
cost=50 , profit=4.25234e+006
Total cost=6.17284e+007 , Total profit=-5.7476e+007
===========================Max & Avg===========================
total=49  amount=7  set=1234567
Max: N=3: B=177234 , P=14.356 %
Avg: B=176367 , P=14.2857 %
Running Time--------> 1587.64 seconds```
```//2012.06.20
//Version 4.0
//1.亂數產生Lotto開獎號碼
//2.計算最高機率號碼
//3.計算平均出現機率
//4.程式執行時間
//5.Lotto開獎號碼不重複
//6.Lotto開獎號碼排序
//7.Lotto下注，彩金為特定倍率*RandomRate

#include <cstdlib>
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;

int main()
{
clock_t tstart,tend;
int amount=0,total=0,set=0,i=0,j=0,k=0,max=0,temp=0,star=0,OpenStar=0;
double Tset=0,Ttotal=0,Tamount=0,cost=0,profit=0,RandomRate=0,t=0;//結算機率時使用double運算

cout <<"input total=?\n";	//輸入可選號碼數量
cin >>total;
cout <<"input amount=?\n";	//輸入開獎號碼數量
cin >>amount;
while(amount>total)			//測試開獎號碼量小於可選號碼量
{
cout <<"amount <= total !!!\n";
cout <<"input amount=?\n";	//重新輸入開獎號碼數量
cin >>amount;
}

cout <<"input OpenStar=?\n";	//輸入開獎幾星有獎金
cin >>OpenStar;
while(OpenStar>amount)			//測試有獎金號碼量小於開獎號碼數量
{
cout <<"OpenStar <= amount !!!\n";
cout <<"input OpenStar=?\n";	//重新輸入開獎幾星有獎金
cin >>OpenStar;
}

vector<int>bets(amount);		//單組下注號碼
vector<int>Tbets(total+5);		//紀錄單組已選號碼
vector<int>Tstar(amount+5);		//紀錄各星中獎注數
vector<double>money(amount+5);	//獎金計算用
for(i=0;i<total+5;i++)
Tbets[i]=0;

cout <<"input Bet Numbers=?  "<<amount<<" Numbers\n";	//輸入下注號碼
for(i=0;i<amount;i++)
{
cin >>bets[i];
while(bets[i]>total || Tbets[bets[i]]>0)
{
if(bets[i]>total)	//測試下注號碼量小於可選號碼量
cout <<"Bet Numbers <= total !!!\n";
else if(Tbets[bets[i]]>0)			//測試重複下注號碼
cout <<"Bet Numbers of repeat !!!\n";
cout <<"input Bet Numbers=?\n";			//重新輸入開獎號碼數量
cin >>bets[i];
}
Tbets[bets[i]]++;
}

cout <<"input set=?\n";			//輸入開獎組數
cin >>set;
cout <<"input each set cost=?\n";	//輸入單注金額
cin >>cost;

tstart = clock();
Tset=set;
Ttotal=total;
Tamount=amount;
vector<int>lotto(amount+5);	//單組開獎號碼
vector<int>Tlotto(total+5);	//紀錄單組已開獎號碼
vector<int>bingo(total+5);	//紀錄全部已開獎號碼
vector<double>Tbingo(total+5);	//紀錄全部已開獎號碼(運算用)
vector<int>SortBingo(total+5);	//排序全部已開獎號碼
vector<int>SortIndex(total+5);	//紀錄排序數字

for(i=0;i<amount+5;i++)
{
lotto[i]=0;
Tstar[i]=0;
}
for(i=0;i<total+5;i++)
{
Tlotto[i]=0;
bingo[i]=0;
Tbingo[i]=0;
SortBingo[i]=0;
SortIndex[i]=0;
}

srand(time(NULL));				//亂數種子
t=(rand()%10)+1;				//RandomRate=t/3
RandomRate=t/3;
money[0]=cost/2*amount/total*RandomRate*OpenStar/27;	//獎金計算
for(i=1;i<=amount;i++)
money[i]=money[i-1]*i*i;			//獎金指數成長計算

for(i=0;i<set;i++)
{
cout <<"["<<i+1<<"] ";
for(j=0;j<amount;j++)
{
lotto[j]=(rand()%total)+1;
while(Tlotto[lotto[j]]>0)	//避免開獎號碼重複
lotto[j]=(rand()%total)+1;

Tlotto[lotto[j]]++;
bingo[lotto[j]]++;
Tbingo[lotto[j]]++;

if(Tlotto[lotto[j]]==Tbets[lotto[j]])	//對中一星
{
star++;
cout <<"+"<<lotto[j]<<" ";
}
else
cout <<lotto[j]<<" ";

if(max<bingo[lotto[j]])
{
max=bingo[lotto[j]];
temp=lotto[j];
}
}
Tstar[star]++;

if(star>=OpenStar)
{
cout<<" ★="<<star<<" \$="<<money[star]<<endl;
profit+=money[star];
}
else
cout<<" ☆="<<star<<" \$=0"<<endl;
for(k=0;k<total+5;k++)
Tlotto[k]=0;	//單組開獎完成，清空紀錄
star=0;			//單組對獎完成，清空紀錄
}

for(i=0;i<total+5;i++)
{
while(bingo[i]>0)	//僅顯示有開出的號碼紀錄
{
cout<<"N="<< i<<" , B="<<bingo[i]<<" , P="<<Tbingo[i]/Tset*100<<" %"<<endl;
break;
}
SortBingo[i]=bingo[i];
SortIndex[i]=i;
}
int x,y;			//Lotto開獎號碼排序
for(i=0;i<total+5;i++)
{
for(j=0;j<total+5;j++)
{
if(SortBingo[j]>SortBingo[j+1])
{
x=SortBingo[j+1];
y=SortIndex[j+1];
SortBingo[j+1]=SortBingo[j];
SortIndex[j+1]=SortIndex[j];
SortBingo[j]=x;
SortIndex[j]=y;
}
}
}

cout<< "=======================Sort Bingo Number=======================\n";
j=1;
for(i=total+5;i>5;i--)
{
cout <<"No."<<j<<"  N="<<SortIndex[i]<<" , B="<<SortBingo[i]<<"\n";
j++;
}
cout<< "=========================Bets & Profit=========================\n";
cout<<"OpenStar="<<OpenStar<<endl;
for(i=1;i<=amount;i++)
{
if(i>=OpenStar)
cout <<"★="<<i<<" , Bingo="<<Tstar[i]<<" , \$="<<money[i]<<endl;
else
cout <<"☆="<<i<<" , Bingo="<<Tstar[i]<<" , \$=0"<<endl;
}
cout <<"Bet Numbers=";
for(i=0;i<amount;i++)
cout <<bets[i]<<"  ";
cout<<" , RandomRate="<<RandomRate<<endl;
cout<<"cost="<<cost<<" , profit="<<profit<<endl;
cout<<"Total cost="<<cost*set<<" , Total profit="<<profit-cost*set<<endl;
cout<< "===========================Max & Avg===========================\n";
cout <<"total="<<total<<"  amount="<<amount<<"  set="<<set<<"\n";
cout<< "Max: N="<< temp<<": B="<<max<<" , P="<<Tbingo[temp]/Tset*100<<" %"<<endl;
cout<< "Avg: B="<<Tamount*Tset/Ttotal<<" , P="<<Tamount/Ttotal*100<<" %"<<endl;
tend = clock();
cout <<"Running Time--------> "<< double(tend-tstart)/CLOCKS_PER_SEC<<" seconds\n";

system("pause");
return 0;
}```

## Version 3.0

```//2012.06.20
//Version 3.0
//1.亂數產生Lotto開獎號碼
//2.計算最高機率號碼
//3.計算平均出現機率
//4.程式執行時間
//5.Lotto開獎號碼不重複
//6.Lotto開獎號碼排序

#include <cstdlib>
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;

int main()
{
clock_t tstart,tend;
int amount=0,total=0,set=0,i=0,j=0,k=0,max=0,temp=0;
double Tset=0,Ttotal=0,Tamount=0;	//結算機率時使用double運算

cout <<"input total=?\n";	//輸入可選號碼數量
cin >>total;
cout <<"input amount=?\n";	//輸入開獎號碼數量
cin >>amount;
while(amount>total)			//測試開獎號碼量小於可選號碼量
{
cout <<"amount <= total !!!\n";
cout <<"input amount=?\n";	//重新輸入開獎號碼數量
cin >>amount;
}
cout <<"input set=?\n";	//輸入開獎組數
cin >>set;

Tset=set;
Ttotal=total;
Tamount=amount;
vector<int>lotto(amount+5);		//單組開獎號碼
vector<int>Tlotto(total+5);		//紀錄單組已開獎號碼
vector<int>bingo(total+5);		//紀錄全部已開獎號碼
vector<double>Tbingo(total+5);	//紀錄全部已開獎號碼(運算用)
vector<int>SortBingo(total+5);	//排序全部已開獎號碼
vector<int>SortIndex(total+5);	//紀錄排序數字

tstart = clock();
for(i=0;i<amount+5;i++)
lotto[i]=0;
for(i=0;i<total+5;i++)
{
bingo[i]=0;
Tbingo[i]=0;
Tlotto[i]=0;
}
srand(time(NULL));				//亂數種子
for(i=0;i<set;i++)
{
cout <<"["<<i+1<<"] ";
for(j=0;j<amount;j++)
{
lotto[j]=(rand()%total)+1;
while(Tlotto[lotto[j]]>0)	//避免開獎號碼重複
lotto[j]=(rand()%total)+1;
cout <<lotto[j]<<" ";
Tlotto[lotto[j]]++;
bingo[lotto[j]]++;
Tbingo[lotto[j]]++;
if(max<bingo[lotto[j]])
{
max=bingo[lotto[j]];
temp=lotto[j];
}
}
cout<<endl;
for(k=0;k<total+5;k++)
Tlotto[k]=0;	//單組開獎完成，清空紀錄
}

for(i=0;i<total+5;i++)
{
while(bingo[i]>0)	//僅顯示有開出的號碼紀錄
{
cout<<"N="<< i<<" , B="<<bingo[i]<<" , P="<<Tbingo[i]/Tset*100<<" %"<<endl;
break;
}
SortBingo[i]=bingo[i];
SortIndex[i]=i;
}
int p,x,y;				//Lotto開獎號碼排序
for(k=0;k<total+5;k++)
{
for(p=0;p<total+5;p++)
{
if(SortBingo[p]>SortBingo[p+1])
{
x=SortBingo[p+1];
y=SortIndex[p+1];
SortBingo[p+1]=SortBingo[p];
SortIndex[p+1]=SortIndex[p];
SortBingo[p]=x;
SortIndex[p]=y;
}
}
}

cout<< "======================Sort Bingo Number======================\n";
j=1;
for(i=total+5-1;i>4;i--)
{
cout <<"No."<<j<<"  N="<<SortIndex[i]<<" , B="<<SortBingo[i]<<"\n";
j++;
}
cout<< "=========================Max & Avg=========================\n";
cout <<"total="<<total<<"  amount="<<amount<<"  set="<<set<<"\n";
cout<< "Max: N="<< temp<<": B="<<max<<" , P="<<Tbingo[temp]/Tset*100<<" %"<<endl;
cout<< "Avg: B="<<Tamount*Tset/Ttotal<<" , P="<<Tamount/Ttotal*100<<" %"<<endl;
tend = clock();
cout <<"Running Time--------> "<< double(tend-tstart)/CLOCKS_PER_SEC<<" seconds\n";

system("pause");
return 0;
}```

## Version 2.0

```//2012.06.19
//Version 2.0
//1.亂數產生Lotto開獎號碼
//2.計算最高機率號碼
//3.計算平均出現機率
//4.程式執行時間
//5.Lotto開獎號碼不重複

#include <cstdlib>
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;

int main()
{
clock_t tstart,tend;
int amount=0,total=0,set=0,i=0,j=0,k=0,max=0,temp=0;
double Tset=0,Ttotal=0,Tamount=0;	//結算機率時使用double運算

cout <<"input total=?\n";	//輸入可選號碼數量
cin >>total;
cout <<"input amount=?\n";	//輸入開獎號碼數量
cin >>amount;
while(amount>total)			//測試開獎號碼量小於可選號碼量
{
cout <<"amount <= total !!!\n";
cout <<"input amount=?\n";	//重新輸入開獎號碼數量
cin >>amount;
}
cout <<"input set=?\n";	//輸入開獎組數
cin >>set;

Tset=set;
Ttotal=total;
Tamount=amount;
vector<int>lotto(amount+5);		//單組開獎號碼
vector<int>Tlotto(total+5);		//紀錄單組已開獎號碼
vector<int>bingo(total+5);		//紀錄全部已開獎號碼
vector<double>Tbingo(total+5);	//紀錄全部已開獎號碼(運算用)

tstart = clock();
for(i=0;i<amount+5;i++)
lotto[i]=0;
for(i=0;i<total+5;i++)
{
bingo[i]=0;
Tbingo[i]=0;
Tlotto[i]=0;
}
srand(time(NULL));				//亂數種子
for(i=0;i<set;i++)
{
cout <<"["<<i+1<<"] ";
for(j=0;j<amount;j++)
{
lotto[j]=(rand()%total)+1;
while(Tlotto[lotto[j]]>0)	//避免開獎號碼重複
lotto[j]=(rand()%total)+1;
cout <<lotto[j]<<" ";
Tlotto[lotto[j]]++;
bingo[lotto[j]]++;
Tbingo[lotto[j]]++;
if(max<bingo[lotto[j]])
{
max=bingo[lotto[j]];
temp=lotto[j];
}
}
cout<<endl;
for(k=0;k<total+5;k++)
Tlotto[k]=0;	//單組開獎完成，清空紀錄
}

for(i=0;i<=total;i++)
while(bingo[i]>0)	//僅顯示有開出的號碼紀錄
{
cout<< i<<": B="<<bingo[i]<<" , P="<<Tbingo[i]/Tset*100<<" %"<<endl;
break;
}

cout<< "=========================Max & Avg=========================\n";
cout <<"total="<<total<<"  amount="<<amount<<"  set="<<set<<"\n";
cout<< "Max: "<< temp<<": B="<<max<<" , P="<<Tbingo[temp]/Tset*100<<" %"<<endl;
cout<< "Avg: B="<<Tamount*Tset/Ttotal<<" , P="<<Tamount/Ttotal*100<<" %"<<endl;
tend = clock();
cout <<"Running Time--------> "<< double(tend-tstart)/CLOCKS_PER_SEC<<" seconds\n";

system("pause");
return 0;
}```

## Version 1.0

```//2012.06.19
//Version 1.0
//1.亂數產生Lotto開獎號碼，號碼可能重複
//2.計算最高機率號碼
//3.計算平均出現機率
//4.程式執行時間

#include <cstdlib>
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;

//void QuickSort(vector<int>list(), int left, int right);
int main()
{
clock_t tstart,tend;
int amount=0,total=0,set=0,i=0,j=0,k=0,max=0,temp=0;
double Tset=0,Ttotal=0,Tamount=0;
cout <<"輸入號碼數量 total=?\n";
cin >>total;
cout <<"輸入開獎號碼數量 amount=?\n";
cin >>amount;
while(amount>total)
{
cout <<"amount 需 <= total\n";
cout <<"輸入開獎號碼數量 amount=?\n";
cin >>amount;
}
cout <<"輸入開獎組數 set=?\n";
cin >>set;
Tset=set;
Ttotal=total;
Tamount=amount;
vector<int>lotto(amount+5);
vector<int>bingo(total+5);
vector<double>Tbingo(total+5);
tstart = clock();
for(i=0;i<total+5;i++)
{
bingo[i]=0;
Tbingo[i]=0;
}
srand(time(NULL));
for(i=0;i<set;i++)
{
cout <<"["<<i+1<<"] ";
for(j=0;j<amount;j++)
{
lotto[j]=(rand()%total)+1;

for(k=0;k<j;k++)
while(lotto[j]==lotto[k])
lotto[j]=(rand()%total)+1;

cout <<lotto[j]<<" ";
bingo[lotto[j]]++;
Tbingo[lotto[j]]++;
if(max<bingo[lotto[j]])
{
max=bingo[lotto[j]];
temp=lotto[j];
}
}
cout<<endl;
}

for(i=0;i<=total;i++)
while(bingo[i]>0)
{
cout<< i<<": B="<<bingo[i]<<" , P="<<Tbingo[i]/Tset*100<<" %"<<endl;
break;
}
/*
QuickSort(bingo, 0, total-1);
for(i=0;i<=total;i++)
while(bingo[i]>0)
{
cout<< i<<": B="<<bingo[i]<<" , P="<<Tbingo[i]/Tset*100<<" %"<<endl;
break;
}
*/
cout<< "=========================Max & Avg=========================\n";
cout <<"total="<<total<<"  amount="<<amount<<"  set="<<set<<"\n";
cout<< "Max: "<< temp<<": B="<<max<<" , P="<<Tbingo[temp]/Tset*100<<" %"<<endl;
cout<< "Avg: B="<<Tamount*Tset/Ttotal<<" , P="<<Tamount/Ttotal*100<<" %"<<endl;
tend = clock();
cout <<"Running Time--------> "<< double(tend-tstart)/CLOCKS_PER_SEC<<" seconds\n";

system("pause");
return 0;
}

void QuickSort(int list[], int left, int right)
{
int i, j, pivot, temp;

if (left < right){
i = left;
j = right + 1;
pivot = list[left];
do
{
do
i++;
while (list[i] < pivot);
do
j--;
while (list[j] > pivot);
/* 若i小於j，交換i與j所指的資料 */
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
while (i < j);
/* j大於i，將list[left] 與索引j所指向的資料交換 */
temp = list[left];
list[left] = list[j];
list[j] = temp;
QuickSort(list, left, j - 1);// 排序左邊
QuickSort(list, j + 1, right);// 排序右邊
}
}```

# Avg Queue

```    假設一個Switch有3個 input ports, 及1個output port, 3個input port進來的packet(封包)

每一input port在每一time slot (亦可以解讀成 1 time slot為一時間單位)會進來一個packet

time slot 時間單位的機率是0.2.
請問長時間下來, switch 的 queue平均長度是多少?

Hint: 可以讓C程式的一個迴圈代表是一個 time slot, 然後在這個迴圈內, 用Random number generator
(C語言的SRAN/RAN)決定每一input port是否有產生packet, 及output port是否可送出 packet, 在宣告

再設一變數來累加每一迴圈queue的數值, 讓迴圈跑很多次 (e.g. 20000次), 這樣就會累加20000次 queue

```//2012.06.18
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;

int main()
{
int TimeSlot=20000,packet=0;
int input1=0,input2=0,input3=0;	//假設一個Switch有3個 input ports
int output=0,output2=0,TestForOut=0;
//假設一個Switch有1個 output ports (1個TimeSlot機率是0.8,需要2個的機率是0.2.)
float TTimeSlot=0,Tpacket=0,AvgQueue=0;
srand(time(NULL));

cout <<"輸入測試時間長度 TimeSlot=?\n";
cin >>TimeSlot;
cout <<"TimeSlot="<<TimeSlot<<"\n";

for(int a=0;a<=TimeSlot;a++)
{
input1=rand()%4;	//會進來一個packet的機率是0.25
input2=rand()%4;
input3=rand()%4;
output=rand()%5;	//packet送出去只需要1個 time slot時間的機率是0.8
output2=rand()%5;	//需要2個time slot 時間單位的機率是0.2.
if(input1==0)
packet++;
if(input2==0)
packet++;
if(input3==0)
packet++;
if(output!=0 && packet>0)	//若queue=0就代表queue是空的不能減 1
{
packet--;
TestForOut=1;
}
if(TimeSlot%2==0 && output2==0 && TestForOut==0 && packet>0)
packet--;

cout <<"input1="<<input1<<"  input2="<<input2<<"  input3="<<input3<<"\n";
cout <<"output="<<output<<"  output2="<<output2<<"\n";
cout <<"packet="<<packet<<"\n\n";
TestForOut=0;
}

Tpacket=packet;
TTimeSlot=TimeSlot;
AvgQueue=Tpacket/TTimeSlot;
cout <<"TimeSlot="<<TimeSlot<<endl;
cout <<"AvgQueue="<<AvgQueue<<endl;

system("pause");
return 0;
}```

# 三角形性質

```//2009.09.10
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main ()
{
char k;
int times=0;
double a,b,c,s1,s2,s3,s,x,A,x1,x2,x3,y1,y2,y3,B,C,D,E,F,G,H,I,J;
double a2,b2,c2,S1,S2,S3,S,X,d,e,f,g,B2,C2,D2,E2,F2,G2,H2,I2,J2;
double f1,f2,f3,g1,g2,g3,h,i,j,l,m,n,o,r,aA,aB,aC,p,q,t;

printf("1.輸入任意字元\n");
scanf("%c",&k);

if(k>='A' && k<='Z')
printf("大寫轉小寫為: %c\n",k+32);
if(k>='a' && k<='z')
printf("小寫轉大寫為: %c\n",k-32);
/*
else
printf("輸入的不是英文字母\n");
*/

printf("\n============================================================\n\n");

printf("2.輸入三個實數作為三角形的三邊長\n");
scanf("%lf%lf%lf",&a,&b,&c);

printf("a=%.3lf\n",a);
printf("b=%.3lf\n",b);
printf("c=%.3lf\n",c);

if(a>=b && a>=c && b>=c)
s1=a,s2=b,s3=c;
else if(a>=b && a>=c && c>=b)
s1=a,s2=c,s3=b;
else if(b>=a && b>=c && a>=c)
s1=b,s2=a,s3=c;
else if(b>=a && b>=c && c>=a)
s1=b,s2=c,s3=a;
else if(c>=b && c>=a && a>=b)
s1=c,s2=a,s3=b;
else if(c>=b && c>=a && b>=a)
s1=c,s2=b,s3=a;

if(s3+s1>s2 && s3+s2>s1 && s2+s1>s3)
{
/*因為兩邊之合大於第三邊,兩邊之差小於第三邊*/
printf("因為 %.2lf + %.2lf > %.2lf , %.2lf > %.2lf - %.2lf\n",s3,s2,s1,s1,s2,s3);
printf("所以 %.2lf, %.2lf, %.2lf 三邊可成為三角形\n",s3,s2,s1);

if(s3*s3+s2*s2==s1*s1)
printf("又 %.2lf * %.2lf + %.2lf * %.2lf = %.2lf * %.2lf  \n故三角形abc為直角三角形\n",s3,s3,s2,s2,s1,s1);
else if(s3*s3+s2*s2<s1*s1)
printf("又 %.2lf * %.2lf + %.2lf * %.2lf < %.2lf * %.2lf  \n故三角形abc為鈍角三角形\n",s3,s3,s2,s2,s1,s1);
else if(s3*s3+s2*s2>s1*s1)
printf("又 %.2lf * %.2lf + %.2lf * %.2lf > %.2lf * %.2lf  \n故三角形abc為銳角三角形\n",s3,s3,s2,s2,s1,s1);
/*s為三角形abc的半周長*/
s=(s1+s2+s3)/2;
x=s*(s-a)*(s-b)*(s-c);
printf("三角形abc面積為 %lf\n",sqrt(x));
}

if(s3+s1<=s2 || s3+s2<=s1 || s2+s1<=s3)
{
/*因為兩邊之合小於等於第三邊,兩邊之差大於等於第三邊*/
printf("因為 %.2lf + %.2lf <= %.2lf , %.2lf <= %.2lf - %.2lf\n",s3,s2,s1,s3,s1,s2);
printf("所以 %.2lf, %.2lf, %.2lf 三邊不可成為三角形\n",s3,s2,s1);
}

printf("\n============================================================\n\n");

printf("3.輸入6個實數作為三點座標 A(x1,y1),B(x2,y2),C(x3,y3)\n");
scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
printf("x1=%.3lf\n",x1);
printf("y1=%.3lf\n",y1);
printf("x2=%.3lf\n",x2);
printf("y2=%.3lf\n",y2);
printf("x3=%.3lf\n",x3);
printf("y3=%.3lf\n",y3);
printf("座標A(%.2lf,%.2lf)\n",x1,y1);
printf("座標B(%.2lf,%.2lf)\n",x2,y2);
printf("座標C(%.2lf,%.2lf)\n",x3,y3);

B=(x1-x2)*(x1-x2);
C=(x1-x3)*(x1-x3);
D=(x2-x3)*(x2-x3);
E=(y1-y2)*(y1-y2);
F=(y1-y3)*(y1-y3);
G=(y2-y3)*(y2-y3);
H=B+E;
I=C+F;
J=D+G;
printf("線段AB長為 %lf\n",sqrt(H));
printf("線段AC長為 %lf\n",sqrt(I));
printf("線段BC長為 %lf\n",sqrt(J));
a2=sqrt(H);
b2=sqrt(I);
c2=sqrt(J);

if(a2>=b2 && a2>=c2 && b2>=c2)
S1=a2,S2=b2,S3=c2;
else if(a2>=b2 && a2>=c2 && c2>=b2)
S1=a2,S2=c2,S3=b2;
else if(b2>=a2 && b2>=c2 && a2>=c2)
S1=b2,S2=a2,S3=c2;
else if(b2>=a2 && b2>=c2 && c2>=a2)
S1=b2,S2=c2,S3=a2;
else if(c2>=b2 && c2>=a2 && a2>=b2)
S1=c2,S2=a2,S3=b2;
else if(c2>=b2 && c2>=a2 && b2>=a2)
S1=c2,S2=b2,S3=a2;

if(S3+S1>S2 && S3+S2>S1 && S2+S1>S3)
{
/*因為兩邊之合大於第三邊,兩邊之差小於第三邊*/
printf("因為 %.2lf + %.2lf > %.2lf , %.2lf > %.2lf - %.2lf\n",S3,S2,S1,S1,S2,S3);
printf("所以 %.2lf, %.2lf, %.2lf 三邊可成為三角形\n",S3,S2,S1);

if(S3*S3+S2*S2==S1*S1)
printf("又 %.2lf * %.2lf + %.2lf * %.2lf = %.2lf * %.2lf  \n故三角形ABC為直角三角形\n",S3,S3,S2,S2,S1,S1);
else if(S3*S3+S2*S2<S1*S1)
printf("又 %.2lf * %.2lf + %.2lf * %.2lf < %.2lf * %.2lf  \n故三角形ABC為鈍角三角形\n",S3,S3,S2,S2,S1,S1);
else if(S3*S3+S2*S2>S1*S1)
printf("又 %.2lf * %.2lf + %.2lf * %.2lf > %.2lf * %.2lf  \n故三角形ABC為銳角三角形\n",S3,S3,S2,S2,S1,S1);
/*S為三角形ABC的半周長*/
S=(S1+S2+S3)/2;
X=S*(S-a2)*(S-b2)*(S-c2);
printf("三角形ABC面積為 %lf\n",sqrt(X));

d=(x1+x2+x3)/3;
e=(y1+y2+y3)/3;
printf("三角形ABC重心為 G(%.2lf,%.2lf)\n",d,e);

f=((x2*x2-x3*x3)*(y1-y2)-(x1*x1-x2*x2)*(y2-y3)-(y1-y3)*(y1-y2)*(y2-y3))/(2*((x2-x3)*(y1-y2)-(x1-x2)*(y2-y3)));
g=(y2*y2*(x1-x3)-y3*y3*(x1-x2)-y1*y1*(x2-x3)-(x1-x2)*(x1-x3)*(x2-x3))/(2*(x1*(y2-y3)+x3*(y1-y2)+x2*(y3-y1)));
printf("三角形ABC外心為 T(%.2lf,%.2lf)\n",f,g);

l=(c2*x1+b2*x2+a2*x3)/(a2+b2+c2);
m=(c2*y1+b2*y2+a2*y3)/(a2+b2+c2);
printf("三角形ABC內心為 K(%.2lf,%.2lf)\n",l,m);

B2=(x1-f)*(x1-f);
C2=(x2-f)*(x2-f);
D2=(x3-f)*(x3-f);
E2=(y1-g)*(y1-g);
F2=(y2-g)*(y2-g);
G2=(y3-g)*(y3-g);

H2=B2+E2;
I2=C2+F2;
J2=D2+G2;

/*
printf("線段TA長為 %lf\n",sqrt(H2));
printf("線段TB長為 %lf\n",sqrt(I2));
printf("線段TC長為 %lf\n",sqrt(J2));
*/
printf("外接圓半徑為 %lf\n",sqrt(H2));

r=sqrt(X)/S;
printf("內接圓半徑為 %lf\n",r);

h=sqrt(H2);
i=h*h;
j=i*3.1415926;
printf("外接圓面積為 %lf π\n",i);
printf("            =%lf \n",j);

n=r*r;
o=n*3.1415926;
printf("內接圓面積為 %lf π\n",n);
printf("            =%lf \n",o);

aA=(b2*b2+a2*a2-c2*c2)/(2*b2*a2);
printf("cosA=%lf\n",aA);
aB=(c2*c2+a2*a2-b2*b2)/(2*c2*a2);
printf("cosB=%lf\n",aB);
aC=(c2*c2+b2*b2-a2*a2)/(2*c2*b2);
printf("cosC=%lf\n",aC);
printf("arc cos(%lf)=%lf\n",aA,acos(aA));
printf("arc cos(%lf)=%lf\n",aB,acos(aB));
printf("arc cos(%lf)=%lf\n",aC,acos(aC));
p=acos(aA)*(180/3.1415926);
q=acos(aB)*(180/3.1415926);
t=180-p-q;
printf("角A=%11.6lf度\n",p);
printf("角B=%11.6lf度\n",q);
printf("角C=%11.6lf度\n",t);
}

if(S3+S1<=S2 || S3+S2<=S1 || S2+S1<=S3)
{
/*因為兩邊之合小於等於第三邊,兩邊之差大於等於第三邊*/
printf("因為 %.2lf + %.2lf <= %.2lf , %.2lf <= %.2lf - %.2lf\n",S3,S2,S1,S3,S1,S2);
printf("所以 %.2lf, %.2lf, %.2lf 三邊不可成為三角形\n",S3,S2,S1);
}

system("pause");
return 0;
}```

# 排序法4 in 1(Dev-cpp版)

PC test data OS CPU RAM Bubble Selection Insert Quick
HOME 10萬 XP sp3 AMD X2 2.71GHz 3.25GB 105.703 51.078 42.469 25.953
HOME 20萬 XP sp3 AMD X2 2.71GHz 3.25GB 356.782 150.015 117.016 16.234
HOME 30萬 XP sp3 AMD X2 2.71GHz 3.25GB 808.984 302.625 226.344 25.344
```//2010.03.19
//Sort	1.氣泡排序	2.選擇排序	3.插入排序	4.快速排序
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <time.h>
#include <vector>
using namespace std;

void BubbleSort(int data[], int count);
void SelectionSort(int data[], int count);
void InsertSort(int array[], int length);
void QuickSort(int list[], int left, int right);
int main()
{
int b=32767,c,x,k,r,p,sort;
clock_t tstart,tend,tstart2,tend2,tend3,tend4,tend5;
srand(time(NULL));
cout<<"請選擇排序法\n";
cout<<"1=氣泡排序	2=選擇排序	3=插入排序	4=快速排序\n";
cin>>sort;
cout<<"請輸入要比較的資料量\n";
cin>>c;
cout<<"請輸入資料範圍0~";
cin>>b;
tstart = clock();

int a[c];	//只有Dev-cpp能用這種宣告
//正式用法為 vector<int>a(c);

for(p=0;p<c;p++)
{
r=rand()%(b+1);
a[p]=r;
}
tend3 = clock();
for(p=0;p<c;p++)
cout<<a[p]<<"\t";
tend4 = clock();
cout<<"\n ====================================排序前====================================\n";
tstart2 = clock();
switch(sort)
{
case 1:cout<<"\n使用1.氣泡排序\n";
cout<<"\n ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓..排序中..↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓\n\n";
BubbleSort(a,c);//氣泡排序
break;
case 2:cout<<"\n使用2.選擇排序\n";
cout<<"\n ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓..排序中..↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓\n\n";
SelectionSort(a,c);//選擇排序
break;
case 3:cout<<"\n使用3.插入排序\n";
cout<<"\n ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓..排序中..↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓\n\n";
InsertSort(a,c);//插入排序
break;
case 4:cout<<"\n使用4.快速排序\n";
cout<<"\n ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓..排序中..↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓\n\n";
QuickSort(a, 0, c-1);//快速排序
break;
default:cout<<"\n使用預設4.快速排序\n";
cout<<"\n ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓..排序中..↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓\n\n";
QuickSort(a, 0, c-1);//快速排序
}
tend2 = clock();
for(p=0;p<c;p++)
cout<<a[p]<<"\t";
tend5 = clock();
cout<<"\n ====================================排序後====================================\n";

cout <<"\n對"<<c<<"筆  範圍由"<<a[0]<<"~"<<a[c-1]<<"的亂數資料進行";
switch(sort)
{
case 1:cout<<"◎氣泡排序◎\n";
break;
case 2:cout<<"◎選擇排序◎\n";
break;
case 3:cout<<"◎插入排序◎\n";
break;
case 4:cout<<"◎快速排序◎\n";
break;
default:cout<<"◎快速排序(預設)◎\n";
}
cout <<"此程式處理時間：\n";
cout <<"產生亂數用了--------> "<< double(tend3-tstart)/CLOCKS_PER_SEC<<"秒\n";
cout <<"印出亂數用了--------> "<< double(tend4-tend3)/CLOCKS_PER_SEC<<"秒\n";
switch(sort)
{
case 1:cout<<"氣泡排序用了--------> ";
break;
case 2:cout<<"選擇排序用了--------> ";
break;
case 3:cout<<"插入排序用了--------> ";
break;
case 4:cout<<"快速排序用了--------> ";
break;
default:cout<<"快速排序(預設)用了--> ";
}
cout <<double(tend2-tstart2)/CLOCKS_PER_SEC<<"秒\n";
cout <<"印出排序用了--------> "<< double(tend5-tend2)/CLOCKS_PER_SEC<<"秒\n";
tend = clock();
cout <<"執行時共用了--------> "<< double(tend-tstart)/CLOCKS_PER_SEC<<"秒\n";

system("pause");
return 0;
}

void BubbleSort(int data[], int count)
{
int k,p,x;
for(k=0;k<count;k++)
{
for(p=0;p<count;p++)
{
if(data[p]>data[p+1] && p+1<=count-1)
{
x=data[p+1];
data[p+1]=data[p];
data[p]=x;
}
}
}
}

void SelectionSort(int data[], int count)
{
int i, j, min, temp;
for (i = 0; i < count - 1; i++)
{
/* find the minimum */
min = i;
for (j = i+1; j < count; j++)
{
if(data[j] < data[min])
{
min = j;
}
}
/* swap data[i] and data[min] */
temp = data[i];
data[i] = data[min];
data[min] = temp;
}
}

void InsertSort(int array[], int length)
{
int i,j,key;
for (i=1;i<length;i++)
{
key=array[i];
// 把i之前大於array[i]的數據向後移動
for (j = i - 1; j >= 0 && array[j] > key; j--)
{
array[j + 1] = array[j];
}
// 在合適位置安放當前元素
array[j + 1] = key;
}
}

void QuickSort(int list[], int left, int right)
{
int i, j, pivot, temp;

if (left < right){
i = left;
j = right + 1;
pivot = list[left];
do
{
do
i++;
while (list[i] < pivot);
do
j--;
while (list[j] > pivot);
/* 若i小於j，交換i與j所指的資料 */
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
while (i < j);
/* j大於i，將list[left] 與索引j所指向的資料交換 */
temp = list[left];
list[left] = list[j];
list[j] = temp;
QuickSort(list, left, j - 1);// 排序左邊
QuickSort(list, j + 1, right);// 排序右邊
}
}```

# C 存讀檔練習

```10
15 25
36 46
57 67
78 88
99 109
123 369
789 147
456 789
555 666
999 999```

## a.cpp

```//2010.08.01
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x,a[x],b[x],i,j,k;	//int a,b,c;
long int c;
FILE * fin=NULL;
FILE * fout=NULL;
fin=fopen("pa.in","r");
fout=fopen("out.txt","w+");
printf("輸入幾個計算\n");
fscanf(fin,"%d", &x);
//scanf("%d",&x);
for(k=0;k<x;k++)
{
printf("輸入a,b  印出a*b乘法表\n");
fscanf(fin,"%d%d",&a[k],&b[k]);
//scanf("%d%d",&a[k],&b[k]);
printf("第%d次輸入  %d*%d乘法表\n\n",k+1,a[k],b[k]);
fprintf(fout , "第%d次輸入  %d*%d乘法表\n\n",k+1,a[k],b[k]);
for(i=1;i<=a[k];i++)
{
for(j=1;j<=b[k];j++)
{
c=i*j;
printf("%d*%d=%2ld\t",i,j,c);
fprintf(fout , "%d*%d=%2ld\t",i,j,c);
}
printf(" \n\n");
}
}

system("pause");
return 0;
}```

# STL應用(vector,sort,for_each)

```//2010.08.26	STL應用 vector,sort,for_each
#include <iostream>
#include <vector>
using namespace std;
vector<int>array2;
int function(int&a)
{
a*=a;
cout<<a<<"  ";
}
int main()
{
int i,n,m,begin;
cout<<"比較幾筆亂數?"<<endl;
cin>>n;
//use vector
vector<int>array(n);
array2.resize(n); //給array2大小
//for(i=0;i<n;i++) 與下方擇一使用
for(i=0;i<array.size();i++)
{
array[i]=i*10%8;
array2[i]=array[i];
cout<<array[i]<<"  ";
}
cout<<endl;
cout<<"使用STL的sort\n";
//use sort
sort(array.begin(),array.end());
for(i=0;i<n;i++)
cout<<array[i]<<"  ";
for(i=0;i<n;i++)
array[i]=i*10%8;
cout<<"\n比較中間的m筆資料\n";
cout<<"給定起始值\n";
cin>>begin;
cout<<"給定m值\n";
cin>>m;
sort(array.begin()+begin,array.end()-(n-m-begin));
for(i=0;i<n;i++)
cout<<array[i]<<"  ";
cout<<endl;
//use for_each
cout<<"使用for_each做平方(array)\n";
for_each(array.begin(),array.end(),function);
cout<<"\n使用for_each做平方(array2)\n";
for_each(array2.begin(),array2.end(),function);
cout<<endl;
system("pause");
return 0;
}```

# 99 NCPC Problem B

## pb.in

```4
3
-4.1 1.2 2.8
1.9 3.1 -1.1
3.0 -2.7 5.8
4
5.0 1.0 -2.0 -1.9
-2.9 7.0 -2.0 -2.0
1.1 1.2 4.0 1.5
2.2 3.3 -4.4 5.5
5
5.0 4.0 0.9 0.0 0.0
0.0 7.0 0.9 2.1 3.9
1.0 4.0 9.1 3.0 1.0
0.3 1.0 9.2 19.9 -9.3
3.1 4.2 5.3 2.4 15.1
8
8.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
-1.0 8.0 1.0 1.0 1.0 1.0 1.0 1.0
-1.0 -1.0 8.0 1.0 1.0 1.0 1.0 1.0
-1.0 -1.0 -1.0 8.0 1.0 1.0 1.0 1.0
-1.0 -1.0 -1.0 -1.0 8.0 1.0 1.0 1.0
-1.0 -1.0 -1.0 -1.0 -1.0 8.0 1.0 1.0
-1.0 -1.0 -1.0 -1.0 -1.0 -1.0 8.0 1.0
-1.0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0 8.0
```

## b.cpp

```//2010.11.19
#include <stdio.h>
#include <stdlib.h>
double array[10000][10000];
int main()
{
int n,m,i,j;
FILE * fin=fopen("pb.in","r");
fscanf(fin,"%d",&n);
//printf("n=%d\n",n);
while(n--)
{
double sum;
int test=1;
fscanf(fin,"%d",&m);
//printf("m=%d\n",m);
for(i=0;i<m;i++)
for(j=0;j<m;j++)
{
fscanf(fin,"%lf",&array[i][j]);
if(array[i][j]<0)
array[i][j]*=-1;
}
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<m;j++)
{
if(i!=j)
sum+=array[i][j];
//printf("%.1lf ",array[i][j]);
}
//printf("sum=%lf   a=%lf\n",sum,array[i][i]);
if(sum>array[i][i])
test=0;
}
if(test)
printf("Y");
else
printf("N");

}
printf("\n");
//system("pause");
return 0;
}```