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.程序截图

  • 相关阅读:
    数据库中的字段NULL值和''
    MySQL Notifier
    LeetCode算法题-Delete Node in a Linked List(Java实现)
    LeetCode算法题-Lowest Common Ancestor of a Binary Search Tree
    LeetCode算法题-Palindrome Linked List(Java实现)
    LeetCode算法题-Implement Queue Using Stacks(Java实现)
    LeetCode算法题-Power Of Two(Java实现)
    LeetCode算法题-Invert Binary Tree
    LeetCode算法题-Implement Stack Using Queues
    LeetCode算法题-Contains Duplicate II(Java实现)
  • 原文地址:https://www.cnblogs.com/wanzitou/p/5557355.html
Copyright © 2011-2022 走看看