一道关于乘法表的问题,望大神写一个完整的代码我琢磨下
今天看了一道关于乘法表的问题,望大神写一个完整的代码我琢磨下,请符合题目要求,拜谢
[解决办法]
#include <iostream>
using namespace std;
void trace(const char *s)
{
cout<<s<<endl;
}
void doit(const int&,const int&,const int&,const int&);
int main()
{
int x1,x2,y1,y2;
trace("请输入乘法表横向起止值,用空格分开。例如:1 10");
cin>>x1>>x2;
trace("请输入乘法表纵向起止值,用空格分开。例如:1 20");
cin>>y1>>y2;
doit(x1,x2,y1,y2);//生成乘法表
return 0;
}
void doit(const int &x1,const int &x2,const int &y1,const int &y2)
{
int arr[30][30]={0};
cout<<"0"<<"\t";
for (int i=x1;i<=x2;i++)
{
cout<<i<<"\t";
}
cout<<endl;
for (int j=y1;j<=y2;j++)
{
cout<<j<<"\t";
for (i=x1;i<=x2;i++)
{
cout<<i*j<<"\t";
}
cout<<endl;
}
}
#include <stdio.h>
#define W 4
void printMTable(int x1,int x2,int y1,int y2) {
int x,y;
printf("%*s",W,"x");
for (x=x1;x<=x2;x++) printf("%*d",W,x);
printf("\n");
for (y=y1;y<=y2;y++) {
printf("%*d",W,y);
for (x=x1;x<=x2;x++) printf("%*d",W,x*y);
printf("\n");
}
}
int main() {
int x1,x2,y1,y2;
scanf("%d-%d%d-%d",&x1,&x2,&y1,&y2);
printMTable(x1,x2,y1,y2);
return 0;
}
//以下为适当的注释:(^_^)
//1-10
//2-12
// x 1 2 3 4 5 6 7 8 9 10
// 2 2 4 6 8 10 12 14 16 18 20
// 3 3 6 9 12 15 18 21 24 27 30
// 4 4 8 12 16 20 24 28 32 36 40
// 5 5 10 15 20 25 30 35 40 45 50
// 6 6 12 18 24 30 36 42 48 54 60
// 7 7 14 21 28 35 42 49 56 63 70
// 8 8 16 24 32 40 48 56 64 72 80
// 9 9 18 27 36 45 54 63 72 81 90
// 10 10 20 30 40 50 60 70 80 90 100
// 11 11 22 33 44 55 66 77 88 99 110
// 12 12 24 36 48 60 72 84 96 108 120
//