标签:sum fine getch getchar size int get info 矩阵
13、一个8*8的矩阵内存储1-9的数字,找到一个4*4的方阵使其和最小。
思路:穷举法
1 #include <stdio.h> 2 3 #define uchar unsigned char 4 5 int main(void) 6 { 7 uchar square[8][8] = { 8 { 1, 4, 6, 8, 4, 9, 0, 7 }, 9 { 2, 6, 3, 7, 2, 1, 8, 7 }, 10 { 1, 6, 9, 8, 5, 3, 2, 8 }, 11 { 4, 5, 7, 2, 1, 2, 8, 3 }, 12 { 1, 7, 9, 4, 3, 7, 3, 9 }, 13 { 8, 9, 1, 3, 4, 5, 2, 7 }, 14 { 6, 8, 4, 3, 2, 9, 7, 3 }, 15 { 1, 3, 2, 8, 5, 5, 3, 4 } 16 }; 17 18 uchar sum = 0; 19 uchar min = 144; 20 uchar i, j, k, l, mini, minj; 21 22 for (i = 0; i < 5; i++) 23 for (j = 0; j < 5; j++) 24 { 25 for (k = i; k < i + 4; k++) 26 for (l = j; l < j + 4; l++) 27 { 28 sum += square[k][l]; 29 } 30 if (sum < min) 31 { 32 min = sum;//record minimal value 33 mini = i; //record location x,y 34 minj = j; 35 } 36 sum = 0; 37 } 38 printf("the minimal square4x4 is %d, location x = %d, y = %d.\n", min, mini, minj); 39 getchar(); 40 41 return 0; 42 }
结果如下:
14、某人设计了一个长正整数减法的计算程序,两个加数分别用数组Va和Vb表示,和用Vc表示。数据采用个位在最右侧的对齐方式(即数组最大下标对应个位),最多可以计算300位十进制数的减法。假设
标签:sum fine getch getchar size int get info 矩阵
原文地址:https://www.cnblogs.com/yangzx/p/11141109.html