用c++语言,用for语言,不要太高级,打印九九乘法表,右上三角形,且最左边有一列对应1~9数字。

如题所述

第1个回答  2011-11-11
/*简单版*/
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
// 乘法表主体
for(int i=1;i<=9;i++)
{
cout << i << " | ";
for(int j=1;j<=9;j++)
{
if(j<i)
{
cout << string(7,' ');
}
else
{
cout << i << "*" << j << "=";
cout.width(2);
cout << i*j << " ";
}
}
cout << endl;
}
return 0;
}
/*
1 | 1*1= 1 1*2= 2 1*3= 3 1*4= 4 1*5= 5 1*6= 6 1*7= 7 1*8= 8 1*9= 9
2 | 2*2= 4 2*3= 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18
3 | 3*3= 9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27
4 | 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36
5 | 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45
6 | 6*6=36 6*7=42 6*8=48 6*9=54
7 | 7*7=49 7*8=56 7*9=63
8 | 8*8=64 8*9=72
9 | 9*9=81
*/

/*复杂版*/
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
// 表头
for(int i=0;i<=9;i++)
{
if(i==0)
{
cout<<string(7,' ') ;
}
else
{
cout.width(6);
cout <<i << ' ';
}
}
// 水平分割线
cout << endl << string(10*7+1,'-') << endl;

// 乘法表主体
for(int i=1;i<=9;i++)
{
cout << i << " | ";
for(int j=1;j<=9;j++)
{
if(j<i)
{
cout << string(7,' ');
}
else
{
cout << i << "*" << j << "=";
cout.width(2);
cout << i*j << " ";
}
}
cout << "|" << endl;
}

// 水平分割线
cout << string(10*7+1,'-') << endl;
return 0;
}
/*
1 2 3 4 5 6 7 8 9
-----------------------------------------------------------------------
1 | 1*1= 1 1*2= 2 1*3= 3 1*4= 4 1*5= 5 1*6= 6 1*7= 7 1*8= 8 1*9= 9 |
2 | 2*2= 4 2*3= 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 |
3 | 3*3= 9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 |
4 | 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 |
5 | 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 |
6 | 6*6=36 6*7=42 6*8=48 6*9=54 |
7 | 7*7=49 7*8=56 7*9=63 |
8 | 8*8=64 8*9=72 |
9 | 9*9=81 |
-----------------------------------------------------------------------
*/
第2个回答  2011-11-11
int i, j;
for (i = 1; i <= 9; i++) {
printf("%d:", i);
for (j = 1; j <= i; j++) {
printf("%d x %d = %d ", i, j, i*j);
}
printf("\n");
}本回答被提问者采纳
第3个回答  2011-11-11
这么简单的问题,还要意思问,主要就是一个打印效果的问题,自己慢慢调整就好了
第4个回答  2011-11-11
同意一楼意见。
第5个回答  2011-11-11
同意一楼。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网