码迷,mamicode.com
首页 > 其他好文 > 详细

FZOJ 2102 Solve equation

时间:2017-04-24 01:01:18      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:style   tor   printf   each   技术   namespace   represent   use   src   

                                                                                                                                     Problem 2102 Solve equation

Accept: 1097    Submit: 2608
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem 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,给定A,B两个数,求k,d两个值,且A,B的进制有可能是2~16进制中的其中一种
思路:首先将A,B都换算成10进制,再作除法运算。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int base;

int translator(string s) {
    int sum = 0;
    for (int i = 0; i < s.size();i++) {
        if (s[i] >= 0&&s[i] <= 9)
            sum = sum*base + s[i] - 0;
        else
            sum = sum*base + s[i] - a + 10;
  }
    return sum;
}

int main() {
    int T;
    scanf("%d",&T);
    while (T--) {
        string s1, s2;
        cin >> s1 >> s2;
        scanf("%d",&base);
        int a = translator(s1);
        int b = translator(s2);
        int c = a / b;
        int mod = a%b;
        printf("(%d,%d)\n",c,mod);

    }
    return 0;
}

 

FZOJ 2102 Solve equation

标签:style   tor   printf   each   技术   namespace   represent   use   src   

原文地址:http://www.cnblogs.com/ZefengYao/p/6754741.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!