打印9*9乘法表,解决数学问题,c和python,谁更方便?
本帖最后由 无闻无问 于 2020-10-10 14:41 编辑c语言还没到家,现在脑门一热,学习了一下python,遂用二个工具编写9*9乘法表进行对比,感觉python编写速度真不错!
#include "stdafx.h"
int main()
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
printf("%d*%d=%d\t", j, i, i*j);
}
printf("\n");
}
getchar();
getchar();
return 0;
}
-------------------------------------------------------------------
for i in range(1,10):
for j in range(1,i+1):
print(str(j)+'*'+str(i)+'='+str(i*j)+'\t',end='')
print('\n')
还有这位仁兄的问题,用c真的麻烦,生成字符串算式后,还要慢慢处理算式并计算结果,可能是我的技术太low了,用python直接一个eval就将字符串表达式进行了计算并返回结果。
https://www.52pojie.cn/thread-1274703-1-1.html
用c 语言,我的代码(初学不到家),太菜,采用的方式及算法也极不科学,大神手下留情,还请高手们指教改进……
intcheckok(int tempInputNum);
int main()
{
int totalNum = 0, totalOk = 0;
int suanShi = { 6,'\0',7,'\0',11,'\0',3,'\0',2,'\0',5,'\0',12 };
printf("在这些数字中间填上+-*/使算式成立:6 7 11 3 2 5 12=9 \n");
int a, b, c, d, e, f;
for (a = 0; a < 4; a++)
for (b = 0; b < 4; b++)
for (c = 0; c < 4; c++)
for (d = 0; d < 4; d++)
for (e = 0; e < 4; e++)
for (f = 0; f < 4; f++)
{
//遍历生成算式
if (a == 0) { suanShi= '+'; }
if (a == 1) { suanShi= '-'; }
if (a == 2) { suanShi= '*'; }
if (a == 3) { suanShi= '/'; }
if (b == 0) { suanShi = '+'; }
if (b == 1) { suanShi = '-'; }
if (b == 2) { suanShi = '*'; }
if (b == 3) { suanShi = '/'; }
if (c == 0) { suanShi= '+'; }
if (c == 1) { suanShi= '-'; }
if (c == 2) { suanShi= '*'; }
if (c == 3) { suanShi= '/'; }
if (d == 0) {suanShi= '+'; }
if (d == 1) {suanShi= '-'; }
if (d == 2) {suanShi= '*'; }
if (d == 3) {suanShi= '/'; }
if (e == 0) {suanShi= '+'; }
if (e == 1) {suanShi= '-'; }
if (e == 2) {suanShi= '*'; }
if (e == 3) {suanShi= '/'; }
if (f == 0) {suanShi= '+'; }
if (f == 1) {suanShi= '-'; }
if (f == 2) {suanShi= '*'; }
if (f == 3) {suanShi= '/'; }
//检查算式是否符合要求。
totalNum ++;
int sum = checkok(suanShi);
if (sum==9)
{
totalOk++;
printf("6%c7%c11%c3%c2%c5%c12=9\n", suanShi, suanShi, suanShi, suanShi, suanShi, suanShi);
//int sumTemp = checkok(suanShi);
}
}
printf("===========计算完毕!总计%d个算式!其中达到要求的有:%d个===========", totalNum, totalOk);
getchar();
getchar();
return 0;
}
//检查算式是否符合要求。
int checkok(int tempInputNum)
{
int tempNumber = 0;
int jishu = 0;
int colume = { '\0' };
for(int numToNnum=0;numToNnum<15;numToNnum++)
colume= tempInputNum;
bool panduan = false;
bool panduan2 = false;
///////////////////////////////////////遍历运算完乖除//////////////////////////////
do
{
panduan = false;
for (int i = 1; i < 14; i++)
{
if (colume == '*' || colume == '/')
{
//如果是乘
if (colume == '*')
{
tempNumber = colume * colume;
colume = tempNumber;
colume = '\0';
colume = '\0';
//将剩下的数向前移2位
int num1 = 0, arry1 = 0; inttempSwap = 0;
for (num1 = 0; num1 < 2; num1++)
for (arry1 = i; arry1 < 14 - 1; arry1++)
{
tempSwap = colume;
colume = colume;
colume = tempSwap;
}
break;//退出计算
}
else if (colume == '/') //如果是除
{
if (colume % colume != 0) return 0;//产生余数直接返回0
tempNumber = colume / colume;
colume = tempNumber;
colume = '\0';
colume = '\0';
//将剩下的数向前移2位
int num1 = 0, arry1 = 0; inttempSwap = 0;
for (num1 = 0; num1 < 2; num1++)
for (arry1 = i; arry1 < 14 - 1; arry1++)
{
tempSwap = colume;
colume = colume;
colume = tempSwap;
}
break;//退出计算
}
}
}
//检查是否还有*/
for (int checksym = 1; checksym < 13; checksym+=2)
{
if (colume == '*' || colume == '/')
{
panduan = true;
break;
}
}
} while (panduan == true);
///////////////////////////////////////遍历运算完+-//////////////////////////////
do {
//如果是加减,计算并退出本次循环
panduan2 = false;
for (int i = 1; i < 14; i++)
if (colume == '+' || colume == '-')
{
//如果是+
if (colume == '+')
{
tempNumber = colume + colume;
colume = tempNumber;
colume = '\0';
colume = '\0';
//将剩下的数向前移2位
int num1 = 0, arry1 = 0; inttempSwap = 0;
for (num1 = 0; num1 < 2; num1++)
for (arry1 = i; arry1 < 14 - 1; arry1++)
{
tempSwap = colume;
colume = colume;
colume = tempSwap;
}
break;//退出计算
}
else if (colume == '-')//如果是-
{
tempNumber = colume - colume;
colume = tempNumber;
colume = '\0';
colume = '\0';
//将剩下的数向前移2位
int num1 = 0, arry1 = 0; inttempSwap = 0;
for (num1 = 0; num1 < 2; num1++)
for (arry1 = i; arry1 < 14 - 1; arry1++)
{
tempSwap = colume;
colume = colume;
colume = tempSwap;
}
break;//退出计算
}
}
//检查是否还有*/
for (int checksym = 1; checksym < 13; checksym+=2)
{
if (colume == '+' || colume == '-')
{
panduan2 = true;
break;
}
}
} while (panduan2 == true);
return colume;
}
而用python,只需要寥寥数行代码:
num =['6','0','7','0','11','0','3','0','2','0','5','0','12']
sym=['+','-','*','/']
for i in sym:
for j in sym:
for k in sym:
for l in sym:
for m in sym:
for n in sym:
num[11]=n
num[9]=m
num[7]=l
num[5]=k
num[3]=j
num[1]=i
strSplit=''.join(num)
if(eval(strSplit)==9):
print(strSplit + '=9')
看来,处理数据,解决一些问题,python这小子名不虚传,方便啊……
j = 1
while j <= 9:
i = 1
while i <= j:
print(f'{i} * {j} = {i*j}', end='\t')
i += 1
print()
j += 1
liphily 发表于 2020-10-12 15:59
python全靠import
c语言只是没那么多的.h给你include
搞大事还得c语言
肯定吹得凶,一是可卖模块,二是像易语言一样见效快… {:1_893:}感谢分享。 py yes! 学习了,楼主厉害!! python是高级的不能再高级的语言了,c语言是低级语言,很多方法都没有的,都是要自己写,所以才麻烦 找工作还是JAVA,PHP之流,PYTHON做为辅助工具写点小程序非常优秀 感谢楼主分享源码,学习了 感觉学会python 转其他语言就比较吃力了 看你写的+-*/就知道是新手