旋轉數組的最小數字,旋轉數組數字

來源:互聯網
上載者:User

旋轉數組的最小數字,旋轉數組數字


把一個數組最開始的若干個元素搬到數組的末尾,我們稱之為數組的旋轉。輸入一個遞增排序的數組的一個旋轉,輸出旋轉數組的最小元素。例如數組{3,4,5,1,2}為{1,2,3,4,5}的一個旋轉,該數組的最小值為1。

輸入:

輸入可能包含多個測試範例,對於每個測試案例,

輸入的第一行為一個整數n(1<= n<=1000000):代表旋轉數組的元素個數。

輸入的第二行包括n個整數,其中每個整數a的範圍是(1<=a<=10000000)。

輸出:

對應每個測試案例,

輸出旋轉數組中最小的元素。

範例輸入:

5

3 4 5 1 2

範例輸出:

1

___________________

#include <stdio.h>#include <stdlib.h>int main(int argc, const char * argv[]) {    // insert code here...    int i, length, *num, cur;    cur = 0;    scanf("%d", &length);    num = (int *)malloc(sizeof(int) * length);    i = length;    while (i > 0) {        scanf("%d", &num[i - 1]);        if (num[cur] <= num[i - 1]) {            cur = i - 1;        }        i--;    };    printf("%d\n", num[cur]);    return 0;}




怎找出一個數組中的兩個最小的數

void Test(int n,char c){//接受數組c和數組長度n
int min=c[0]//定義最小(其類型我用INT,需要根據實際寫)
int min2=c[0]//定義第二最小(其類型我用INT,需要根據實際寫)
for(int i=0;i<=n-1;i++){
if(min>=c[i])min=c[i];
}//獲得最小
for(int i=0;i<=n-1;i++){
if(min2>=c[i]&&min2!=min)min2=c[i];
}//獲得第二小

//return min //想返回最小值,需要把void Test,改為min的數字類型
//return min2 //想返回第二小值,需要把void Test,改為min2的數字類型
}

另外排序後再選擇方法也不錯
 
vc++二維數組元素旋轉90°

#include <iostream.h>
#define M 4
class CIRCUM{
int a[M][M];
public:
CIRCUM(int x[M][M])
{
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
a[i][j]=x[i][j];
}
void circumrotate()
{
int b[M][M];
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
b[M-1-j][i]=a[i][j];
for(int k=0;k<M;k++)
for(int l=0;l<M;l++)
a[k][l]=b[k][l];
}
void show()
{
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
if(a[i][j]<10)
cout<<" "<<a[i][j]<<" ";
else cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
};
void main()
{
int data[M][M]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
CIRCUM arr(data);
cout<<"原二維數組為: "<<endl;
arr.show();
arr.circumrotate();
cout<<"旋轉後二維數組為: "<<endl;
arr.show();
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.