今天很高興,解決了回字型數字輸出問題。祥見下文

初次發代碼,哈哈。

  1. // Console_test.cpp : Defines the entry point for the console application.
  2. /*本程序主要是輸出從1到9個環的“回”字圖形
  3. 需求按順序輸出從1到9個環的“回”字圖形,參見輸出。
  4. 輸出:
  5. ------- 
  6. 222 
  7. 212 
  8. 222 
  9. ------- 
  10. 33333 
  11. 32223 
  12. 32123 
  13. 32223 
  14. 33333 
  15. ------- 
  16. 4444444 
  17. 4333334 
  18. 4322234 
  19. 4321234 
  20. 4322234 
  21. 4333334 
  22. 4444444 
  23. ------- 
  24. 555555555 
  25. 544444445 
  26. 543333345 
  27. 543222345 
  28. 543212345 
  29. 543222345 
  30. 543333345 
  31. 544444445 
  32. 555555555 
  33. ------- 
  34. 66666666666 
  35. 65555555556 
  36. 65444444456 
  37. 65433333456 
  38. 65432223456 
  39. 65432123456 
  40. 65432223456 
  41. 65433333456 
  42. 65444444456 
  43. 65555555556 
  44. 66666666666 
  45. ------- 
  46. 7777777777777 
  47. 7666666666667 
  48. 7655555555567 
  49. 7654444444567 
  50. 7654333334567 
  51. 7654322234567 
  52. 7654321234567 
  53. 7654322234567 
  54. 7654333334567 
  55. 7654444444567 
  56. 7655555555567 
  57. 7666666666667 
  58. 7777777777777 
  59. ------- 
  60. 888888888888888 
  61. 877777777777778 
  62. 876666666666678 
  63. 876555555555678 
  64. 876544444445678 
  65. 876543333345678 
  66. 876543222345678 
  67. 876543212345678 
  68. 876543222345678 
  69. 876543333345678 
  70. 876544444445678 
  71. 876555555555678 
  72. 876666666666678 
  73. 877777777777778 
  74. 888888888888888 
  75. ------- 
  76. 99999999999999999 
  77. 98888888888888889 
  78. 98777777777777789 
  79. 98766666666666789 
  80. 98765555555556789 
  81. 98765444444456789 
  82. 98765433333456789 
  83. 98765432223456789 
  84. 98765432123456789 
  85. 98765432223456789 
  86. 98765433333456789 
  87. 98765444444456789 
  88. 98765555555556789 
  89. 98766666666666789 
  90. 98777777777777789 
  91. 98888888888888889 
  92. 99999999999999999 
  93. ------- 
  94. */
  95. #include <cstring>
  96. #include <iostream>
  97. using namespace std;
  98. //#include <string>//在程序中使用串需要有串定義,否則可能莫名其妙出錯。
  99. #ifdef _DEBUG
  100. #define new DEBUG_NEW
  101. #undef THIS_FILE
  102. static char THIS_FILE[] = __FILE__;
  103. #endif
  104. /////////////////////////////////////////////////////////////////////////////
  105. // The one and only application object
  106. //
  107. void main()
  108. {
  109.     void hui(int a);//聲明一個無須返回值的函數。
  110.     hui(9);//打印回字型9的圖形。
  111. }
  112. void hui (int a)
  113. {
  114.     int c,m,n;//d,//m爲行數,n爲列數,輸出爲M*N回字矩陣,c爲行輸出基準數,d爲列輸出基準數。
  115.     c=a;//d=a;d不使用。
  116.     for(m=1;m<=2*a-1;m++)//行計數//如果寫成m<2a-1就會出錯,因爲電腦不認識2a,只認識2*a
  117.     {
  118.         for(n=1;n<=2*a-1;n++)//列計數
  119.         {
  120.             if(n<a-c+1)
  121.             {
  122.                 cout<<a-n+1<<" ";
  123.             }
  124.             else
  125.             {
  126.                 if (n>a+c-1)//if(n>a-c+1 && n>9)
  127.                 {
  128.                     cout<<n+1-a<<" ";
  129.                 }
  130.                 else
  131.                 {
  132.                     cout<<c<<" ";
  133.                 }
  134.             }
  135.         }
  136.         cout<<" 行輸出基準數c爲:"<<c<<endl;//作爲數值大小的觀察點
  137.         if(m<a)
  138.         {
  139.             c--;
  140.         }
  141.         else
  142.         {
  143.             c++;
  144.         }
  145.     }
  146. }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章