題目描述:
第一行輸入一個數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****************************************************************/