pat 1009 - 1010

1009. Product of Polynomials (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 1002题目很像,pat 1002是多项式的加法,而1009这道是多项式的乘法,str1 * str2,最后的结果长度会变大。我们只需要将str1中每一个非0的项与str2中每一个非0项都得相乘。

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

double str1[1002], str2[1002];
double res[2004];

int main()
{
	freopen("E://input.txt", "r", stdin);
	
	memset(str1, 0, sizeof(str1));
	memset(str2, 0, sizeof(str2));
	memset(res, 0, sizeof(res));
	
	int n, tmp;
	
	scanf("%d", &n);
	for(int i = 0; i < n; i ++)
	{
		scanf("%d", &tmp);
		scanf("%lf", &str1[tmp]);
	}
	
	scanf("%d", &n);
	for(int i = 0; i < n; i ++)
	{
		scanf("%d", &tmp);
		scanf("%lf", &str2[tmp]);
	}
	
	for(int i = 0; i < 1002; i ++)
	{
		for(int j = 0; j < 1002; j ++)
		{
			res[i+j] = res[i+j] + str1[i]*str2[j];
			
		}
	}
	
	int count = 0;
	
	for(int i = 2004; i >= 0; i --)
	{
		if(res[i] != 0)
			count ++;
	}
	printf("%d", count);
	
	
	for(int i = 2004; i >= 0; i --)
	{
		if(res[i] != 0)
		{
			count --;
			printf(" %d %.1f", i, res[i]);
		}
	}
	
	printf("\n");
	return 0;
}

1010. Radix (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
题目主要考察不同进制之间的转换,给出N1和N2, tag表示后面的radix是前面N1还是N2的多少进制,如6 110 1 10,表示6的10进制。 和110(当110为多少进制的时候它和6相等)。首先我得会把其中一个已知数转换成10进制后为base。然后另外一个数看其有多少位,如果只有一位为x,并且与base相等。则此时应该输出x+1,当有2位以上的时候,我们采用二分查找的方法去找出是多少进制。最大进制是这个就是base,最小呢,就是比num中每位数中最大的大1.eg.01257这个数最少是8进制。具体看代码如下:

#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;

long long to_radix(string num, long long radix)
{
	long long res = 0;
	
	for(int i = 0; i < num.length(); i ++)
	{
		int x;
		if(num[i] >= '0' && num[i] <= '9')
			x = num[i] - '0';
		else
			x = num[i] - 'a' + 10;
		
		res = res * radix + x;
	}
	return res;
}

int main()
{
	freopen("E://input.txt", "r", stdin);
	string str1, str2, num;
	int tag;
	long long radix, base;
	
	cin>>str1>>str2>>tag>>radix;
	
	if(tag == 1)
	{
		num = str2;
		base = to_radix(str1, radix);
	}
	else
	{
		num = str1;
		base = to_radix(str2, radix);
	}
	
	//如果num只有一位 
	if(num.length() == 1)
	{
		int x;
		if(num[0] >= '0' && num[0] <= '9')
			x =	num[0] - '0';
		else
			x = num[0] - 'a' + 10;
		
		if(x == base)
			cout<<x+1<<endl;
		else
			cout<<"Impossible"<<endl;
		
		return 0;
	}
		
	long long minRadix = 0, maxRadix = base;
	//最小的Radinx肯定比num中大,eg.  078最少是9进制,那么最大呢,最大情况就是num就是base-1,这样的话最大maxRadix为base 
	for(int i = 0; i < num.length(); i ++)
	{
		int x;
		if(num[i] >= '0' && num[i] <= '9')
			x = num[i] - '0';
		else 
			x = num[i] - 'a' + 10;
		
		if(x + 1 > minRadix)
			minRadix = x + 1;
	}
	
	
	//采用二分查找方法找到基数 
	while(minRadix <= maxRadix)
	{
		long long middle = (minRadix + maxRadix) / 2;
		long long x = to_radix(num, middle);
		
		if(x == base)
		{
			cout<<middle<<endl;
			return 0;
		}
		else if(x > base || x < 0)
			maxRadix = middle - 1;
		else
			minRadix = middle + 1;
	}
	cout<<"Impossible"<<endl;
	
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值