刷题笔记day7

【PAT1006】 Sign In and Sign Out 

 考察点在于qsort函数cmp的写法。

#include<stdio.h>
#include<stdlib.h>
char name[16][100];

struct stu
{
    char name[16];
    int h1,m1,s1;
    int h2,m2,s2;
}a[100];

int cmp1(const void* a,const void* b)
{
    struct stu* c = (struct stu*)a;
    struct stu* d = (struct stu*)b;

    if((*c).h1 == (*d).h1)
    {
        if((*c).m1 == (*d).m1)
        {
            return (*c).s1 - (*d).s1;
        }
        else
        {
            return (*c).m1 - (*d).m1;
        }
    }
    else
    {
        return (*c).h1 - (*d).h1;
    }
}

int cmp2(const void* a,const void* b)
{
    struct stu* c = (struct stu*)b;
    struct stu* d = (struct stu*)a;

    if((*c).h2 == (*d).h2)
    {
        if((*c).m2 == (*d).m2)
        {
            return (*c).s2 - (*d).s2;
        }
        else
        {
            return (*c).m2 - (*d).m2;
        }
    }
    else
    {
        return (*c).h2 - (*d).h2;
    }
}


int main()
{
    int n;
    scanf("%d",&n);
    
    if(n==0) return 0;

    int i;
    for(i=0;i<n;i++)
    {
        scanf("%s",a[i].name);
        scanf("%d:%d:%d",&a[i].h1,&a[i].m1,&a[i].s1);
        scanf("%d:%d:%d",&a[i].h2,&a[i].m2,&a[i].s2);
    }

    qsort(a,n,sizeof(struct stu),cmp1);
    printf("%s ",a[0].name);

    qsort(a,n,sizeof(struct stu),cmp2);
    printf("%s",a[0].name);

}

【PAT A1007】Maximum Subsequence Sum

开辟额外数组空间,存储first和last

#include<stdio.h>
int a[10005];
int dp[10005];
int first[10005];
int last[10005];

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

    for(i=1;i<n;i++)
    {
        if(dp[i-1]+a[i] > a[i])
        {
            dp[i] = dp[i-1]+a[i];
            first[i] = first[i-1];
            last[i] = i;
        }
        else
        {
            dp[i] = a[i];
            first[i] = i;
            last[i] = i;
        }
    }

    int max = dp[0];
    int k=0;
    for(i=1;i<n;i++)
    {
        if(dp[i]>max)
        {
            k=i;
            max = dp[i];
        }
    }
    
    if(max<0)
        printf("0 %d %d\n",a[0],a[n-1]);
    else
        printf("%d %d %d\n",max,a[first[k]],a[last[k]]);


}

【PAT A1008】 Elevator 

#include<stdio.h>

int main()
{
    int n;
    
    int ans = 0;
    int now = 0;
    
    scanf("%d",&n);

    int i;
    for(i=0;i<n;i++)
    {
        int des;
        scanf("%d",&des);

        if(des-now>0)
        {
            ans += ((des-now)*6);
        }
        else
        {
            ans += ((now-des)*4);
        }

        now = des;
        ans += 5;
    }

    printf("%d",ans);
}

【PAT A1009】 Product of Polynomials

要注意数组c的大小大于2000

#include<stdio.h>
double a[1005] = {0.0};
double b[1005] = {0.0};
double c[3000] = {0.0};
int main()
{
    int k1,k2;
    scanf("%d",&k1);

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

    scanf("%d",&k2);

    for(i=0;i<k2;i++)
    {
        int zhi;
        scanf("%d",&zhi);
        scanf("%lf",&b[zhi]);
    }

    for(i=0;i<1005;i++)
    {
        if(a[i]!=0)
        {
            int j;
            for(j=0;j<1005;j++)
            {
                if(b[j]!=0)
                {
                    c[i+j] += a[i]*b[j];
                }
            }
        }
    }
    
    int ccount=0;
    for(i=0;i<3000;i++)
    {
        if(c[i]!=0)
            ccount++;
    }
    
    printf("%d",ccount);
    for(i=2999;i>=0;i--)
    {
        if(c[i]!=0)
        {
            printf(" %d %.1lf",i,c[i]);
        }
    }
}

【PAT A1010】 Radix 

 看了答案才知道,本题radix的范围是2~int_max(?),本来以为由于字母和数字限制,表示的范围是2~35。

另外一个坑是,由于范围过大,要使用二分法。

最坑的一个坑是,由于radix太大,longlong也可能会溢出。需要判断,如果结果为负数,那么说明已经溢出。

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<unordered_map>
#include<cmath>

using namespace std;

int main()
{
    string n1, n2;
    int tag, radix;
    cin >> n1 >> n2 >> tag >> radix;
    long long a = 0, b = 0,res;
    if (tag == 2) swap(n1, n2);

    char ch;
    int index = 0;
    while (!n1.empty())
    {
        ch = n1.back();
        if (ch >= 'a' && ch <= 'z')
        {
            a += (ch - 'a' + 10) * pow(radix, index);
        }
        else
        {
            a += (ch - '0') * pow(radix, index);
        }
        n1.pop_back();
        index++;
    }

    long long temp = 0;
    for (int i = 0; i < n2.length(); i++)
    {
        if (n2[i] > temp) temp = n2[i];
    }
    if (temp >= 97) temp -= 87;
    else temp -= 48;

    long long left = temp + 1;
    long long right = a + 1;
    res = a + 2;
    while(left <= right)
    {
        temp = (left + right) / 2;
        index = 0;
        b = 0;
        string tempn2 = n2;
        while (!tempn2.empty())
        {
            ch = tempn2.back();
            if (ch >= 'a' && ch <= 'z')
            {
                b += (ch - 'a' + 10) * pow(temp, index);
            }
            else
            {
                b += (ch - '0') * pow(temp, index);
            }
            tempn2.pop_back();
            index++;
            if (b > a || b < 0) break;
        }
        if (a == b)
        {
            res = min(res, temp);
            right--;
        }
        else if (b > a || b < 0)
        {
            right = temp - 1;
        }
        else if (b < a)
        {
            left = temp + 1;
        }
    }
    if (res == a + 2) cout << "Impossible" << endl;
    else cout << res << endl;
    return 0;
}

【PAT A1011】 World Cup Betting

输出double类型,使用%f

#include<stdio.h>
char s[3] = {'W','T','L'};
double t[3] = {0};
int ti[3] = {0};
int main()
{
    int i;
    int n=3;
    for(i=0;i<n;i++)
    {
        int j;
        for(j=0;j<n;j++)
        {
            double temp;
            scanf("%lf",&temp);
            if(temp>t[i])
            {
                t[i] = temp;
                ti[i] = j;
            }

        }
    }
    for(i=0;i<n;i++)
    {
        printf("%c ",s[ti[i]]);

    }
    double ans =( t[0]*t[1]*t[2]*0.65-1)*2;
    printf("%.2f",ans);

}

 

posted @ 2020-03-14 13:57  yoyoyayababy  阅读(109)  评论(0编辑  收藏  举报