博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FZU 2102 Solve equation(水,进制转化)&& FZU 2111(贪心,交换使数字最小)
阅读量:5034 次
发布时间:2019-06-12

本文共 4093 字,大约阅读时间需要 13 分钟。

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
#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;}
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
#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;}

 

转载于:https://www.cnblogs.com/handsomecui/p/5468162.html

你可能感兴趣的文章
MATLAB GUI程序设计中使文本框接收多行输入的方法
查看>>
全文检索-Elasticsearch (四) elasticsearch.net 客户端
查看>>
Oracle DBMS_SESSION
查看>>
sublime复制当前行到下一行
查看>>
WPF 3D变换应用
查看>>
luogu4012 深海机器人问题 网络流
查看>>
android 拍照上传照片
查看>>
ArchLinux安装开源VMware Tools
查看>>
系统用户分析模型
查看>>
DB2 锁升级示例1
查看>>
16.RDD实战
查看>>
MainFrame知识小结(20120210)—dfsort/syncsort中的数据类型
查看>>
jsp题库 (一)小测(25/21)
查看>>
D - Flip tile
查看>>
Java连接RabbitMQ之创建连接
查看>>
开户vim编程之--cscope支持
查看>>
python数据类型图解
查看>>
C#微信登录-手机网站APP应用
查看>>
HTML5实践 -- iPhone Safari Viewport Scaling Bug
查看>>
一位数据挖掘成功人士 给 数据挖掘在读研究生 的建议
查看>>