zoukankan      html  css  js  c++  java
  • CF 990A. Commentary Boxes【数学/模拟】

    【链接】:CF
    【题意】:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价。
    【分析】:分情况讨论,自己多举几个栗子。
    【代码】:

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    using namespace std;
    
    typedef long long ll;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const int INF = 0x3f3f3f3f;
    const ll LNF = 1e18;
    const int maxn = 1e3 + 100;
    const int maxm = 100;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    //const int dx[] = {-1,1,0,0,1,1,-1,-1};
    //const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dx[] = {-1,0,1,0};
    int dy[] = {0,1,0,-1};
    //        ио/ср/об/вС
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int dir[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} };
    //int dir[4][2]= {{-1,0},{0,1},{1,0},{0,-1}};
    
    ll n,m;
    ll a,b;
    //对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价
    int main()
    {
        while(cin>>n>>m>>a>>b)
        {
            ll ans;
            if(n%m==0)
            {
                cout<<"0"<<endl;
                continue;
            }
            else
            {
                if(n>m)
                {
                    ans=min((n-m*(n/m))*b,(((n/m+1)*m-n)*a));
                }
                else ans=min(n*b,(m-n)*a);
            }
            cout<<ans<<endl;
        }
    }
    /*
    9 7
    9/7=1
    9%7=2
    ↑
    
    ↓
    7-2*2*2=1
    4 8 1 5
    
    7/2=3
    7%2=1
    
    8 3 3 7
    8/3=2
    8%3=2
    2*7=14↓
    1*3=3↑
    
    9 6
    6 6
    12 6
    9/6=1
    9%6=3
    */
    
  • 相关阅读:
    Algs4-2.2.1给出原地归并排序merge的排序过程
    Algs4-2.2.2给出自顶向下归并排序的排序过程
    Algs4-2.1.38不同类型的元素
    Algs4-2.1.36不均匀的数据
    Algs4-2.1.37部分有序
    Algs4-2.1.35不均匀的概率分布
    Algs4-2.1.34罕见情况
    升级python到2.7版本pip不可用
    随机验证码
    python文件操作
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9212917.html
Copyright © 2011-2022 走看看