蛇形数组打印(两种形式)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                蛇形数组打印(两种形式)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                #蛇形數組打印
 ##第一種形式
- 形式1
 
- 形式2
 
- 形式3
 
- 形式4
 
完整代碼
#include<iostream> #include<stdlib.h> #include <iomanip>using namespace std; /*打印函數*/ void my_printf(int arr[100][100],int num) {int i, j;for (i = 0; i < num; i++){for (j = 0; j < num; j++){cout << setw(5) << arr[i][j];}cout << endl;} }int main() {int arr[100][100] = { 0 };int i, j;int x, y;int num = 0;int count = 1;/*1的起始位置*/arr[x = 4][y = 0] = 1;cin >> num;//打印的列數/*判斷起始位置的下一個數組是0&&沒有越界。就置1*/for (i = 0; i < num*num; i++){while (arr[x][y+1] == 0 && y + 1 < num)arr[x][++y] = ++count;while (arr[x+1][y] == 0 && x + 1< num)arr[++x][y] = ++count;while (arr[x][y-1] == 0 && y - 1 >= 0)arr[x][--y] = ++count;while (arr[x-1][y] == 0 && x - 1>= 0)arr[--x][y] = ++count;}my_printf(arr,num);system("pause"); }##第二種形式
5 1 3 6 10 15 2 5 9 14 4 8 13 7 12 11完整代碼
#include<iostream>using namespace std;int main() {int N = 0;while (cin >> N) {int i, k;int x = 1;int count = 1;for (i = 1; i <= N; i++){count = x;cout << count;for (k = i + 1; k <= N; k++){count = count + k;//首地址值cout << ' ';cout << count;}x += i;//1 2 4 7 11 計算每行第一個元素值cout << endl;}} }總結
以上是生活随笔為你收集整理的蛇形数组打印(两种形式)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 请问格兰迪斯在哪?
 - 下一篇: Linux网站大杂烩《自己查阅》