The most stupid way to solve simple questions about thieves

Source: Internet
Author: User

The most stupid way to solve simple questions about thieves
Question: enter a 5x5 matrix, move the largest element to the center, and place the four corners with four smallest elements (from left to right, from top to bottom, and store them in ascending order)

Idea: the maximum value is the best to find. It is traversed once in a loop to find the maximum value and its address. Then we can find the minimum four digits. My idea is to use an array to store the first row of the Two-dimensional array, and then traverse the second row, compare the largest element in array B. Replace the largest element in array B with the smallest element in array B. Then, find the addresses of these four numbers. At first, I didn't add the flag, so that once two identical numbers exist in the two-dimensional array, the program crashes. After realizing this, I set a corresponding flag1 and flag2 for each number. Once one is found, the flag is set to 1 at the same time, it indicates that this number has already been obtained. Finally, output.

1 # include <stdio. h> 2 int main () {3 void sort (int q []); 4 int a [5] [5]; 5 int B [5]; 6 int max = 0, temp; 7 int flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0; 8 int * p; 9 int * p1, * p2, * p3, * p4; 10 for (int I = 0; I <5; I ++) 11 for (int j = 0; j <5; j ++) 12 scanf ("% d", & a [I] [j]); 13 for (int I = 0; I <5; I ++) 14 B [I] = a [0] [I]; 15 16 for (int I = 0; I <5; I ++) {17 for (int j = 0; j <5; j ++) {18 if (a [I] [j]> max) {max = a [I] [j]; p = & a [I] [j];} 19} 20} 21 for (int I = 1; I <5; I ++) {22 for (int j = 0; j <5; j ++) {23 for (int k = 4; k> = 0; k --) {24 if (a [I] [j] <B [k]) {25 B [k] = a [I] [j]; 26 sort (B); break; 27} 28} 29} 30} 31 for (int I = 0; I <5; I ++) {32 for (int j = 0; j <5; j ++) {33 if (a [I] [j] = B [0] & flag1 = 0) {p1 = & a [I] [j]; flag1 = 1;} 34 else if (a [I] [j] = B [1] & flag2 = 0) {p2 = & a [I] [j]; flag2 = 1 ;} 35 else if (a [I] [j] = B [2] & flag3 = 0) {p3 = & a [I] [j]; flag3 = 1;} 36 else if (a [I] [j] = B [3] & flag4 = 0) {p4 = & a [I] [j]; flag4 = 1 ;}37} 38} 39 * p = a [2] [2]; a [2] [2] = max; 40 * p1 = a [0] [0]; a [0] [0] = B [0]; 41 * p2 = a [0] [4]; a [0] [4] = B [1]; 42 * p3 = a [4] [0]; a [4] [0] = B [2]; 43 * p4 = a [4] [4]; a [4] [4] = B [3]; 44 printf ("\ n"); 45 for (int I = 0; I <5; I ++) {46 for (int j = 0; j <5; j ++) {47 printf ("% d", a [I] [j]); 48} 49 printf ("\ n "); 50} 51} 52 // sort 53 void sort (int q []) {54 int temp1; 55 for (int I = 0; I <5; I ++) {56 for (int j = I + 1; j <5; j ++) {57 if (q [I]> q [j]) {58 temp1 = q [I]; q [I] = q [j]; q [j] = temp1; 59} 60} 61} 62}

Running result:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.