FZU Problem 2102 Solve equation 第三屆福建省大學生程序設計競賽

Problem 2102 Solve equation

Accept: 596 Submit: 1320
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

32bc 33f 16123 100 101 1 2

Sample Output

(0,700)(1,23)(1,0)

Source

“高教社杯”第三屆福建省大學生程序設計競賽 


題目大意:給出A和B兩個數基於C的進制,通過A=k*B+d,求出最大的k以及對應的d


pow()函數居然不會用。。醉了  可以pow(double,int)但不能是pow(int,int),要去省賽了,得天天做題了,雖然我很菜。。

#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
char temp[17]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
int judge(char c)
{
	for(int i=0;i<16;i++)
		if(c==temp[i])
			return i;
}
int tran(char a[],int t)
{
	int p=0,j=0;
	for(int i=strlen(a)-1;i>=0;i--)
	{
		p+=judge(a[i])*pow((double)t,j);
		j++;
	}
	return p;
}
int main()
{
	int n,t;
	char a[100],b[100];
	cin>>n;
	while(n--)
	{
		cin>>a>>b>>t;
		int A,B;
		A=tran(a,t);
		B=tran(b,t);
		int k=A/B;
		int d=A%B;
		cout<<"("<<k<<","<<d<<")"<<endl;
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章