初次發代碼,哈哈。
- // Console_test.cpp : Defines the entry point for the console application.
- /*本程序主要是輸出從1到9個環的“回”字圖形
- 需求按順序輸出從1到9個環的“回”字圖形,參見輸出。
- 輸出:
- 1
- -------
- 222
- 212
- 222
- -------
- 33333
- 32223
- 32123
- 32223
- 33333
- -------
- 4444444
- 4333334
- 4322234
- 4321234
- 4322234
- 4333334
- 4444444
- -------
- 555555555
- 544444445
- 543333345
- 543222345
- 543212345
- 543222345
- 543333345
- 544444445
- 555555555
- -------
- 66666666666
- 65555555556
- 65444444456
- 65433333456
- 65432223456
- 65432123456
- 65432223456
- 65433333456
- 65444444456
- 65555555556
- 66666666666
- -------
- 7777777777777
- 7666666666667
- 7655555555567
- 7654444444567
- 7654333334567
- 7654322234567
- 7654321234567
- 7654322234567
- 7654333334567
- 7654444444567
- 7655555555567
- 7666666666667
- 7777777777777
- -------
- 888888888888888
- 877777777777778
- 876666666666678
- 876555555555678
- 876544444445678
- 876543333345678
- 876543222345678
- 876543212345678
- 876543222345678
- 876543333345678
- 876544444445678
- 876555555555678
- 876666666666678
- 877777777777778
- 888888888888888
- -------
- 99999999999999999
- 98888888888888889
- 98777777777777789
- 98766666666666789
- 98765555555556789
- 98765444444456789
- 98765433333456789
- 98765432223456789
- 98765432123456789
- 98765432223456789
- 98765433333456789
- 98765444444456789
- 98765555555556789
- 98766666666666789
- 98777777777777789
- 98888888888888889
- 99999999999999999
- -------
- */
- #include <cstring>
- #include <iostream>
- using namespace std;
- //#include <string>//在程序中使用串需要有串定義,否則可能莫名其妙出錯。
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // The one and only application object
- //
- void main()
- {
- void hui(int a);//聲明一個無須返回值的函數。
- hui(9);//打印回字型9的圖形。
- }
- void hui (int a)
- {
- int c,m,n;//d,//m爲行數,n爲列數,輸出爲M*N回字矩陣,c爲行輸出基準數,d爲列輸出基準數。
- c=a;//d=a;d不使用。
- for(m=1;m<=2*a-1;m++)//行計數//如果寫成m<2a-1就會出錯,因爲電腦不認識2a,只認識2*a
- {
- for(n=1;n<=2*a-1;n++)//列計數
- {
- if(n<a-c+1)
- {
- cout<<a-n+1<<" ";
- }
- else
- {
- if (n>a+c-1)//if(n>a-c+1 && n>9)
- {
- cout<<n+1-a<<" ";
- }
- else
- {
- cout<<c<<" ";
- }
- }
- }
- cout<<" 行輸出基準數c爲:"<<c<<endl;//作爲數值大小的觀察點
- if(m<a)
- {
- c--;
- }
- else
- {
- c++;
- }
- }
- }