Description
You are given two positive integers A and B in Base C. For the equation:
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
Sample Input
Sample Output
#include#include #include #include 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;}
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
Sample Input
Sample Output
#include#include #include #include #include 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;}