C++依照正態散布來分列整型數組元素。本站提示廣大學習愛好者:(C++依照正態散布來分列整型數組元素)文章只能為提供參考,不一定能成為您想要的結果。以下是C++依照正態散布來分列整型數組元素正文
標題請求以下:
給定一個數組input[],
假如數組長度n為奇數,則將數組中最年夜的元素放到output[]數組最中央的地位,
假如數組長度n為偶數,則將數組中最年夜的元素放到 output[] 數組中央兩個地位偏右的誰人地位上,
然後再按從年夜到小的次序,順次在第一個地位的雙方,依照一左一右的次序,順次寄存剩下的數。
這類處置後成果,假如依照元素的值表現一種散布的圖形的話,那繪制後的圖形應當是正態散布。
關於正態散布:
正態散布(Normal distribution)別名高斯散布(Gaussian distribution),是一個在數學、物理及工程等范疇都異常主要的幾率散布,在統計學的很多方面有側重年夜的影響力。若隨機變量X屈服一個數學希冀為μ、方差為σ^2的高斯散布,記為N(μ,σ^2)。其幾率密度函數為正態散布的希冀值μ決議了其地位,其尺度差σ決議了散布的幅度。因其曲線呈鐘形,是以人們又常常稱之為鐘形曲線。我們平日所說的尺度正態散布是μ= 0,σ = 1的正態散布。
這裡只是從成果上發生接洽,算法與正態散布有關。
代碼完成以下:
void sort(int input[],int output[], int n)
{
int m=n;
//cout<<m<<endl;
int i,j,temp;
bool exchange;//交流標記
for(i=0;i<m-1;i++)
{ //最多做n-1趟排序
exchange=FALSE; //本趟排序開端前,交流標記應為假
for(j=m-2;j>=i;j--) //對以後無序區R[i..n]自下向上掃描
if(input[j+1]<input[j])
{//交流記載
temp=input[j+1]; //R[0]不是尖兵,僅做暫存單位
input[j+1]=input[j];
input[j]=temp;
exchange=TRUE; //產生了交流,故將交流標記置為真
}
if(!exchange) //本趟排序未產生交流,提早終止算法
break;
//cout<<input[5]<<endl;
}
for(int wc1=0; wc1<m; wc1++)//只是來顯示排序成果~
{
cout<<input[wc1]<<" ";
}
cout << endl;
int q=m-1;
if((m%2)==0)
{
int mid=m/2;
for (int tempmid=0; tempmid<=mid; tempmid++)//留意輪回語句的履行次序
{
output[mid+tempmid]=input[q];
q--;
output[mid-tempmid-1]=input[q];
q--;
}
}
if((m%2)!=0)//留意輪回語句的履行次序
{
int mid=q/2;
output[mid]=input[q];
for (int tempmid=1;tempmid<=mid;tempmid++)
{
q--;
output[mid-tempmid]=input[q];
q--;
output[mid+tempmid]=input[q];
}
}
for(int wc=0; wc<m; wc++)
{
cout<<output[wc]<<" ";
}
cout << endl;
}
int main()
{
int input[] = {3, 6, 1, 9, 7, 8, 2};
int wc=0;
int nCount = sizeof(input)/sizeof(int);
for(wc=0; wc<nCount; wc++)//
{
cout<<input[wc] << " ";
//cout<<"\n"<<endl;
}
cout << endl;
int output[]= {3, 6, 1, 9, 7, 8, 2};
sort(input,output, nCount);
return 0;
}
測試成果:
當int input[] = {3, 6, 1, 9,7, 8, 2, 10};,成果以下:
3 6 1 9 7 8 2 10
1 2 3 6 7 8 9 10
1 3 7 9 10 8 6 2
當int input[] = {3, 6, 1, 9,7, 8, 2, 10};,成果以下:
3 6 1 9 7 8 2
1 2 3 6 7 8 9
2 6 8 9 7 3