ÂÝÐý¾ØÕó ¶ÔÒ»¸öÈçÏÂËùʾµÄ¾ØÕó ¹æÂÉÊÇ´ÓÊ××ø±ê¿ªÊ¼ÒÀ´ÎÂÝÐýÔö´ó ºÜ¶àÃæÊÔÌⶼϲ»¶ÎÊÕâÑùµÄÎÊÌ⣬½â¾öµÄ·½·¨ºÜ¼òµ¥£¬°Ñ¸´ÔÓµÄÎÊÌâ·Ö½â³É¼òµ¥µÄÎÊÌâ¡£ ¢Ù°ÑÒ»¸ö¾ØÕó·Ö²ã£¬Ò»²ãÒ»²ãµÄ½â¾ö£» ¢ÚÔÙ·Ö²ãµÄ»ù´¡ÉÏ£¬ÔÙ½«Ò»²ã·Ö³ÉÉÏÏÂ×óÓÒËĸö²¿·Öµ¥¶À½â¾ö£» ֻҪעÒâÒ»ÏÂÊý×éµÄ±ß½ç¼´¿ÉÑ»·Íê³É£¬ÓÐÒ»µãÐèҪעÒâµ±Êý×éµÄ´óСΪÆæÊý»òÊÇżÊýʱÊÇÓÐÉÔÐí²»Í¬µÄÐèҪעÒâһϡ£ #include <iostream>#include <iomanip>int main(){ using namespace std; cout << "Enter the size of matrix: "; int n,m=1; cin >> n; //ÊäÈë¾ØÕóµÄ´óС int **arr = new int*[n]; //¶¯Ì¬·ÖÅä¶þά¾ØÕó if (NULL==arr) //¼ì²â·ÖÅäÊÇ·ñ³É¹¦ { exit(1); } for (int i = 0; i < n; i++) { arr = new int[n]; if (NULL == arr) { exit(1); } } for (int i = 0; i < ((n % 2 != 0) ? (n - 1) / 2:n/2); i++)//¶ÔÆæż²»Í¬µÄÊý×é·Ö±ð¼ÆËã²ãÊý { for (int j = i; j < n-i-1; j++) //´¦ÀíÉÏÃæ { arr[j] = m++; } for (int j = i; j <n-i-1; j++) //´¦ÀíÓÒÃæ { arr[j][n - i - 1] = m++; } for (int j = n-i-1; j > i; j--) //´¦ÀíÏÂÃæ { arr[n - i - 1][j] = m++; } for (int j = n-i-1; j>i; j--) //´¦Àí×óÃæ { arr[j] = m++; } } if (n%2!=0) //ÈôÊÇÆæÊý£¬²¹³öÖÐÐĵã { arr[n / 2][n / 2] = m; } for (int i = 0; i < n; i++) //Ñ»·´òÓ¡ { for (int j = 0; j <n; j++) { cout <<left<<setw(4)<< arr[j]; } cout << endl; } for (int i = 0; i < n; i++) //ÊͷŶþάÊý×é { delete[] arr; } delete[] arr; cin.get(); cin.get(); return 0;}1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
ÊäÈëÒ»¸ö¾ØÕ󣬰´ÕÕ´ÓÍâÏòÀïÒÔ˳ʱÕëµÄ˳ÐòÒÀ´Î´òÓ¡³öÿһ¸öÊý×Ö£¬ÀýÈ磬Èç¹ûÊäÈëÈçϾØÕó£º 1 2 3 4 5 6 7 8 9 10 11 12 ÔòÒÀ´Î´òÓ¡³öÊý×Ö1,2,3,4,8,12£¬11,10,9,5,6,7 Èô¹ûÒª´¦ÀíµÄ²»ÊÇÒ»¸öÒ»Ö±Õý·½ÐεĶøÊÇÒ»¸ö³¤·½ÐεĻ°ÉÏÃæµÄ·½·¨¾Í±È½ÏÄÑ´¦ÀíÁË£¬Òò´ËÖ»ÐèÒªÉÔ×÷Ð޸ļ´¿É vector<int> printMatrix(vector<vector<int> > matrix) { int row = matrix.size(); int col = matrix[0].size(); vector<int> res; // ÊäÈëµÄ¶þάÊý×é·Ç·¨£¬·µ»Ø¿ÕµÄÊý×é if (row == 0 || col == 0) return res; // ¶¨ÒåËĸö¹Ø¼ü±äÁ¿£¬±íʾ×óÉϺÍÓÒϵĴòÓ¡·¶Î§ int left = 0, top = 0, right = col - 1, bottom = row - 1; while (left <= right && top <= bottom) { // left to right for (int i = left; i <= right; ++i) res.push_back(matrix[top]); // top to bottom for (int i = top + 1; i <= bottom; ++i) res.push_back(matrix[right]); // right to left if (top != bottom) for (int i = right - 1; i >= left; --i) res.push_back(matrix[bottom]); // bottom to top if (left != right) for (int i = bottom - 1; i > top; --i) res.push_back(matrix[left]); left++,top++,right--,bottom--; } return res; }1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
ÔÙÀ´¿´Ò»¸öÉñÆæµÄÊý×ézigzag ÔÚJPEGͼÏñ´¦ÀíËã·¨ÖÐÊ×ÏȶÔͼÏñ½øÐзֿ飬»áʹÓõ½Õâ¸ö¡°Ö®¡±×Ö¾ØÕó£¬Ëü³¤³ÉÏÂÃæÕâ¸öÑù×Ó
Ëü¾ÍÏñÒ»¸öÉßÒ»Ñù£¬Ò»Ö±ÔÚÑØ×ŶԽÇÏßÈÆ£¬Ò»ÏÂÔö¼Ó£¬Ò»Ï¼õСºÜÄÑʵÏÖ°¡£¬»¹ÊÇÒ»²¿·ÖÒ»²¿·ÖÀ´·ÖÎö¡£ ÎÒÃÇÏȵ¥¶ÀÀ´¿´¿´×óÉϲ¿·Ö ÕâÀïÎÒÃÇҪעÒâµ½Ò»¼þÊÂÇ飬zigzagÊý×éµÄ»ù±¾±ä»»µ¥Î»ºÍÆÕͨ¾ØÕó²»Ò»Ñù£¬²»ÔÚÊÇ°´ÐлòÊÇÁб仯ÁË£¬Ò²¾ÍÊÇ˵ÐÐÊýºÍÁÐÊý¶¼ÊDZ任µÄ£¬µ«ÊÇÔÚÒ»ÌõбÏßÉÏÓиö¹Ø¼üµÄÐÔÖÊ£¬ºá×Ý×ø±êÖ®ºÍÊÇÏàµÈµÄ£¬Õ⽫³ÉΪ¸ã¶¨Õâ¸özigzagµÄÍ»ÆƵ㡣 ½«Ã¿Ð±¿ªÊ¼Ôö³¤µÄµÚÒ»¸öµã½Ð×öÌø±äµã£¨¿ÉÄÜÔÚµÚÒ»ÐÐÒ²¿ÉÄÜÔÚµÚÒ»ÁУ¬ÀýÈç1¡¢3¡¢6£©£¬ÄÇô¾Í»á·¢ÏÖÌø±äµãµÄÖµµÈÓÚ£¬Ôڸõã֮ǰµÄÔªËصĸöÊý£¬±ÈÈçÉÏͼÖÐ6Õâ¸öÌø±äµã£¬Ëü¾ÍµÈÓÚÔÚËü֮ǰµÄÔªËصĸöÊý6£¬¶ø6¶ÔÓ¦µÄ×ø±êΪ£¨0,3£©¶ÔÓÚ×ø±êºÍS=i+j=3£¬¶øËü֮ǰµÄÔªËØ sum=1+2+3=S∗(S+1)/2 ÎÒÃǽâ¾öÁËÒ»¸öбÐеijõʼֵµÄÎÊÌ⣬½ÓÏÂÀ´¾ÍÊÇҪȷ¶¨Ò»¸öбÐÐÄÚµÄÔªËØÊÇ´Ó×óϵ½ÓÒÉϵĻ¹ÊÇ´ÓÓÒÉϵ½×óϵģ¬ÆäʵºÜºÃ·Ö±ð£¬ÒòΪÊǽ»Ìæ³öÏÖ£¬ÀûÓÃSµÄÆæżÐÔ¼´¿ÉÅжϣº ¢ÙÈôSΪÆæÊý£¬ÔòÊÇ´ÓÓÒÉÏÖÁ×óϵģ» ¢ÚÈôSΪżÊý£¬ÔòÊÇ´Ó×óÏÂÖÁÓÒÉϵģ» ×îºóÈ·¶¨×¼È·µÄÊýÖµ ÎÒÃÇÒÔ£¨1,2£©µãΪÀý£¬ÎÒÃÇÒѾ֪µÀËüËùÔÚбµÄ³õʼֵΪ6£¬²¢ÊÇ°´ÕÕ´ÓÓÒÉϵ½×óϵÝÔöµÄ£¬ÄÇôÎÒÃÇÖ»ÐèÒªÖªµÀËüµÄ×Ý×ø±êÊǼ¸¾ÍÊÇÔö¼ÓÁ˼¸£¬Òò´ËÔÚ×óÉϲ¿·ÖÖµÓë×ø±êµÄ¶ÔÓ¦¹Øϵ¿ÉÒÔд×÷ arr[j] = s*(s + 1) / 2 + (((i + j) % 2 == 0) ? j: i);½ÓÏÂÀ´¿´¿´ÓÒϽÇ
ÒòΪ½á¹¹±äÁË Ìøתµã£¨13£©¾ÍÎÞ·¨Í¨¹ý sum=1+2+3=S∗(S+1)/2 ¼ÆËãÁË,Òò´ËÎÒÃDz»ÕÒÌøתµãÁË·´¹ýÀ´ÕÒ½áÊøµã£¨12,14£©ÀûÓÃÕâ¸öµã·´¹ýÀ´Ëã¸÷µãµÄÖµ¡£ ÕâÀï¿ÉÒÔ½«×îºóÒ»¸öµãµ±³É£¨0,0£©µã£¬»á±È½ÏºÃÀí½â s = (n - 1 - i) + (n - 1 - j);arr[j] = squa - s*(s + 1) / 2 - (n - (((i + j) % 2 == 0) ? j : i));ÕâÑù¾ÍÍê³ÉÁË #include <iostream>#include <iomanip>int main(){ using namespace std; int n; int s, i, j; int squa; cout << "Enter the size of matrix: "; cin >> n; int **arr = new int*[n]; ////¶¯Ì¬·ÖÅä¶þά¾ØÕó if (NULL==arr) { exit(1); } for (int i = 0; i < n; i++) { arr = new int[n]; if (NULL==arr) { exit(1); } } squa = n*n; for (i = 0; i < n; i++) { for (j = 0; j < n;j++) { s = i + j; //È·¶¨Êǵڼ¸Ð±ÐÐ if (s<n) //×óÉϲ¿·Ö { arr[j] = s*(s + 1) / 2 + (((i + j) % 2 == 0) ? j: i); } else //ÓÒϲ¿·Ö { s = (n - 1 - i) + (n - 1 - j); arr[j] = squa - s*(s + 1) / 2 - (n - (((i + j) % 2 == 0) ? j : i)); } } } // for (int i = 0; i < n; i++) //Ñ»·´òÓ¡ { for (int j = 0; j < n; j++) { cout << left << setw(6) << arr[j]; } cout << endl; } for (int i = 0; i < n; i++) //ÊͷŶþάÊý×é { delete[] arr; } delete[] arr; cin.sync(); cin.get(); return0;} |