FZU 2102 Solve equation(水,进制转化)&& FZU 2111(贪心,交换使数字最小)

C

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

Submit Status Practice FZU 2102

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)\n", r,l);
    }
    return 0;
}

H

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

Submit Status Practice FZU 2111

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\n", s);
    }
    return 0;
}
时间: 2024-10-13 23:29:30

FZU 2102 Solve equation(水,进制转化)&& FZU 2111(贪心,交换使数字最小)的相关文章

FZU 2102 Solve equation 多进制计算

Solve equation Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u FZU 2102 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

ACM: FZU 2102 Solve equation - 手速题

FZU 2102   Solve equation Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Practice 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-negati

FZOJ 2102 Solve equation

                                                                                                                                     Problem 2102 Solve equation Accept: 1097    Submit: 2608Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Desc

c语言进制转化

#include <stdio.h> // 进制转化 int main(void) { int i1 = 12; int i2 = 88; int i3 = 0x32C; printf("八进制输出i1 = %o\n", i1); // 14 printf("十六进制输出i2 = %x\n", i2); // 58 printf("十六进制输出i3 = %x\n", i3); // 32c printf("十进制输出i3 =

Java Integer 进制转化的实现(附源码),对模与补码的理解

1.toBinaryString方法的实现 1 public static String toBinaryString(int i) { 2 return toUnsignedString0(i, 1); 3 } 4 private static String toUnsignedString0(int val, int shift) { 5 // assert shift > 0 && shift <=5 : "Illegal shift value";

栈的应用之进制转化

顺序栈的代码: 不再赘述:点击打开链接 //栈的应用-----进制转化 #include"stack.h" int main() { Stack st; InitStack(&st); int select; int num1; //要转化的数 int num2; //转化之后各个位的数 int flag = 1; //控制循环结束 while(flag) { cout<<"****************进制转换*****************&quo

计算机基础知识_进制转化

进制转化 一.任何一个进制转化为10进制的方式 156的十进制可以看做1*10^2 + 5*10^1  +   6*10^0 首先我们看一下156 ,平方,次方等等都是根据后面有多少位决定的,如果用计算机计算,则正好是156 33是一个八进制可以看做3*8^1+3*8^0 次方  换算出来就是10进制 如果你本身是x进制,那么你的数字就看做  num * x^位数  比如我有一个16进制的数字,转为10进制 16进制在计算机中表示方法: 1 2 3 4 5 6 7 8 9 a b c d e f

python数据结构:进制转化探索

*********************************第一部分******************************************************************* *********************************************************************************************************************** # 输入excel的行号,输出对应列的序号(从0开

标识符,进制转化,原反补码等

标识符 Java对包.类.方法.参数.变量等要素命名时使用的字符序列. 规则:***** 1.由字母(含中.英.日.俄等).数字.下划线_和美元符号$组成. 2.不能以数字开头   int 123a=1; 3.区分大小写  int a=1和int A=1是不一样的. 4.长度无限制.(一般编程长度不超过15个字符) 5.不能是Java中的保留字和关键字    int class="1"    int ainta=1 保留字和关键字都有哪些?我们知道以后应该避开它们. 标识符命名习惯:*