12*12乘法表

问题:

输出如下乘法表

代码:

#include<iostream>
#include <iomanip>

using namespace std;

int main(){
    int i,j;
    for (i=1; i <= 12; i++){
        
        for (j = 1; j <= 12; j++){
            cout <<setiosflags(std::ios::left) << setw(2)<< i  
                <<setiosflags(std::ios::left) << setw(3)<<"×"
                <<setiosflags(std::ios::left) << setw(3) << j 
                <<setiosflags(std::ios::left) << setw(3) << "=" 
                <<setiosflags(std::ios::left) << setw(3) << (i*j) << setw(3) << " ";
            if (i<j)
            {
                break;
            }
        }
        cout << endl;
    }
    return 0;
}

分析:

setw(3),setiosflags(std::ios::left)只能保证后面的一个输出是三位和左对齐的,所以要对齐所有就要把每个输出都框起来。

猜你喜欢

转载自www.cnblogs.com/dushenda/p/9656901.html