【PTA】PAT (Advanced Level) Practice 1007-1010 (language C++)

The ideas are in the code comments.

1007 Maximum Subsequence Sum

Given a sequence of K integers
insert image description here
A continuous subsequence is defined to be
insert image description here
where 1≤i≤j≤K. The 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

translate:

Find out in the second lineThe largest contiguous subsequence and, and the largest contiguous subsequence ofFirstandlast number.
If the largest subsequence is not unique, output the subsequence with the smallest indices i and j.
If all numbers are negative, then its maximum sum is defined as 0, and you should output the first and last number of the entire sequence.

code:

The ideas are in the code comments.

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    
    
    int k;
    cin>>k;
    vector<int> a(k);
    
    //首尾元素的下标
    int left=0,right = k-1;
    
    //sum是临时加和,maxsum是最终的加和,tempindex是left的临时下标
    int sum=0, maxsum=-1, tempindex = 0;
    
    for(int i = 0;i < k; i++){
    
    
        //输入数据
        cin>>a[i];
        sum = sum + a[i];
        
        //当sum小于0,再加其他数字都还是会拉低数值,所以直接舍弃
        if(sum < 0){
    
    
            sum = 0;
            tempindex = i + 1;
        }
        //只有当sum大于0且大于maxsum(等于maxsum,取前者)的情况,maxsum才更新,
        else if(sum > maxsum){
    
    
            maxsum = sum;
            left = tempindex;
            right = i;
        }
    }
    if(maxsum < 0) maxsum = 0;
    cout << maxsum << " " << a[left] << " " << a[right];
    return 0;
}

1008 Elevator

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

translate:

The tallest building in our city has only one elevator. The request list consists of N positive numbers. These numbers indicate which floors the elevator will stop at, in a specific order. It takes 6 seconds for the elevator to go up one floor and 4 seconds for the next floor. The elevator stops for 5 seconds at each stop.

For a given list of requests, calculate the total time taken to complete the requests on the list. The elevator starts at floor 0 and does not need to return to the bottom floor when the request is fulfilled.

code:

#include <iostream>
using namespace std;
int main()
{
    
    
    int a,now = 0,time = 0;
    cin>>a;
    while(cin>>a){
    
    
        //上升
        if(a>now){
    
    
            time = time + 6*(a-now);
        }
        //下降
        else{
    
    
            time = time + 4*(now-a);
        }
        now = a;
        time += 5;//停下来的时间
    }
    cout<<time;
}

1009 Product of Polynomials

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:
insert image description here

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

translate:

Given two polynomials A and B, find the result of A*B.
Input sample:

There are several terms where index 1 corresponds to coefficient 1 and index 2 corresponds to coefficient 2. . .

Request output:

There are several coefficients that are not zero and output the exponents and corresponding coefficients of the results one by one

code:

#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
    
    
    double b, arr[1004]={
    
    0.0} , ans[2001]={
    
    0.0};
    int n1 , n2 , a , cnt=0;
    cin>>n1;
    //输入第一行数据
    for( int i=0 ; i < n1 ; i++ ){
    
    
        cin >> a >> b;
        arr[a] = b;
    }
    //输入第二行数据
    cin>>n2;
    for( int i=0 ; i < n2 ; i++ ){
    
    
        cin >> a >> b;
        //逐项相乘,然后将系数加起来,放入ans数组
        for( int j=0 ; j < 1001 ; j++ ){
    
    
            ans[ j + a ] += arr[ j ] * b;
        }
    }
    //数系数不为0有几项
    for(int i=2000 ; i>=0; i--){
    
    
        if(ans[i]!=0.0)
            cnt++;
    }
    cout<<cnt<<" ";
    //输出系数,逆着输出
    int flag=0;
    for(int i=2000 ; i>=0; i--){
    
    
        if( ans[i]!=0.0 ){
    
    
            cout<< i << setiosflags(ios::fixed)<<setprecision(1) << " " << ans[i];
            flag++;
        }
        //最后一项不输出空格
        if(ans[i]!=0.0 && flag!=cnt)
            cout<<" ";
    }
}

1010 Radix

insert image description here

translate:

For any pair of positive integers n1 and n2, your task is to find the base of one number while the base of the other number is known.

Input specification:
Each input file contains a test case. Each line contains 4 positive integers: N1 N2 Label base.
Here neither N1 nor N2 has more than 10 digits. A number less than its base, chosen from the set {0-9,A-z}, where 0-9 represents decimal numbers 0-9 and A-z represents decimal numbers 10-35. If the label is 1, the last base is the base of N1, if the label is 2, the last base is the base of N2.

Output requirements:
for each test case, on one lineprint the base of another number, so that the equation N1 = N2 is true.
If this equation is impossible, thenoutput impossible.
If the solution is not unique, thenoutput the smallest possible cardinality

code:

#include<iostream>
#include<cmath> //pow()
#include<cctype>
#include<algorithm>
#include<iomanip> //保留一位
using namespace std;

//给一个数值和进制,将数值转化为10进制
long long convert(string n, long long radix){
    
    
    long long sum = 0;
    int index = 0, temp = 0;
    //auto是C语言的一个关键字,关键字主要用于声明变量的生存期为自动
    //rbegin(),rend()反向迭代器
    for(auto it = n.rbegin() ; it != n.rend(); it++){
    
    
        
        //二进制有0-9,a-z,字母要转化成数字
        temp = isdigit(*it) ? *it -'0' : *it -'a' + 10;
        
        //pow(x,y)用来求 x 的 y 次方的值
        sum += temp * pow(radix , index++);
    }
    return sum;
}

//二分查找法,找到令两个数值相等的进制数
long long find_radix(string n,long long num){
    
    
    char it = *max_element(n.begin(),n.end());//查询最大值所在的第一个位置
    long long low = (isdigit(it)? it-'0' : it -'a' + 10) +1;
    long long high = max(num, low);
    while(low  <= high){
    
    
        long long mid = (low + high)/2;
        long long t = convert(n,mid); //要找的值
        
        if (t<0 || t>num) high = mid - 1;
        else if (t == num) return mid; //找到
        else low = mid +1;
    }
    return -1; //找不到
}

int main()
{
    
    
    string s1, s2;
    long long tag = 0, radix = 0, result_radix;
    cin>> s1 >> s2 >> tag >> radix;
    
    //已知的数的基数是第一个数的基数?是则将s1转化为10进制后比较,不是则将s2转化为10进制后比较
    result_radix = tag == 1 ? find_radix(s2, convert(s1 , radix)) :find_radix(s1, convert(s2 , radix));
    
    //输出
    if(result_radix == -1){
    
    
        cout<<"Impossible";
    }else{
    
    
        cout<<setiosflags(ios::fixed)<<setprecision(1)<<result_radix;
    }
    return 0;
}

END

おすすめ

転載: blog.csdn.net/qq_51669241/article/details/124913651