Bloomberg 05112012

来源:互联网 发布:淘宝宝贝没有展现词 编辑:程序博客网 时间:2024/05/20 01:09
/*Optimized way to find an element in a matrix where every row and column is sorted.*/#include <iostream>int main(){int a[4][4] = {{1, 5, 6, 7},{2, 6, 7, 8},{3, 7, 8, 9},{4, 8, 9, 10}};int target;printf("Please input the number: ");scanf("%d", &target);int i = 3, j = 0;while(i >= 0 && j <= 3){if(a[i][j] < target){j++;}else if(a[i][j] > target){i--;}else{printf("Location:  x - %d, y - %d\n", i, j);break;}}printf("Cannot find this number\n");return 0;}