您的位置:首页 > 其它

PAT (Advanced Level) Practise 1001-1010

2013-06-07 00:00 831 查看

1001. A+B Format (20)

Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input
-1000000 9

Sample Output
-999,991

分析:关键点在于输出的时候每三个数字加一个逗号,代码中输入两个数字之后求得和,再转换为字符串,转换时每逆序存入数组中,每3个数字加一个逗号,这样数字“123456”就可以变为“654,321”,再在最后根据前面求得的和判断是否需要再加一个“-”

代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int a = 0;
int b = 0;
int c = 0;
int tmp = 0;
int len = 0;
int i = 0;
int j = 0;
char result[10];

while(scanf("%d %d", &a, &b) != EOF){
c = a + b;
if(c < 0){
tmp = -c;
}else{
tmp = c;
}

while(tmp/10 != 0){
result[j++] = tmp % 10 + '0';
len++;
if(len%3 == 0){
result[j++] = ',';
}
tmp = tmp /10;
}
result[j++] = tmp % 10 + '0';
if(c < 0 ){
result[j++] = '-';
}

for(i = j-1; i >=0; i--){
printf("%c", result);
}
printf("\n");

}
return 0;
}

以上为第1001题!

==============================================================

==============================================================

1002. A+B for Polynomials (25)

This time, you are supposed to find A+B where A and B are two polynomials.

Input

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.

Output

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input
2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output
3 2 1.5 1 2.9 0 3.2

分析:建数组coeff[1001], expo[1001],没输入一组数据coefftmp, expotmp,则将expo[expotmp]置1,并将coeff[expotmp]赋值为coefftmp,即将expotmp当做索引。缺点是浪费空间。

注意:输入数据时,需要判断输入的coefftmp是否为0,此外,当前后两组数据有相同expo指数时,还需判断系数之和是否也为0,若为0,则对应的expo[expotmp]必须置0,结果中不予输出,此时还需要注意,最后数据个数中,若相加之后系数为0,则size--。

代码:

#include <stdio.h>
#include <stdlib.h>

#define MaxN 1001
#define MaxK 10

int main()
{

int K1;
int K2;
float coeff[MaxN];  //coefficient
int expo[MaxN];  //exponential
float coefftmp;
int expotmp;
int i, j, k;
int isfirst1;
int isfirst2;
int index;
int size = 0;

for(i = 0; i < MaxN; i++){
coeff[i] = 0.0;
expo[i] = 0;
}

scanf("%d", &K1);
for(i = 0; i < K1; i++){
scanf("%d%f", &expotmp, &coefftmp);
if(coefftmp != 0.0){
size++;
expo[expotmp] = 1;
coeff[expotmp] = coefftmp;
}
}

scanf("%d", &K2);
for(j = 0; j < K2; j++){
scanf("%d%f", &expotmp, &coefftmp);
if(expo[expotmp] == 1){
coeff[expotmp] += coefftmp;
if(coeff[expotmp] == 0.0){  //rejudge if coeff of sum equals to 0?
expo[expotmp] = 0;
size--;
}
}else{
if(coefftmp != 0){
expo[expotmp] = 1;
coeff[expotmp] = coefftmp;
size++;
}
}
}

printf("%d", size);
i = 0;
for(i = MaxN - 1; i >= 0; i--){
if(expo[i] != 0){
printf(" %d %.1f", i, coeff[i]);
}
}
printf("\n");
return 0;
}

以上为第1002题!

==============================================================

1003. Emergency (25)★★★★★

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input
5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output
2 4

分析:题意是要求单源点对最短路径数和最大的点权重之和。知道要用dijskra算法,但为了求最短路径数,还得用深搜,依次把点加入集合中,若路径长度=最短路径值时,路径数+1。同时求出最大的点权重之和。具体看参考来源。第一次用到dijkstra,之前看书的时候只用邻接表实现了一下,写的时候太慢了,可学习这种写法,而且dfs也不怎么熟练,这题之后稍微有点理解了,多用。

分析DFS的时候需哟啊递归分析,比较麻烦。看下图



代码:

#include <stdio.h>
#include <stdlib.h>

#define INFINITY 100000
#define MAXNUM 500
int map[MAXNUM][MAXNUM];
int team[MAXNUM];
int isVisit[MAXNUM];
int dist[MAXNUM];
int ans;
int maxTeamNum = 0;

void init()
{
int i, j;
for(i = 0; i < MAXNUM; i++){
isVisit[i] = 0;
team[i] = 0;
dist[i] = INFINITY;
for(j = 0; j < MAXNUM; j++){
if(j != i){
map[i][j] = INFINITY;
map[j][i] = INFINITY;
}
}
}
}

void dijskra(int n, int source)
{

int i,j;
int minDis = INFINITY;
int index = 0;

for(i=0; i<n; i++){
dist[i] = map[source][i];
}

isVisit[source] = 1;//将其加入已访问点的集合

for(i = 0; i < n-1; i++){
minDis = INFINITY;
index = 0;
for(j = 0; j < n; j++){
if(!isVisit[j] && dist[j] < minDis){//找到最小距离点
minDis = dist[j];
index = j;
}
}

isVisit[index] = 1;//将其加入已访问点的集合
for(j = 0; j < n; j++){//更新源点与每个点之间的距离
if(map[index][j] < INFINITY && dist[j] > dist[index] + map[index][j])
dist[j] = dist[index] + map[index][j];
}
}
}

void dfs(int n,int cId,int dest,int curDis,int curTeamNum)
{
int i;

isVisit[cId] = 1;
if(cId == dest){
if(curDis == dist[dest]){
ans++;//最短路径数+1
if(curTeamNum > maxTeamNum)
maxTeamNum = curTeamNum;
}
return;
}

if(curDis > dist[dest]){//当前的路径长度已经超过最短路径,就没有必要继续搜索了。
return;
}

for(i = 0; i < n; i++){
if(!isVisit[i] && map[cId][i] < INFINITY){//城市i未被访问过,且cId到i连通
dfs(n, i, dest, curDis + map[cId][i], curTeamNum + team[i]);
isVisit[i]=0;
}
}
}

int main()
{
int n, m, source, dest;
int c1, c2, length;
int i;

scanf("%d%d%d%d", &n, &m, &source, &dest);

init();
for(i = 0; i < n; i++){
scanf("%d", &team[i]);
}

for(i = 0; i < m; i++){
scanf("%d%d%d", &c1, &c2, &length);
map[c1][c2] = length;
map[c2][c1] = length;
}

dijskra(n, source);
for(i = 0; i < n; i++){
isVisit[i] = 0;
}

dfs(n, source, dest, 0, team[source]);
printf("%d %d\n", ans, maxTeamNum);
return 0;
}

参考:http://blog.csdn.net/jjike/article/details/8615653

以上为第1003题!

==============================================================

1004. Counting Leaves (30)

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input

Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:

ID K ID[1] ID[2] ... ID[K]
where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.
Output

For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output "0 1" in a line.

Sample Input
2 1
01 1 02

Sample Output
0 1


分析:分析:开始用了树的孩子节点表示法,但是总有两个测试点不能通过,搞不明白

后来参照了参考代码,忽然发现人家写的时候貌似都很少用到复杂的数据结构,

都用数组什么的就能实现了,以后多留意一下。

用一个parent[]数组保存父节点,再输入m行后,每次都可以将id节点标为non-leaf,

再对每个节点(leaf节点)计算他们的层数,相应层数的总的叶节点数目++即可。

注意:题目可能默认节点是按照从01递增来排序的

代码:

#include <stdio.h>
#include <stdlib.h>
#define MAXNUM 100

int level[MAXNUM];
int parent[MAXNUM];
int isleaf[MAXNUM];
int maxLevel = 0;

void findLevel(int x)
{
int r = x;
int tmp = 0;  //叶节点的层数,从0开始

while(parent[r] != r){
tmp++;
r = parent[r];
}
level[tmp]++;  //所在层数的叶节点数+1
if(tmp >= maxLevel){
maxLevel = tmp;
}
}

//节点号应该默认是从01递增
int main()
{
int i, j;
int n, m, id, k, tmp;

for(i = 0; i < MAXNUM; i++){
level[i] = 0;
parent[i] = i;
isleaf[i] = 1;
}

scanf("%d%d", &n, &m);
if(n == 1){
printf("1\n");
return 0;
}else{
for(i = 0; i < m; i++){  //input m lines
scanf("%d%d", &id, &k);
for(j = 0; j < k; j++){
scanf("%d", &tmp);
parent[tmp] = id;  //set the parent of j to id
}
isleaf[id] = 0;  //node id is not leaf again
}

for(i = 1; i <= n; i++){
if(isleaf[i] == 1){
findLevel(i);
}
}

printf("%d", level[0]);
for(i = 1; i <= maxLevel; i++){
printf(" %d", level[i]);
}
printf("\n");
return 0;
}
}


以上为第1004题!

==============================================================

1005. Spell It Right (20)

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:
12345

Sample Output:
one five


分析:类似于PAT basiclevel的 1002.写出这个数(20)
http://my.oschina.net/zjlaobusi/blog/136186#OSC_h1_2

代码:

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 101

int main()
{
char numberSeq[MAX_SIZE];
char *array[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
char result[10];  //max sum of 100 digit is less than 900
int i = 0;
int j = 0;
int sum = 0;
int size = 0;
int tmp = 0;

//read in data as string,
size = read(fileno(stdin), numberSeq, MAX_SIZE) - 1;
for(i = 0; i < size; i++)
{
sum += (numberSeq[i] - '0');
}

//transform sum(int) to a reversed string
//456-->"654"
tmp = sum;
while(tmp/10 != 0){
result[j] = tmp % 10 + '0';
j++;
tmp /= 10;
}
result[j] = tmp%10 + '0';

//print the string reversely
//"654"-->4(si)-->5"wu"->6"liu"
printf("%s", array[result[j]-'0']);
for(i = j-1; i >= 0; i--)
{
printf(" %s", array[result[i]-'0']);
}
printf("\n");
return 0;
}

以上为第1005题!

==============================================================

1006. Sign In and Sign Out (25)

At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.

Input Specification:

Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

ID_number Sign_in_time Sign_out_time

where times are given in the format HH:MM:SS, and ID number is a string with no more than 15 characters.

Output Specification:

For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

Sample Input:
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

Sample Output:
SC3021234 CS301133
分析:类似于PAT Basic Level的第1004.成绩排名(20)
http://my.oschina.net/zjlaobusi/blog/136186#OSC_h1_4

代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int m;
int i;
char Locker[16];
char Unlocker[16];
char student[16];
char startTime[9];
char endTime[9];
char earlistTime[9] = "23:59:59";
char latestTime[9] = "00:00:00";
int result = 0;

scanf("%d", &m);
for(i = 0; i < m; i++){
scanf("%s%s%s", student, startTime, endTime);
result = strncmp(startTime, earlistTime, 8);
if(result <= 0){  //startTime < earlistTime
strncpy(earlistTime, startTime, sizeof(startTime));
strncpy(Unlocker, student, sizeof(student));
}

result = strncmp(endTime, latestTime, 8);
if(result >= 0){
strncpy(latestTime, endTime, sizeof(endTime));
strncpy(Locker, student, sizeof(student));
}
}

printf("%s %s\n", Unlocker, Locker);

return 0;
}


以上为第1006题!

==============================================================

1007. Maximum Subsequence Sum (25)

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The [i]Maximum Subsequence
is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:
10 1 4

分析:最大子序列和,dsaac(《数据结构与算法分析》)中有,可参考 ,可用递归方法。 但是此题有点变化,不仅需要求出最大和,还需要求出最大和时的首序列元素以及尾序列元素。还需注意题目要求:若有多个相同的最大和,输出第一组,若所有元素全部为负数,则输出0以及数组的首元素以及尾元素。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAXNUM 10000

int main()
{
int a[MAXNUM];
int i, j;
int maxfront = 0;
int maxrear = 0;
int front = 0;
int rear = 0;
int sum = -1;
int maxsum = -1;
int n;

scanf("%d", &n);
for(i = 0; i < n; i++){
scanf("%d", &a[i]);
}

for(i = 0; i < n; i++){
if(sum < 0){
sum = a[i];
front = i;
rear = i;
}else{
sum += a[i];
rear = i;
}

if(sum > maxsum){
maxsum = sum;
maxfront = front;
maxrear = rear;
}
}

if(maxsum < 0){
printf("0 %d %d\n", a[0], a[n-1]);
}else{
printf("%d %d %d\n", maxsum, a[maxfront], a[maxrear]);
}
return 0;
}

以上为第1007题!

==============================================================

1008. Elevator (20)

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:
3 2 3 1

Sample Output:
41

分析:此题略简单,给出电梯的上下路径,结合停留时间以及上下时间,计算总共花费时间。

T = n*5 + up*6 + down*4(up为总共上升层数,down为总共下降层数,n为需要停留的层数)

代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define MAXNUM 10000

int main()
{
int prev = 0, curr = 0;
int n, i;
int up = 0;
int down = 0;
int totaltime;

scanf("%d", &n);
i = n;
while(i--){
scanf("%d", &curr);
if(curr > prev){
up = curr - prev + up;
}else if(curr < prev){
down = prev - curr + down;
}
prev = curr;
}

totaltime = n*5 + up*6 + down*4;
printf("%d\n", totaltime);
return 0;
}


以上为第1008题!

==============================================================

1009. Product of Polynomials (25)

This time, you are supposed to find A*B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < ... < N2 < N1 <=1000.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input
2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output
3 3 3.6 2 6.0 1 1.6
分析:参考PAT advanced level 1002:A+B。虽然数组比较占空间,但是便捷。
代码:

#include <stdio.h>
#include <stdlib.h>

#define MaxN 1000
#define MaxK 10

int main()
{

int K1;
int K2;
double coeff1[MaxN + 1];  //coefficient
int expo1[MaxN + 1];  //exponential
double coeff2[MaxN + 1];
int expo2[MaxN + 1];
double coeffFinal[2*MaxN + 2];
int expoFinal[2*MaxN + 2];
double coefftmp;
int expotmp;
int i, j, k;
int size = 0;

for(i = 0; i < MaxN+1; i++){
coeff1[i] = 0.0;
expo1[i] = 0;
coeff2[i] = 0.0;
expo2[i] = 0;
coeffFinal[i] = 0.0;
coeffFinal[i+MaxN+1] = 0.0;
expoFinal[i] = 0;
expoFinal[i+MaxN+1] = 0;
}

scanf("%d", &K1);
for(i = 0; i < K1; i++){
scanf("%d%lf", &expotmp, &coefftmp);
if(coefftmp != 0.0){
//size++;
expo1[expotmp] = 1;
coeff1[expotmp] = coefftmp;
}
}

scanf("%d", &K2);
for(j = 0; j < K2; j++){
scanf("%d%lf", &expotmp, &coefftmp);
if(coefftmp != 0.0){
expo2[expotmp] = 1;
coeff2[expotmp] = coefftmp;
}
}

for(i = 0; i < MaxN + 1; i++){
if(expo1[i] == 1){
for(j = 0; j < MaxN + 1; j++){
if(expo2[j] == 1){
if(expoFinal[i + j] == 0){
size++;
expoFinal[i + j] = 1;
}

coeffFinal[i + j] += (coeff1[i] * coeff2[j]);
if(coeffFinal[i + j] == 0.0000){
size--;
expoFinal[i + j] = 0;
}
}

}
}

}

printf("%d", size);
i = 0;
for(i = 2*MaxN; i >= 0; i--){
if(expoFinal[i] != 0){
printf(" %d %.1f", i, coeffFinal[i]);
}
}
printf("\n");
return 0;
}

以上为第1009题!

==============================================================

1010. Radix (25)★★★★★

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number "radix" is the radix of N1 if "tag" is 1, or of N2 if "tag" is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print "Impossible". If the solution is not unique, output the smallest possible radix.

Sample Input 1:
6 110 1 10

Sample Output 1:
2

Sample Input 2:
1 ab 1 2

Sample Output 2:
Impossible
分析:太恶心了这题,从其他博客上参考来的思路。
一开始可能会有个误区,认为radix的范围是[2~35](因为输入数字中每个数字取值为0~9,10~35),但事实上radix可以非常大,甚至超过long long取值范围,比如例子:9223372036854775806 10 1 10,此时数字10的radix即为 92233720368547758076。

然偶就是如何求这个radix了,radix的下界就是没有给出radix的那个数字每一位最大值+1,而如何求上界呢?刚好看上面一段话,上界可以取给出了radix的那个数字。但是这里有个特例,比如 11 b 1 10,此时若上界取为给出了radix的那个数字,即11,明显不对,但是若取为没有给出radix的那个数字的每一位中最大值+1,即radix = b+1=12,结果正确。

再然后就是从radix的下界到上界之间,不断尝试哪个radix满足要求。若依次尝试,时间要求不满足,转而用二分法来检验就好。

前提:给出radix的那一个数字最好不超过long long范围。

思路:http://biaobiaoqi.me/blog/2013/07/31/pat-1001-1010-solutions/

代码参考:http://blog.csdn.net/jjike/article/details/8589151下面那份ac的代码

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int getValue(char c)
{
if(c >= 'a' && c <= 'z'){
return c - 'a' + 10;
}else if(c >= '0' && c <= '9'){
return c - '0';
}
}
long long myAtoI(char *str, int n, long long radix)
{
long long number = 0;
int i;
long long tmp = 0;
long long r = 1;

for(i = n - 1; i >= 0; i--){
number += getValue(str[i]) * r;
r *= radix;
}

return number;
}

long long findLowRadix(char *s)
{
int len = strlen(s);
long long low = 0;
long long num = 0;
long long i;

for(i = len - 1; i >= 0; i--){
num = getValue(s[i]);
if(num + 1 > low){
low = num + 1;
}
}

return low;

}

//s > target, return 1
//s == target, return 0
//s < target, return -1
int compare(char *s, long long radix, long long target)
{
long long len = strlen(s);
long long m = 1;
long long num = 0;
long long sum = 0;
long long i;

for(i = len - 1; i >= 0; i--){
num = getValue(s[i]);
sum += num*m;
m *= radix;
if(sum > target){ //avoid overflow
return 1;
}
}

if(sum > target){
return 1;
}else if(sum < target){
return -1;
}else{
return 0;
}
}

long long binarySearch(char *s, long long low, long long high, long long target)
{
long long mid = low;
long long tmp;

while(low <= high){
tmp = compare(s, mid, target);
if(tmp > 0){ //s > target, decrease the radix of s
high = mid - 1;
}else if(tmp < 0){  //s < target, increase the radix of s
low = mid + 1;
}else{  //s ==  target, return the radix
return mid;
}
mid = (low + high)/2;
}

return -1;
}

int main()
{
char a[11];
char b[11];
long long radix, radix1, radix2;
long long least, most;
long long number1, number2;
long long res;
int tag;
int i, j;

scanf("%s%s%d%lld", a, b, &tag, &radix);
if(tag == 1){
radix1 = radix;
number1 = myAtoI(a, strlen(a), radix1);
least = findLowRadix(b);
most = (number1 + 1 > least + 1)?(number1 + 1):(least + 1);
res = binarySearch(b, least, most, number1);

}else if(tag == 2){
radix2 = radix;
number2 = myAtoI(b, strlen(b), radix2);
least = findLowRadix(a);
most = (number2 + 1 > least + 1)?(number2 + 1):(least + 1);
res = binarySearch(a, least, most, number2);

}

if(res == -1){
printf("Impossible");
}else{
printf("%lld", res);
}
printf("\n");
return 0;
}

以上为第1010题!

==============================================================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT