编程心得

    技术2022-07-10  103

    关于一个优秀程序员半小时必须解决的5个问题(之一)

    编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。 用简单粗暴的方法最有效,嘿嘿。

    operator = ['+', '-', ''] for i0 in operator: for i1 in operator: for i2 in operator: for i3 in operator: for i4 in operator: for i5 in operator: for i6 in operator: for i7 in operator: string = '1' + i0 + '2' + i1 + '3' + i2 + '4' \ + i3 + '5' + i4 + '6' + i5 + '7' + i6 + '8' + i7 + '9' if eval(string) == 100: print("{0}=100".format(string))

    细想一下,这样表达太不雅观,体现不出python代码简洁明快的特点,还是改进一下吧。

    from itertools import product p = ['+', '-', ''] for i0, i1, i2, i3, i4, i5, i6, i7 in product(p, p, p, p, p, p, p, p): string = '1' + i0 + '2' + i1 + '3' + i2 + '4' \ + i3 + '5' + i4 + '6' + i5 + '7' + i6 + '8' + i7 + '9' if eval(string) == 100: print("{0}=100".format(string))

    再改进一下:

    from itertools import product p = ['+', '-', ''] operator = [p for _ in range(8)] for g in product(*operator): string = '' for i in range(1, 9): string += str(i) + g[i - 1] string += '9' if eval(string) == 100: print("{0}=100".format(string))

    用C++是这样写的:

    #include <iostream> #include<deque> #include<vector> using namespace std; int main() { vector<string>jiang; string base = "123456789"; string operation = "+- ";//运算符集合 deque<int>data;//存放由“+”、“-”分割的多个数据 deque<char>ch;//存放运算符集合 string hao, ming, result, transition; //遍历 for (auto i0 : operation) for (auto i1 : operation) for (auto i2 : operation) for (auto i3 : operation) for (auto i4 : operation) for (auto i5 : operation) for (auto i6 : operation) for (auto i7 : operation) { char z[] = { i0,i1,i2,i3,i4,i5,i6,i7 }; hao = ""; for (int i = 0; i < 9; i++) {//把运算符插入到数字中间,获得字符串表达式 hao += base[i]; if (i != 8)hao.append(1, z[i]); } ming = ""; for (auto k : hao) {//把字符串表达式中的空格剔除 if (k != ' ')ming += k; } result = ming;//每次获取完整表达式后保存起来,一旦满足条件,直接使用该数据进行输出 data.clear(); ch.clear();//每次使用前清空 /*把由“+”、“-”分割的由数字组成的字符串逐个转换成整数保存到一个队列中, 运算符保存到另外一个队列中,字符串表达式中的数据和运算符分别进栈*/ while (ming != "") { int count = 0, mark = 0;//count确定运算符的位置,mark标记是否找到运算符 transition = ""; for (auto i : ming) { if (i == '+' || i == '-') { mark = 1; transition = ming.substr(0, count); data.push_back(atoi(transition.c_str())); ch.push_back(i); break; } count++; } //如果没有找运算符,把剩余的字符串全部转换成整数推到栈里 if (mark == 0) { data.push_back(atoi(ming.c_str())); ming = ""; } /*如果找到运算符,说明后续还有数据或运算符,丢弃当前运算符及前面的字符串, 保留后续的字符串,重复找下一个运算符*/ else { transition = ""; int cc = 0; for (auto j : ming) { if (cc > count)transition += j; cc++; } ming = transition; } } //对满足条件的表达式进行输出 int sum = 0; char c = '0'; sum += data.front(); data.pop_front(); while (!ch.empty()) { c = ch.front(); if (c == '+') sum += data.front(); else sum -= data.front(); data.pop_front(); ch.pop_front(); } if (sum == 100)cout << result << "=" << sum << endl; } } D:\Python\study\venv\Scripts\python.exe D:/Python/study/test21.py 1+2+3-4+5+6+78+9=100 1+2+34-5+67-8+9=100 1+23-4+5+6+78-9=100 1+23-4+56+7+8+9=100 12+3+4+5-6-7+89=100 12+3-4+5+67+8+9=100 12-3-4+5-6+7+89=100 123+4-5+67-89=100 123+45-67+8-9=100 123-4-5-6-7+8-9=100 123-45-67+89=100 Process finished with exit code 0
    Processed: 0.013, SQL: 9