找最小數

來源:互聯網
上載者:User
題目描述:

第一行輸入一個數n,1 <= n <= 1000,下面輸入n行資料,每一行有兩個數,分別是x y。輸出一組x y,該組資料是所有資料中x最小,且在x相等的情況下y最小的。  輸入:

輸入有多組資料。
每組輸入n,然後輸入n個整數對。 輸出:

輸出最小的整數對。

範例輸入:

5  3 3  2 2  5 5  2 1  3 6
範例輸出:
2 1
來源: 2010年北京郵電大學電腦研究生機試真題 AC代碼:

記憶體:920Kb, 耗時:10ms

#include<stdio.h> int n, arr[1005][2]; int main() {    int i;    while(scanf("%d", &n) != EOF) {        for(i = 0; i < n; i++) {            scanf("%d %d", &arr[i][0], &arr[i][1]);        }        int min_index = 0;        for(i = 0; i < n; i++) {            if(arr[i][0] < arr[min_index][0]) {                min_index = i;            }            else if(arr[i][0] == arr[min_index][0] && arr[i][1] < arr[min_index][1]) {                min_index = i;            }        }        printf("%d %d\n", arr[min_index][0], arr[min_index][1]);    }     return 0;} /**************************************************************    Problem: 1170    User: wusuopuBUPT    Language: C    Result: Accepted    Time:10 ms    Memory:920 kb****************************************************************/


更好的方法:

記憶體:912kb, 耗時0ms

#include<stdio.h>int main() { int n, x, y, tempx, tempy; while(scanf("%d", &n) != EOF) {       scanf("%d", &x);       scanf("%d", &y);       while(-- n) {             scanf("%d", &tempx);             scanf("%d", &tempy);             if(tempx < x || tempx == x && tempy < y) {x = tempx; y = tempy;}        }        printf("%d %d\n", x, y); } return 0;}/**************************************************************    Problem: 1170    User: kaoyanasi    Language: C    Result: Accepted    Time:0 ms    Memory:912 kb****************************************************************/



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.