用vector打印乘法口诀表

打印效果:
用vector打印乘法口诀表_第1张图片

#include 
#include 
#include 
using namespace std;

int main(){
     
	int j = 0;
	vector<vector<int> >vec(9, vector<int>(0));
	for (int i = 0; i < 9; i++){
     
		for (j = 0; j <= i+1; j++){
     
			vec[i].push_back((i+1)*(j+1));
		}
	}

	for (int i = 0; i < vec.size(); i++){
     
		for (int j = 0; j <= i; j++){
     
			//使用setw(n) 进行格式控制
			cout << (i+1) << "*" << (j+1) << "=" << setw(2) << vec[i][j]<< " ";
			//printf("%d*%d=%2d ", (i+1),(j+1),vec[i][j]);
		}
		cout << endl;
	}
	return 0;
}

你可能感兴趣的:(c++)