本帖最后由 无闻无问 于 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;
}
-------------------------------------------------------------------
fori inrange(1,10):
forj inrange(1,i+1):
print(str(j)+'*'+str(i)+'='+str(i*j)+'\t',end='')
print('\n')
还有这位仁兄的问题,用c真的麻烦,生成字符串算式后,还要慢慢处理算式并计算结果,可能是我的技术太low了,用python直接一个eval就将字符串表达式进行了计算并返回结果。
124024lwop8yyhjjo8sarz.png (302.4 KB, 下载次数: 0)
2020-10-10 11:56 上传
用c 语言,我的代码(初学不到家),太菜,采用的方式及算法也极不科学,大神手下留情,还请高手们指教改进……
int checkok(int tempInputNum[15]);
int main()
{
int totalNum = 0, totalOk = 0;
int suanShi[15] = { 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[1] = '+'; }
if (a == 1) { suanShi[1] = '-'; }
if (a == 2) { suanShi[1] = '*'; }
if (a == 3) { suanShi[1] = '/'; }
if (b == 0) { suanShi[3] = '+'; }
if (b == 1) { suanShi[3] = '-'; }
if (b == 2) { suanShi[3] = '*'; }
if (b == 3) { suanShi[3] = '/'; }
if (c == 0) { suanShi[5] = '+'; }
if (c == 1) { suanShi[5] = '-'; }
if (c == 2) { suanShi[5] = '*'; }
if (c == 3) { suanShi[5] = '/'; }
if (d == 0) {suanShi[7] = '+'; }
if (d == 1) {suanShi[7] = '-'; }
if (d == 2) {suanShi[7] = '*'; }
if (d == 3) {suanShi[7] = '/'; }
if (e == 0) {suanShi[9] = '+'; }
if (e == 1) {suanShi[9] = '-'; }
if (e == 2) {suanShi[9] = '*'; }
if (e == 3) {suanShi[9] = '/'; }
if (f == 0) {suanShi[11] = '+'; }
if (f == 1) {suanShi[11] = '-'; }
if (f == 2) {suanShi[11] = '*'; }
if (f == 3) {suanShi[11] = '/'; }
//检查算式是否符合要求。
totalNum ++;
int sum = checkok(suanShi);
if (sum==9)
{
totalOk++;
printf("6%c7%c11%c3%c2%c5%c12=9\n", suanShi[1], suanShi[3], suanShi[5], suanShi[7], suanShi[9], suanShi[11]);
//int sumTemp = checkok(suanShi);
}
}
printf("===========计算完毕!总计%d个算式!其中达到要求的有:%d个===========", totalNum, totalOk);
getchar();
getchar();
return 0;
}
//检查算式是否符合要求。
int checkok(int tempInputNum[15])
{
int tempNumber = 0;
int jishu = 0;
int colume[15] = { '\0' };
for(int numToNnum=0;numToNnum<15;numToNnum++)
colume[numToNnum]= tempInputNum[numToNnum];
bool panduan = false;
bool panduan2 = false;
///遍历运算完乖除//
do
{
panduan = false;
for (int i = 1; i < 14; i++)
{
if (colume== '*' || colume== '/')
{
//如果是乘
if (colume== '*')
{
tempNumber = colume[i - 1] * colume[i + 1];
colume[i - 1] = tempNumber;
colume= '\0';
colume[i + 1] = '\0';
//将剩下的数向前移2位
int num1 = 0, arry1 = 0; int tempSwap = 0;
for (num1 = 0; num1 < 2; num1++)
for (arry1 = i; arry1 < 14 - 1; arry1++)
{
tempSwap = colume[arry1];
colume[arry1] = colume[arry1 + 1];
colume[arry1 + 1] = tempSwap;
}
break;//退出计算
}
else if (colume== '/') //如果是除
{
if (colume[i - 1] % colume[i + 1] != 0) return 0;//产生余数直接返回0
tempNumber = colume[i - 1] / colume[i + 1];
colume[i - 1] = tempNumber;
colume= '\0';
colume[i + 1] = '\0';
//将剩下的数向前移2位
int num1 = 0, arry1 = 0; int tempSwap = 0;
for (num1 = 0; num1 < 2; num1++)
for (arry1 = i; arry1 < 14 - 1; arry1++)
{
tempSwap = colume[arry1];
colume[arry1] = colume[arry1 + 1];
colume[arry1 + 1] = tempSwap;
}
break;//退出计算
}
}
}
//检查是否还有*/
for (int checksym = 1; checksym < 13; checksym+=2)
{
if (colume[checksym] == '*' || colume[checksym] == '/')
{
panduan = true;
break;
}
}
} while (panduan == true);
///遍历运算完+-//
do {
//如果是加减,计算并退出本次循环
panduan2 = false;
for (int i = 1; i < 14; i++)
if (colume== '+' || colume== '-')
{
//如果是+
if (colume== '+')
{
tempNumber = colume[i - 1] + colume[i + 1];
colume[i - 1] = tempNumber;
colume= '\0';
colume[i + 1] = '\0';
//将剩下的数向前移2位
int num1 = 0, arry1 = 0; int tempSwap = 0;
for (num1 = 0; num1 < 2; num1++)
for (arry1 = i; arry1 < 14 - 1; arry1++)
{
tempSwap = colume[arry1];
colume[arry1] = colume[arry1 + 1];
colume[arry1 + 1] = tempSwap;
}
break;//退出计算
}
else if (colume== '-')//如果是-
{
tempNumber = colume[i - 1] - colume[i + 1];
colume[i - 1] = tempNumber;
colume= '\0';
colume[i + 1] = '\0';
//将剩下的数向前移2位
int num1 = 0, arry1 = 0; int tempSwap = 0;
for (num1 = 0; num1 < 2; num1++)
for (arry1 = i; arry1 < 14 - 1; arry1++)
{
tempSwap = colume[arry1];
colume[arry1] = colume[arry1 + 1];
colume[arry1 + 1] = tempSwap;
}
break;//退出计算
}
}
//检查是否还有*/
for (int checksym = 1; checksym < 13; checksym+=2)
{
if (colume[checksym] == '+' || colume[checksym] == '-')
{
panduan2 = true;
break;
}
}
} while (panduan2 == true);
return colume[0];
}
c123.jpg (110.33 KB, 下载次数: 0)
2020-10-10 11:52 上传
而用python,只需要寥寥数行代码:
num =['6','0','7','0','11','0','3','0','2','0','5','0','12']
sym=['+','-','*','/']
fori insym:
forj insym:
fork insym:
forl insym:
form insym:
forn insym:
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')
py123.jpg (124.42 KB, 下载次数: 1)
2020-10-10 11:52 上传
看来,处理数据,解决一些问题,python这小子名不虚传,方便啊……