希望能让看到的人看懂。
先看代码和效果:
思路呢就是两层循环去输出x轴和y轴,y轴的话可以看出一直是 9 个是不会变得,所以 i 就是9次循环。x轴的话就是每次会加一列,所以 j 就会在y轴加 1 时每次都加上 1 ,也就是num++。
至于for循环里面的 if..else ,就是用来排版的,有兴趣的可以删掉看一下。
cout可以换成printf,自己尝试一下有助于理解。
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
handle()
{
int num = 1;
for (int i = 1; i < 10; i++)
{
for (int j = 1; j <= num; j++)
{
cout<<j<<"x"<<i<<"="<<j*i;
if(j*i < 10)
cout<<" ";
else
cout<<" ";
}
cout<<"\n"<<endl;
if (num == 9)
break;
num++;
}
}
int main(int argc, char** argv) {
handle();
return 0;
}
这种的话都是基础,当然还有其他写法,想研究的自己研究吧。