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')
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';
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')