首頁  >  文章  >  後端開發  >  c++輸出二維字元矩陣對齊

c++輸出二維字元矩陣對齊

little bottle
little bottle轉載
2019-04-24 14:40:105637瀏覽

本篇文章,小編想帶大家回憶一下C ,本篇文章的主要內容是用c 輸出二維字符矩陣對齊,具有一定的參考價值,有興趣的朋友可以了解一下。

頭檔#include 3f68df5471146346142495b14e43a419

關鍵字:setw(n),std::left,std::right

實例:輸出一個0-4的12*12方陣,要求數字寬度為4,居左對齊,右下角輸出出品人、時間、運行時間居右對齊。

程式碼:

#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int main()
{
    srand((unsigned)time(NULL));
    int p[12][12] = { 0 };
    for (int i = 0; i < 12; i++)
    {
        for (int j = 0; j < 12; j++)
        {
            p[i][j] = rand() % 5;
        }
    }
    for (int i = 0; i < 12; i++)
    {
        for (int j = 0; j < 12; j++)
        {
            cout <<std::left<< setw(4) << p[i][j];
        }
        cout << endl;
    }
    time_t now = time(0);
    char *t = ctime(&now);
    cout << std::right << setw(45) << "出品人:会武术之白猫" << endl;
    cout << std::right << setw(46) << t << endl;
    cout << std::right << setw(41) << clock() / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;
}

結果:

#一個setw和std::right只對後邊一個變數有效,切記。

相關教學:C 影片教學

以上是c++輸出二維字元矩陣對齊的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除