1 2 3 4 5 6 7 8 9 =110

SMITH_JAT 2013-04-17 02:35:45
1 2 3 4 5 6 7 8 9 =110 中间可以挿+ - 或者不挿 如果1 2 不挿的话 就是12 求指教 ,现在我的思路是:1,用随机数产生八个符号 然后将 数字与符号都压入栈中,最后出栈 一个一个进行相加 如果是-将其转换成赋数进行相加 ,例如 符号为 ++--++--
s= 0 ,s=(-9)+(-8)+7+6 +(-5)+(-4)+3+2 +1 从尾部开始进行运算 求各位高手指点迷津。
...全文
1453 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
aozhi 2013-04-19
  • 打赏
  • 举报
回复
引用 9 楼 zhao4zhong1 的回复:
C/C++ code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929……
请看清题再贴代码。
赵4老师 2013-04-19
  • 打赏
  • 举报
回复
/*---------------------------------------
函数型计算器(VC++6.0,Win32 Console)程序由 yu_hua 于2007-07-27设计完成
功能:
目前提供了10多个常用数学函数:
    ⑴正弦sin
    ⑵余弦cos
    ⑶正切tan
    ⑷开平方sqrt
    ⑸反正弦arcsin
    ⑹反余弦arccos
    ⑺反正切arctan
    ⑻常用对数lg
    ⑼自然对数ln
    ⑽e指数exp
    ⑾乘幂函数∧
用法:
如果要求2的32次幂,可以打入2^32<回车>
如果要求30度角的正切可键入tan(Pi/6)<回车>
注意不能打入:tan(30)<Enter>
如果要求1.23弧度的正弦,有几种方法都有效:
sin(1.23)<Enter>
sin 1.23 <Enter>
sin1.23  <Enter>
如果验证正余弦的平方和公式,可打入sin(1.23)^2+cos(1.23)^2 <Enter>或sin1.23^2+cos1.23^2 <Enter>
此外两函数表达式连在一起,自动理解为相乘如:sin1.23cos0.77+cos1.23sin0.77就等价于sin(1.23)*cos(0.77)+cos(1.23)*sin(0.77)
当然你还可以依据三角变换,再用sin(1.23+0.77)也即sin2验证一下。
本计算器充分考虑了运算符的优先级因此诸如:2+3*4^2 实际上相当于:2+(3*(4*4))
另外函数名前面如果是数字,那么自动认为二者相乘.
同理,如果某数的右侧是左括号,则自动认为该数与括弧项之间隐含一乘号。
如:3sin1.2^2+5cos2.1^2 相当于3*sin2(1.2)+5*cos2(2.1)
又如:4(3-2(sqrt5-1)+ln2)+lg5 相当于4*(3-2*(√5 -1)+loge(2))+log10(5)
此外,本计算器提供了圆周率 Pi键入字母时不区分大小写,以方便使用。
----------------------------------------*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <windows.h>
using namespace std;
const char Tab=0x9;
const int  DIGIT=1;
const int MAXLEN=16384;
char s[MAXLEN],*endss;
int pcs=15;
double fun(double x,char op[],int *iop) {
    while (op[*iop-1]<32) //本行使得函数嵌套调用时不必加括号,如 arc sin(sin(1.234)) 只需键入arc sin sin 1.234<Enter>
        switch (op[*iop-1]) {
        case  7: x=sin(x);  (*iop)--;break;
        case  8: x=cos(x);  (*iop)--;break;
        case  9: x=tan(x);  (*iop)--;break;
        case 10: x=sqrt(x); (*iop)--;break;
        case 11: x=asin(x); (*iop)--;break;
        case 12: x=acos(x); (*iop)--;break;
        case 13: x=atan(x); (*iop)--;break;
        case 14: x=log10(x);(*iop)--;break;
        case 15: x=log(x);  (*iop)--;break;
        case 16: x=exp(x);  (*iop)--;break;
        }
    return x;
}
double calc(char *expr,char **addr) {
    static int deep; //递归深度
    static char *fname[]={ "sin","cos","tan","sqrt","arcsin","arccos","arctan","lg","ln","exp",NULL};
    double ST[10]={0.0}; //数字栈
    char op[10]={'+'}; //运算符栈
    char c,*rexp,*pp,*pf;
    int ist=1,iop=1,last,i;
    if (!deep) {
        pp=pf=expr;
        do {
            c = *pp++;
            if (c!=' '&& c!=Tab)
                *pf++ = c;
        } while (c!='\0');
    }
    pp=expr;
    if ((c=*pp)=='-'||c=='+') {
        op[0] = c;
        pp++;
    }
    last = !DIGIT;
    while ((c=*pp)!='\0') {
        if (c=='(') {//左圆括弧
            deep++;
            ST[ist++]=calc(++pp,addr);
            deep--;
            ST[ist-1]=fun(ST[ist-1],op,&iop);
            pp = *addr;
            last = DIGIT;
            if (*pp == '('||isalpha(*pp) && strnicmp(pp,"Pi",2)) {//目的是:当右圆括弧的右恻为左圆括弧或函数名字时,默认其为乘法
                op[iop++]='*';
                last = !DIGIT;
                c = op[--iop];
                goto operate ;
            }
        }
        else if (c==')') {//右圆括弧
            pp++;
            break;
        } else if (isalpha(c)) {
            if (!strnicmp(pp,"Pi",2)) {
                if (last==DIGIT) {
                    cout<< "π左侧遇)" <<endl;exit(1);
                }
                ST[ist++]=3.14159265358979323846264338328;
                ST[ist-1]=fun(ST[ist-1],op,&iop);
                pp += 2;
                last = DIGIT;
                if (!strnicmp(pp,"Pi",2)) {
                    cout<< "两个π相连" <<endl;exit(2);
                }
                if (*pp=='(') {
                    cout<< "π右侧遇(" <<endl;exit(3);
                }
            } else {
                for (i=0; (pf=fname[i])!=NULL; i++)
                    if (!strnicmp(pp,pf,strlen(pf))) break;
                if (pf!=NULL) {
                    op[iop++] = 07+i;
                    pp += strlen(pf);
                } else {
                    cout<< "陌生函数名" <<endl;exit(4);
                }
            }
        } else if (c=='+'||c=='-'||c=='*'||c=='/'||c=='^') {
            char cc;
            if (last != DIGIT) {
                cout<< "运算符粘连" <<endl;exit(5);
            }
            pp++;
            if (c=='+'||c=='-') {
                do {
                    cc = op[--iop];
                    --ist;
                    switch (cc) {
                    case '+':  ST[ist-1] += ST[ist];break;
                    case '-':  ST[ist-1] -= ST[ist];break;
                    case '*':  ST[ist-1] *= ST[ist];break;
                    case '/':  ST[ist-1] /= ST[ist];break;
                    case '^':  ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
                    }
                } while (iop);
                op[iop++] = c;
            } else if (c=='*'||c=='/') {
operate:        cc = op[iop-1];
                if (cc=='+'||cc=='-') {
                    op[iop++] = c;
                } else {
                    --ist;
                    op[iop-1] = c;
                    switch (cc) {
                    case '*':  ST[ist-1] *= ST[ist];break;
                    case '/':  ST[ist-1] /= ST[ist];break;
                    case '^':  ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
                    }
                }
            } else {
                cc = op[iop-1];
                if (cc=='^') {
                    cout<< "乘幂符连用" <<endl;exit(6);
                }
                op[iop++] = c;
            }
            last = !DIGIT;
        } else {
            if (last == DIGIT) {
                cout<< "两数字粘连" <<endl;exit(7);
            }
            ST[ist++]=strtod(pp,&rexp);
            ST[ist-1]=fun(ST[ist-1],op,&iop);
            if (pp == rexp) {
                cout<< "非法字符" <<endl;exit(8);
            }
            pp = rexp;
            last = DIGIT;
            if (*pp == '('||isalpha(*pp)) {
                op[iop++]='*';
                last = !DIGIT;
                c = op[--iop];
                goto operate ;
            }
        }
    }
    *addr=pp;
    if (iop>=ist) {
        cout<< "表达式有误" <<endl;exit(9);
    }
    while (iop) {
        --ist;
        switch (op[--iop]) {
        case '+':  ST[ist-1] += ST[ist];break;
        case '-':  ST[ist-1] -= ST[ist];break;
        case '*':  ST[ist-1] *= ST[ist];break;
        case '/':  ST[ist-1] /= ST[ist];break;
        case '^':  ST[ist-1] = pow(ST[ist-1],ST[ist]);break;
        }
    }
    return ST[0];
}
int main(int argc,char **argv) {
    //1 2 3 4 5 6 7 8 9 =110 中间可以挿+ - 或者不挿 如果1 2 不挿的话 就是12
    char op[4]=" +-";
    int g1;
    int g2;
    int g3;
    int g4;
    int g5;
    int g6;
    int g7;
    int g8;

    for (g1=0;g1<3;g1++)
    for (g2=0;g2<3;g2++)
    for (g3=0;g3<3;g3++)
    for (g4=0;g4<3;g4++)
    for (g5=0;g5<3;g5++)
    for (g6=0;g6<3;g6++)
    for (g7=0;g7<3;g7++)
    for (g8=0;g8<3;g8++) {
        strcpy(s,"1 2 3 4 5 6 7 8 9");
        s[ 1]=op[g1];
        s[ 3]=op[g2];
        s[ 5]=op[g3];
        s[ 7]=op[g4];
        s[ 9]=op[g5];
        s[11]=op[g6];
        s[13]=op[g7];
        s[15]=op[g8];
        if (110.0==calc(s,&endss)) printf("%s=110\n",s);
    }
    /*
    if (argc<=1) {
        if (GetConsoleOutputCP()!=936) system("chcp 936>NUL");//中文代码页
        cout << "计算函数表达式的值。"<<endl<<"支持(),+,-,*,/,^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp"<<endl;
        while (1) {
            cout << "请输入表达式:";
            gets(s);
            if (s[0]==0) break;//
            cout << s <<"=";
            cout << setprecision(15) << calc(s,&endss) << endl;
        }
    } else {
        strncpy(s,argv[1],MAXLEN-1);s[MAXLEN-1]=0;
        if (argc>=3) {
            pcs=atoi(argv[2]);
            if (pcs<0||15<pcs) pcs=15;
            printf("%.*lf\n",pcs,calc(s,&endss));
        } else {
            printf("%.15lg\n",calc(s,&endss));
        }
    }
    */
    return 0;
}
//123+4+5+67-89=110
//123+4-5-6-7-8+9=110
//123-4+5-6-7+8-9=110
//123-4-5+6+7-8-9=110
//12+34+56+7-8+9=110
//12+3+45+67-8-9=110
//12-3+4-5+6+7+89=110
//1+234-56-78+9=110
//1+2+34+5+67-8+9=110
//1-2+3+45-6+78-9=110
vuqrzk5w 2013-04-19
  • 打赏
  • 举报
回复
引用 10 楼 aozhi 的回复:
引用 9 楼 zhao4zhong1 的回复:C/C++ code?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081……
跟我有同样的感觉,zhao4zhong1 这家伙,每次回答人问题,不看题,自己怎么方便怎么回答.已经有人评论过他了,不过他还一副盛气凌人的样子,说问问题的人太菜,看不懂他的大才.
c090869 2013-04-18
  • 打赏
  • 举报
回复
噢,算法错误,应该先乘后加减.
c090869 2013-04-18
  • 打赏
  • 举报
回复
用暴力法,把所有计算方式都算一遍. int b[10]; void dd(long x,int n) { int i,s; if(n==10&&x==110) { for(i=2;i<10;i++) printf("%d",b[i]); printf("\n"); } if(n<10) for(i=0;i<3;i++) { b[n]=i; if(i==0) s=x+n; if(i==1) s=x-n; if(i==2) s=x*10+n; dd(s,n+1); } } void main() { dd(1,2);}
c090869 2013-04-18
  • 打赏
  • 举报
回复
又尝试了一下,为了保证先乘后加减,用叄数 x,z,y 保留前面加减运算。 n 表示下一个要运算的数字。宭 int b[10]; void dd(long x,int z,long y,int n) { int i,q,fh,h; if(n==10) { if(z==0) q=x+y; else q=x-y; if(q==110) { for(i=2;i<10;i++) printf(" %d",b[i]); printf("\n"); } } if(n<10) for(i=0;i<3;i++) { b[n]=i; if(i!=2){ if(z==0) q=x+y; else q=x-y; fh=i;h=n;} if(i==2) { q=x; fh=z; h=y*10+n; } dd(q,fh,h,n+1); } } void main() { dd(0,0,1,2);}
rocktyt 2013-04-18
  • 打赏
  • 举报
回复
一个实现,可能比较丑陋但是结果是正确的
enum class ops
{
    plus,
    minus,
    multiplies,
    divides,
    power,
    root,
    concat//10*a+b
};
int level(ops op)
{
    switch (op)
    {
    case ops::plus:
    case ops::minus:
        return 0;
    case ops::concat:
        return 1;
    default:
        return 0;
    }
}
struct operation
{
    ops op;
    int lv;
    operation(ops t = ops::plus)
    :op(t), lv(level(t))
    {}
    int operator()(int a, int b)
    {
        switch (op)
        {
        case ops::plus:
            return a+b;
        case ops::minus:
            return a-b;
        case ops::concat:
            return 10*a+b;
        default:
            return 0;
        }
    }
    operator const char*()
    {
        switch (op)
        {
        case ops::plus:
            return "+";
        case ops::minus:
            return "-";
        default:
            return "";
        }
    }
};

int calculate(vector<int> nums, vector<operation> opers)
{
    for (int lv = 9; lv >= 0; lv--)
    {
        auto itn = nums.begin();
        auto ito = opers.begin();
        for(; ito != opers.end();)
        {
            if (ito->lv == lv)
            {
                //calculate here, remove a number and a operator,
                //then replace the other number with the answer
                int ans = (*ito)(itn[0], itn[1]);
                nums.erase(itn);
                *itn = ans;
                opers.erase(ito);
            }
            else
            {
                ++itn;
                ++ito;
            }
        }
    }
    return nums[0];
}
int main()
{
    ops op[] = {ops::plus, ops::minus, ops::concat};
    vector<int> nums{1,2,3,4,5,6,7,8,9};
    int ans=110;
    vector<operation> opers;
    for (int i=0; i<6561; i++)
    {
        int t=i;
        for (int j=0; j<8; j++)
        {
            opers.push_back(op[t%3]);
            t/=3;
        }
        if (calculate(nums, opers) == 110)
        {
            for (int j=0; j<8; j++)
            {
                cout<<j+1<<opers[j];
            }
            cout<<9<<"=110"<<endl;
        }
        opers.clear();
    }
    return 0 ;
}
vuqrzk5w 2013-04-18
  • 打赏
  • 举报
回复
没看懂你这个题啊,数字间只允许插+、-?那怎么还有负数出现了,还是说数字间可以插两个,三个?
ZHONGCONGWUJIANG 2013-04-17
  • 打赏
  • 举报
回复
然后可以使用爬山法,就是修改其中某个算符之后判断是否能与所需结果更加接近,有16个方向,找到其中最接近的,直到不能更加接近,这包括两种情况,一种是得到结果,一种是局部最优,第一种的话就正确结束,第二种的话就再随即开始。 不过就算用随机生成符号,也不需要使用栈,而只需一个符号数组,一个之前运算结果和一个当前值
SMITH_JAT 2013-04-17
  • 打赏
  • 举报
回复
随机数产生 0 1 2 中 任意一个 0 代表+ 1 代表 - 2代表不加符号
rocktyt 2013-04-17
  • 打赏
  • 举报
回复
定义一种连接运算,效果是 connect(a,b) = a*10+b 优先级高于加和减 然后就循环测试咯 随机数能干什么用?

69,372

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧