• 计算等式


    1103 计算等式

    题目描述

    编程在算式123_45_67_8_9=N的下划线部分填上加号(+)或减号(-),使该等式成立。要求程序运行时输出该等式。(保证数据存在满足的等式)

    输入描述

    /*
    输入一个整数N。
    */
    100
    

    输出描述

    /*
    输出满足条件的等式。若不存在满足的等式,则输出"impossible"(输出不包括引号)
    */
    123+45-67+8-9=100
    
    #include<stdio.h>
    #include<string.h>
    
    //直接暴力枚举就完事了
    void isequeal(int n) {
        int x =0;
        int res = 0;
        char c1, c2, c3, c4;
        for(x=0; x<16; x++ ){
            int v = 123;
            c1 = x & 8 ? '+' : '-';
            if ( x & 8 )  v +=  45; else v -= 45;
    
            c2 = x & 4 ? '+' : '-';
            if ( x &4 )  v += 67; else v -= 67;
    
            c3 = x & 2 ? '+' : '-';
            if ( x & 2)  v+= 8; else v -= 8;
    
            c4 = x & 1 ? '+' :  '-';
            if ( x & 1)  v+= 9; else v -= 9;
    
            if ( v == n ) {
                printf("123%c45%c67%c8%c9=%d
    ",c1,c2,c3,c4,n);
                res = 1;
           }
        }
    
        if(!res)
            printf("impossible
    ");
    }
    
    int main()
    {
        int n=0;
        scanf("%d",&n);
        isequeal(n);
        return 0;
    }
    
  • 相关阅读:
    有关乞讨的人
    雪之国
    Direction
    Qt表格
    单例模式
    工作
    Qt模型model、视图view、代理
    Qt数据库sqlite
    QDateTime
    跨工程传输数据
  • 原文地址:https://www.cnblogs.com/lwp-nicol/p/14279184.html
Copyright © 2020-2023  润新知