zoukankan      html  css  js  c++  java
  • 微软买书问题

    1.算法思想

    先用枚举法把依次把每种情况从大到小写出来,结果就会发现到8的时候情况就发生变化,以10为1个轮回,除10取余。

    2.代码

    #include<iostream>
    using namespace std;
    #define N 100
    int n;
    void main()
    {
        cout << "请输入你要购买的书的数目:";
        cin >> n;
        int i,j;
        i = n % 10;
        j = (n - i) / 10;
        if (i == 8)
        {
            if (j == 0)
            {
                cout << "应该购买5卷中任意4卷,每卷" << 2 << "";
            }
            else
            {
                cout << "先买5种不同的卷各" << 2 * j << "本,再挑4种不同的卷每卷" << 2 << "";
            }
        
        }
        else if (i == 0)
        {
            cout << "应该购买5卷,每卷" << 2*j << "";
        }
        else if (i == 1)
        {
            if (j == 0)
            {
                cout << "应该购买5卷中任意1卷"<< 1 << "";
            }
            else
            {
                cout << "先买5种不同的卷各" << 2 * j << "本,再购买5卷中任意1卷" << 1 << "";
            }
        }
        else if (i == 2)
        {
            if (j == 0)
            {
                cout << "应该购买5卷中任意2卷每卷" << 1 << "";
            }
            else
            {
                cout << "先买5种不同的卷各" << 2 * j<< "本,再购买5卷中任意2卷每卷" << 1 << "";
            }
        }
        else if (i == 3)
        {
            if (j == 0)
            {
                cout << "应该购买5卷中任意3卷每卷" << 1 << "";
            }
            else
            {
                cout << "先买5种不同的卷各" << 2 * j << "本,再购买5卷中任意3卷每卷" << 1 << "";
            }
        }
        else if (i == 4)
        {
            if (j == 0)
            {
                cout << "应该购买5卷中任意4卷每卷" << 1 << "";
            }
            else
            {
                cout << "先买5种不同的卷各" << 2 * j << "本,再购买5卷中任意2卷每卷" << 1 << "";
            }
        }
        else if (i == 5)
        {
            if (j == 0)
            {
                cout << "应该购买5卷每卷" << 1 << "";
            }
            else
            {
                cout << "买5种不同的卷各" << 2 * j +1<< "";
            }
        }
        else if (i == 6)
        {
            if (j == 0)
            {
                cout << "应该购买5卷每卷" << 1 << "本,再任意购买1卷1本";
            }
            else
            {
                cout << "先买5种不同的卷各" << 2 * j +1<< "本,再任意购买1卷1本";
            }
        }
        else if (i == 7)
        {
            if (j == 0)
            {
                cout << "应该购买5卷每卷" << 1 << "本,再任意购买2卷每卷1本";
            }
            else
            {
                cout << "先买5种不同的卷各" << 2 * j + 1 << "本,再任意购买2卷每卷1本";
            }
        }
        else if (i == 9)
        {
            if (j == 0)
            {
                cout << "应该购买5卷每卷" << 1 << "本,再任意购买4卷每卷1本";
            }
            else
            {
                cout << "先买5种不同的卷各" << 2 * j + 1 << "本,再任意购买4卷每卷1本";
            }
        }
    }

    3.程序截图

  • 相关阅读:
    Spring EL Operators example
    Spring EL method invocation example
    Spring EL hello world example
    Spring @PostConstruct and @PreDestroy example
    Spring init-method and destroy-method example
    Define custom @Required-style annotation in Spring
    Spring dependency checking with @Required Annotation
    Spring properties dependency checking
    UESTC 883 方老师与两个串 --二分搜索+DP
    UESTC 882 冬马党 --状压DP
  • 原文地址:https://www.cnblogs.com/wanzitou/p/5557355.html
Copyright © 2011-2022 走看看