zoukankan      html  css  js  c++  java
  • 计算等式

    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;
    }
    
  • 相关阅读:
    CodeForces
    Codeforces 1523D Love-Hate(随机化算法,sos dp)
    CodeForces
    讲题泛做
    CF vp 新题乱做
    10.11 牛客
    10.6 牛客
    10.4 牛客
    10.9 模拟考试 题解报告
    9.18 校内模拟赛 题解报告
  • 原文地址:https://www.cnblogs.com/lwp-nicol/p/14279184.html
Copyright © 2011-2022 走看看