找最小数

来源:互联网 发布:百度云盘vip破解版mac 编辑:程序博客网 时间:2024/05/16 18:02
题目描述:

第一行输入一个数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****************************************************************/



0 0
原创粉丝点击