PAT甲级 (1006-1010)

1006

https://pintia.cn/problem-sets/994805342720868352/problems/994805516654460928
在这里插入图片描述

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>

using namespace std;

struct stu{
    string id;
    string in;
    string out;
};

const int MAXN = 1000;

stu s[MAXN];

bool cmp1(stu s1,stu s2){
    return s1.in<s2.in;
}

bool cmp2(stu s1,stu s2){
    return s1.out>s2.out;
}

int main(){
    //freopen("D://case.txt","r",stdin);
    std::ios::sync_with_stdio(false);//加速输入C++
    int n;
    cin>>n;
    for (int i=0;i<n;i++){
        stu t;
        cin>>t.id;
        cin>>t.in;
        cin>>t.out;
        s[i] = t;
    }
    sort(s,s+n,cmp1);
    cout<<s[0].id<<' ';
    sort(s,s+n,cmp2);
    cout<<s[0].id;
    return 0;
}

1007

https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168
在这里插入图片描述

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>

using namespace std;

const int MAXN = 100000;

long long arr[MAXN];
long long dp[MAXN];

long long first[MAXN];//第i个数字结尾时序列的开始数字
long long ender[MAXN];//第i个数字结尾时序列的结束数字

long long fir;//最终结果
long long en;//最终结果

long long maxsubseqyence(int n){
    long long maximum = -9999;
    for (int i=0;i<n;i++){
        if (i == 0){
            dp[i] = arr[i];
            first[i] = arr[i];
            ender[i] = arr[i];
        }
        else {
            if (arr[i]>dp[i-1]+arr[i]){
                dp [i] = arr[i];
                first[i] = arr[i];
                ender[i] = arr[i];
            }
            else {
                dp[i] = dp[i-1]+arr[i];
                first[i] = first[i-1];//起始数字不变
                ender[i] = arr[i];//结尾数字往后移动
            }
        }
        if (maximum<dp[i]){//更改结果
            fir = first[i];
            en = ender[i];
        }
        maximum = max(maximum,dp[i]);
    }
    return maximum;
}


int main(){
    //freopen("D://case.txt","r",stdin);
    std::ios::sync_with_stdio(false);//加速输入C++
    int m;
    cin>>m;
    for (int i=0;i<m;i++){
        cin>>arr[i];
    }
    if (m == 0){
        cout<<"0 0 0";
    }
    else {
        long long result = maxsubseqyence(m);
        if (result<0){
            cout<<'0'<<' ';
            cout<<arr[0]<<' '<<arr[m-1];
        }
        else {
            cout<<result<<' ';//cout优先级最高,直接写一行会优先输出cout<<maxsubseqyence(m)<<' '<<fir<<' '<<en;
            cout<<fir<<' '<<en;
        }
    }

    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 1000000;

long long arr[MAXN];
 
int main()
{
	int m;
	cin>>m;
	long long count = -1;
    long long first = 0 ;
    long long end = m-1;
    long long temp = 0;
    long long a= 0;
	for (int i = 0; i<m;i++){
		cin>>arr[i];
		temp+=arr[i];
		if (temp < 0){//都是负数
			temp = 0;
			a = i + 1;
		}
		else if (temp>count){//比前面的大
			count = temp;
			end = i;
			first = a;
		}
	}
	if (count < 0)
		count = 0;
	cout << count << " " << arr[first] << " " << arr[end];
	return 0;
}

1008

https://pintia.cn/problem-sets/994805342720868352/problems/994805511923286016
在这里插入图片描述

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>

using namespace std;

int main(){
    //freopen("D://case.txt","r",stdin);
    std::ios::sync_with_stdio(false);//加速输入C++
    int casenum;
    cin>>casenum;
    int before = 0;
    int now;
    int time = 0;
    while(casenum--){
        cin>>now;
        if (now>=before){time += 6*(now-before)+5;}
        else {time += 4*(before-now)+5;}
        before = now;
    }
    cout<<time;
    return 0;
}

1009

https://pintia.cn/problem-sets/994805342720868352/problems/994805509540921344
在这里插入图片描述

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <iomanip>

using namespace std;

const int MAXN = 2010;

double result[MAXN+10];//每个幂的系数结果
double case1[MAXN+10];


int main(){
    //freopen("D://case.txt","r",stdin);
    std::ios::sync_with_stdio(false);//加速输入C++
    int m;
    cin>>m;
    while(m--){
        int x;
        double y;
        cin>>x;//幂
        cin>>y;//系数
        case1[x] = y;
    }
    cin>>m;
    while(m--){
        int x;
        double y;
        cin>>x;//幂
        cin>>y;//系数
        for (int i=0;i<1010;i++){//输入的每一个幂和case1相乘,这个位置i最大为1010,若时2000+,会数组越界,因为下面是x+i
            result[x+i] += y*case1[i];
        }
    }
    int num = 0;
    for (int i=0;i<2002;i++){if (result[i] != 0){num++;}}
    cout<<num;
    for (int i=2002;i>=0;i--){
        if (result[i] != 0){
            cout<<' '<<i<<' ';
            cout<<fixed << setprecision(1)<<result[i];
        }
    }
    return 0;
}

1010

https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536
在这里插入图片描述

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <iomanip>
#include <string>

using namespace std;

string a,b;
int tag;
int radix;

long long chartoint(char x){
    if (x>='0'&&x<='9'){return x-'0';}
    else {return x-'a'+10;}
}

char inttochar(long long x){
    if (x>=0&&x<=9){return x+'0';}
    else {return x-10+'a';}
}

long long turnten(string str,int radix){//str数字是radix进制,转成十进制
    long long result = 0;
    for (int i=0;i<str.size();i++){
        result = result*radix+chartoint(str[i]);
    }
    return result;
}

string turnradix(long long x,int radix){//十进制转radix进制
    string s = "";
    char tmp;
    while(x!=0){
        tmp = x%radix+'0';
        s += tmp;
        x /= radix;
    }
    string str(s.rbegin(),s.rend());
    return str;
}

int findradix(string str){//找到字符串中最大字符+1,作为最小进制
    char imin = '0';
    for (int i=0;i<str.size();i++){//进制最小是b的最大字符+1
        imin = max(str[i],imin);
    }
    return chartoint(imin+1);
}

//radix的a用十进制表示,radix_mid进制的b改为10进制表示,如果b小了,则进制变大
void fun(string a,string b){
    long long a_ten = turnten(a,radix);
    long long radix_min = findradix(b);//找到字符串中最大字符+1,作为最小进制
    long long radix_max = max(a_ten,radix_min);//最大是a的十进制表示+1或者最小进制
    while(radix_min<=radix_max){
        long long radix_mid = (radix_max+radix_min)/2;
        long long b_ten = turnten(b,radix_mid);
        if (a_ten == b_ten){cout<<radix_mid;return;}
        else if (a_ten>b_ten && b_ten>=0){radix_min = radix_mid+1;}//当b_ten过大,会溢出,变为负数
        else {radix_max = radix_mid-1;}
    }
    cout<<"Impossible";
    return;
}

int main(){
    //freopen("D://case.txt","r",stdin);
    std::ios::sync_with_stdio(false);//加速输入C++
    cin>>a>>b>>tag>>radix;
    if (tag == 1){
        fun(a,b);
    }
    else {
        fun(b,a);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值