C++ C++;乘法表
我需要创建一个乘法表,但有非常具体的说明。除了一个零件外,我所有的东西都在工作。输出示例必须如下所示:C++ C++;乘法表,c++,C++,我需要创建一个乘法表,但有非常具体的说明。除了一个零件外,我所有的东西都在工作。输出示例必须如下所示: 1 2 3 4 5 6 7 2 4 6 8 10 12 14 3 6 9 12 15 18 21 4 8 12 16 20 24 28 #include <iostream> #include <iomanip> using namespace std; int main(){ int x = 0; int y = 0; int
1 2 3 4 5 6 7
2 4 6 8 10 12 14
3 6 9 12 15 18 21
4 8 12 16 20 24 28
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int x = 0;
int y = 0;
int a = 0;
// int y = 0; this is the error btw you can't re declare variable
int b = 0; //this is what you wanted i guess
cin >> x;
cin >> y;
for(b = 1; b <= y; ++b){
for(a = 1; a < x; ++a){
cout << setw(2) << a * b << " ";
}
cout <<setw(2)<< b * x << endl;
}
return 0;
}
我的输出看起来像这样,但我编写的代码在每行末尾都有一个空格字符(即7、14、21、28后面有一个空格字符),我不能这样做。以下是我的输出:
1 2 3 4 5 6 7
2 4 6 8 10 12 14
3 6 9 12 15 18 21
4 8 12 16 20 24 28
如果高亮显示每个表,可以看到顶部的表不包含每行末尾的空格字符,但底部的表包含空格字符
我问了我的教授,他说我必须为此写一个特例,但我不知道该怎么做。这是我的密码:
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int x = 0;
int y = 0;
int a = 0;
int b = 0;
cin >> x;
cin >> y;
for(b = 1; b <= y; ++b){
for(a = 1; a <= x; ++a){
cout << setw(2) << a * b << " ";
}
cout << endl;
}
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
int x=0;
int y=0;
int a=0;
int b=0;
cin>>x;
cin>>y;
对于(b=1;b添加一个简单的if
语句,以便在循环的最后一次迭代中无空格地写入输出
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int x = 0;
int y = 0;
int a = 0;
int y = 0;
cin >> x;
cin >> y;
for(b = 1; b <= y; ++b){
for(a = 1; a <= x; ++a){
if (a == x) cout << setw(2) << a * b;
else cout << setw(2) << a * b << " ";
}
cout << endl;
}
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
int x=0;
int y=0;
int a=0;
int y=0;
cin>>x;
cin>>y;
对于(b=1;b添加一个简单的if
语句,以便在循环的最后一次迭代中无空格地写入输出
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int x = 0;
int y = 0;
int a = 0;
int y = 0;
cin >> x;
cin >> y;
for(b = 1; b <= y; ++b){
for(a = 1; a <= x; ++a){
if (a == x) cout << setw(2) << a * b;
else cout << setw(2) << a * b << " ";
}
cout << endl;
}
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
int x=0;
int y=0;
int a=0;
int y=0;
cin>>x;
cin>>y;
对于(b=1;b你可以这样做:
1 2 3 4 5 6 7
2 4 6 8 10 12 14
3 6 9 12 15 18 21
4 8 12 16 20 24 28
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int x = 0;
int y = 0;
int a = 0;
// int y = 0; this is the error btw you can't re declare variable
int b = 0; //this is what you wanted i guess
cin >> x;
cin >> y;
for(b = 1; b <= y; ++b){
for(a = 1; a < x; ++a){
cout << setw(2) << a * b << " ";
}
cout <<setw(2)<< b * x << endl;
}
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
int x=0;
int y=0;
int a=0;
//int y=0;这是一个错误,顺便说一句,您不能重新声明变量
int b=0;//我想这就是你想要的
cin>>x;
cin>>y;
对于(b=1;b你可以这样做:
1 2 3 4 5 6 7
2 4 6 8 10 12 14
3 6 9 12 15 18 21
4 8 12 16 20 24 28
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int x = 0;
int y = 0;
int a = 0;
// int y = 0; this is the error btw you can't re declare variable
int b = 0; //this is what you wanted i guess
cin >> x;
cin >> y;
for(b = 1; b <= y; ++b){
for(a = 1; a < x; ++a){
cout << setw(2) << a * b << " ";
}
cout <<setw(2)<< b * x << endl;
}
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
int x=0;
int y=0;
int a=0;
//int y=0;这是一个错误,顺便说一句,您不能重新声明变量
int b=0;//我想这就是你想要的
cin>>x;
cin>>y;
对于(b=1;b使用条件运算符提高可读性
cout << setw(2) << a * b << (a==x ? "" : " ");
cout使用条件运算符提高可读性
cout << setw(2) << a * b << (a==x ? "" : " ");
当a
时,您不能只考虑打印空格字符吗?好吧,您可以写一个空格,这样空格就会出现在输出中。只是不要写它来修复代码。不过,也许更简单,请查看setw()
在cppreference.com上。如果在第一列,不要打印空格,否则在打印数字之前打印空格。或者使用setw
和右对齐(cout
将填充空格)。您是否考虑过在a
时只打印空格字符?好吧,您可以编写一个空格,以便在输出中显示空格。只是不要编写它来修复代码。不过,也许更简单,请查看setw()
在cppreference.com上。如果在第一列,请不要打印空格,否则在打印数字之前先打印空格。或者使用setw
和右对齐(cout
将填充空格)。