/*
*时间:2018年4月20日11:06:04-2018年4月20日11:32:38
*题目:1046.Shortest Distance
*分数:20
*编译器:g++
*题目描述:
The task is really simple: given N exits on a highway which forms a simple cycle, you are
supposed to tell the shortest distance between any pair of exits.
Input Specification:
Each input file contains one test case. For each case, the first line contains an integer N
(in [3, 105]), followed by N integer distances D1 D2 ... DN, where Di is the distance between
the i-th and the (i+1)-st exits, and DN is between the N-th and the 1st exits. All the numbers
in a line are separated by a space. The second line gives a positive integer M (<=104),
with M lines follow, each contains a pair of exit numbers, provided that the exits are
numbered from 1 to N. It is guaranteed that the total round trip distance is
no more than 107.
Output Specification:
For each test case, print your results in M lines, each contains the shortest distance
between the corresponding given pair of exits.
Sample Input:
5 1 2 4 14 9
3
1 3
2 5
4 1
Sample Output:
3
10
7
*/
//思路:由于是cycle所以路径是有两个方向,称路标从小打到为正方向
//计算正方向的路径,再和总路径减去正方向的路径做对比,哪个小取哪个
//为了防止超时,将路径和做一个组数,表示从1到当前路标下的路径之和
#include <iostream>
using namespace std;
#include <math.h>
int main()
{
int N, M;
cin>>N;
int sum[N+1];
sum[0] = 0;
int distance[N];
for (int i=0; i<N; i++)
{
cin>>distance[i];
sum[i+1] = sum[i] + distance[i];//sum[i]表示1到i-1的路径之和
}
cin>>M;
int exits[2 * M];
for (int i=0; i<2*M; i++)
cin>>exits[i];
for (int i=0; i<M; i++)
{
int a = max(exits[2*i], exits[2*i+1]);
int b = min(exits[2*i], exits[2*i+1]);
int temp = sum[a-1] - sum[b-1];//正向路径
if (temp < sum[N] - temp) cout<<temp<<endl;
else cout<<sum[N] - temp<<endl;
}
return 0;
}
/*
*时间:s2018年4月20日12:52:03-2018年4月20日13:18:47
*题目:1065.A+B and C (64bit)
*分数:20
*编译器:g++
*题目描述:
Given three integers A, B and C in [-2^63, 2^63], you are supposed to tell whether A+B > C.
Input Specification:
The first line of the input gives the positive number of test cases, T (<=10). Then T test
cases follow, each consists of a single line containing three integers A, B and C,
separated by single spaces.
Output Specification:
For each test case, output in one line "Case #X: true" if A+B>C, or "Case #X: false"
otherwise, where X is the case number (starting from 1).
Sample Input:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false
*/
//这题的关键就是long long 类型装不下了怎么处理,long long的范围是[-2^63, 2^63)
//由于A, B and C in [-2^63, 2^63],所以A+B最大就是2^64,用long long装A,B的话会超出范围
//1、如果A和B都是正数,那么最大值为2^64-2,超出范围之后就为[-2^63, -2]即<0,所以A B 都是正数,和为负数就判断为true(C不可能超出范围一定比A+B小)
//2、如果A和B都是负数,那么最大值为-2^64,超出范围之后就为[0,2^63]即>=0,所以A B 都是负数,和>=0就判断为false(C不可能超出范围一定比A+B小)
//3、其余的按正常情况判断
//以上思路参考《算法笔记》
#include <iostream>
using namespace std;
int main()
{
int T;
cin>>T;
long long num[T][3];
for (int i=0; i<T; i++)
{
cin>>num[i][0]>>num[i][1]>>num[i][2];
long long res = num[i][1] + num[i][0]; //很关键,和必须用long long类型,也可以加起来之后强制类型转换
if (num[i][0]>0 && num[i][1]>0 && res<0) cout<<"Case #"<<i+1<<": true"<<endl;//情况1
else if (num[i][0]<0 && num[i][1]<0 && res>=0) cout<<"Case #"<<i+1<<": false"<<endl;//情况2
else if (res > num[i][2]) cout<<"Case #"<<i+1<<": true"<<endl;//情况3
else cout<<"Case #"<<i+1<<": false"<<endl;
}
return 0;
}
/*
*时间:2018年4月21日16:41:04-2018年4月21日17:29:25
*题目:1002.A+B for Polynomials
*分数:20
*编译器:g++
*题目描述:
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
*/
//思路:数组的散列思想,数组下表即次数,数组值为这个这个次数的项的系数
#include <iostream>
using namespace std;
#include <math.h>
#include <stdio.h>
int main()
{
double coe[1010] = {0};
int M, N;
cin>>M;
double polyA[2*M];
for (int i=0; i<2*M; i++)
cin>>polyA[i];
cin>>N;
double polyB[2*N];
for (int i=0; i<2*N; i++)
cin>>polyB[i];
for (int i=0; i<max(N, M); i++)//将大的数组遍历完全
{
if (i < 2 * M) coe[(int)polyA[2*i]] += (double)polyA[2*i+1];//i<2M要注意,不然数组会越界
if (i < 2 * N) coe[(int)polyB[2*i]] += (double)polyB[2*i+1];//与上面类似
}
int len = 0;
for (int i=0; i<1010; i++)//非零项的个数
{
if (coe[i] != 0) len++;
//cout<<i<<" "<<coe[i]<<endl;
}
if (len == 0) cout<<"0";
else cout<<len<<" ";
int temp = 0;
for (int i=1009; i>=0; i--)
{
if (temp == len - 1 && coe[i] != 0) printf("%d %.1f", i, coe[i]);//小技巧,表示最后一个输出,输出的格式注意一下
else if (coe[i] != 0)
{
printf("%d %.1f ", i, coe[i]);
temp++;
}
}
return 0;
}
/*
*时间:2018年4月21日20:55:35-2018年4月21日21:03:58
*题目:1009.Product of Polynomials
*分数:20
*编译器:g++
*题目描述:
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
*/
//思路:这题在上面一题基础上就核心部分改一下就OK了,五分钟搞定,多项式乘法的法则
#include <iostream>
using namespace std;
#include <math.h>
#include <stdio.h>
int main()
{
double coe[2010] = {0};
int M, N;
cin>>M;
double polyA[2*M];
for (int i=0; i<2*M; i++)
cin>>polyA[i];
cin>>N;
double polyB[2*N];
for (int i=0; i<2*N; i++)
cin>>polyB[i];
for (int i=0; i<M; i++)//这里搞了个双层循环,原来以为会超时的,结果全部AC
{
for (int j=0; j<N; j++)
{
coe[(int)polyA[2*i]+(int)polyB[2*j]] += (double)(polyA[2*i+1] * polyB[2*j+1]);
}
}
int len = 0;
for (int i=0; i<2010; i++)
{
if (coe[i] != 0) len++;
//cout<<i<<" "<<coe[i]<<endl;
}
if (len == 0) cout<<"0";
else cout<<len<<" ";
int temp = 0;
for (int i=2009; i>=0; i--)
{
if (temp == len - 1 && coe[i] != 0) printf("%d %.1f", i, coe[i]);
else if (coe[i] != 0)
{
printf("%d %.1f ", i, coe[i]);
temp++;
}
}
return 0;
}
【PAT甲】A1046/A1065/A1002/A1009
最新推荐文章于 2023-03-16 22:47:48 发布