zoukankan      html  css  js  c++  java
  • FZU 2102 Solve equation(水,进制转化)&& FZU 2111(贪心,交换使数字最小)

    C
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    You are given two positive integers A and B in Base C. For the equation:

    A=k*B+d

    We know there always existing many non-negative pairs (k, d) that satisfy the equation above. Now in this problem, we want to maximize k.

    For example, A="123" and B="100", C=10. So both A and B are in Base 10. Then we have:

    (1) A=0*B+123

    (2) A=1*B+23

    As we want to maximize k, we finally get one solution: (1, 23)

    The range of C is between 2 and 16, and we use 'a', 'b', 'c', 'd', 'e', 'f' to represent 10, 11, 12, 13, 14, 15, respectively.

    Input

    The first line of the input contains an integer T (T≤10), indicating the number of test cases.

    Then T cases, for any case, only 3 positive integers A, B and C (2≤C≤16) in a single line. You can assume that in Base 10, both A and B is less than 2^31.

    Output

    For each test case, output the solution “(k,d)” to the equation in Base 10.

    Sample Input

    3
    2bc 33f 16
    123 100 10
    1 1 2

    Sample Output

    (0,700)
    (1,23)
    (1,0)
    题解:
    A=k*B+d找到k,d使等式成立,并且k最大,A,B是C进制的;很简单, k = (A - d)/B;
    k最大, k = floor(A/B);进制转化成10进制就好了
    代码:
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    bool is_digit(char c){
        if(c >= '0' && c <= '9')
            return true;
        return false;
    }
    int cj(char *s, int C){
        int x = 0;
    //    cout << s << " " << C << endl;
        for(int i = 0; s[i]; i++){
            int t = is_digit(s[i]) ? s[i] - '0':s[i] - 'a' + 10;
            x = x * C + t;
        }
    //    cout << "x = " << x << endl;
        return x;
    }
    int main(){
        int T;
        scanf("%d", &T);
        char s[110], s1[110];
        while(T--){
            int A, B, C;
            scanf("%s%s%d", s, s1, &C);
            A = cj(s, C);
            B = cj(s1, C);
            int r;
            r = A/B;
            int l = A - r * B;
            printf("(%d,%d)
    ", r,l);
        }
        return 0;
    }
    H
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    Now you are given one non-negative integer n in 10-base notation, it will only contain digits ('0'-'9'). You are allowed to choose 2 integers i and j, such that: i!=j, 1≤i<j≤|n|, here |n| means the length of n’s 10-base notation. Then we can swap n[i] and n[j].

    For example, n=9012, we choose i=1, j=3, then we swap n[1] and n[3], then we get 1092, which is smaller than the original n.

    Now you are allowed to operate at most M times, so what is the smallest number you can get after the operation(s)?

    Please note that in this problem, leading zero is not allowed!

    Input

    The first line of the input contains an integer T (T≤100), indicating the number of test cases.

    Then T cases, for any case, only 2 integers n and M (0≤n<10^1000, 0≤M≤100) in a single line.

    Output

    For each test case, output the minimum number we can get after no more than M operations.

    Sample Input

    3 9012 0 9012 1 9012 2

    Sample Output

    9012 1092 1029
    题解:
    水贪心,交换不能出现前缀0;
    代码:
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    using namespace std;
    const int MAXN = 1010;
    char s[MAXN];
    int main(){
        int T, M;
        scanf("%d", &T);
        while(T--){
            scanf("%s%d", s, &M);
                for(int i = 0; s[i]; i++){
                    for(int j = i + 1; s[j]; j++){
                        char pos = i;
                        if(M <= 0)break;
                        if(i == 0){
                            if(s[j] != '0' && s[j] < s[pos]){
                                pos = j;
                            }
                        }
                        else{
                            if(s[j] < s[pos]){
                                pos = j;
                            }
                        }
                        if(pos != i){
                            swap(s[pos], s[i]);
                            M--;
                        }
                    }
                }
                printf("%s
    ", s);
        }
        return 0;
    }
  • 相关阅读:
    Oracle建立表空间和用户
    Session详解
    Spring中AOP方式实现多数据源切换
    Filter(过滤器)学习
    不用加号运算
    数字转化为十六进制
    1px像素问题(移动端经典问题)
    对postcss-plugin-px2rem的研究
    npm cache clean --force
    对async/await的研究
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5468162.html
Copyright © 2011-2022 走看看