PAT甲级刷题日记(1月27日,1011)

1001 A+B Format

PTA | 程序设计类实验辅助教学平台

#include <iostream>
#include <iomanip>

int main()
{
    int a, b, sum;
    int three_digits[5];//sum从右到左每3位为一个数组元素
    std::cin >> a >> b;
    sum = a + b;
    if(sum == 0)//防止输出three_digits[-1]
    {
        std::cout << sum;
        return 0;
    }
    int i = 0;
    while(sum != 0)//得到three_digits数组
    {
        three_digits[i] = sum % 1000;
        sum = sum / 1000;
        ++i;
    }
    std::cout << three_digits[i-1];//输出sum的头几位,头几位之前需要保留符号,并且没有逗号
    for(int j = i - 2;j >= 0;--j)//输出sum的其他部分
    {
        if(three_digits[j] < 0)
        {
            three_digits[j] = -three_digits[j];//去除符号
        }
        std::cout << ',' << std::setw(3) << std::setfill('0') << three_digits[j];//加上逗号,并且补0
    }
    std::cout << '\n';
}

1002 A+B for Polynomials

PTA | 程序设计类实验辅助教学平台

感觉自己写的好啰嗦...

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>

struct Term
{
    int N;//exponent
    float aN;//coefficient
};

bool cmp_with_N(const Term& a, const Term& b)
{
    return a.N > b.N;
}

int main()
{
    //多项式,即f(x)=An * x^n + An-1 * x^(n-1) + An-2 * x^(n-2) * ... * A2 * x^2 + A1 * x + A0
    //K表示多项式非零项的数量,NK表示指数幂,aNK表示系数。exponent n.指数,幂    coefficient n.系数
    //实际上是一个多项式合并问题。
    //我的思路是用两个vector分别保存A和B的项。然后对A和B的项依次遍历,再分情况加入到SUM中.
    //刚开始以为输入信息时也是保证指数由大到小的,然而提交的时候发现只有17分,在对A和B按照指数的大小进行排序之后,就全通过了.
    
    int k1, k2;
    int k3 = 0;
    std::vector<Term> A(10);
    std::vector<Term> B(10);
    std::vector<Term> SUM(20);
    
    //输入多项式A的信息
    std::cin >> k1;
    for(int i = 0;i < k1;++i)
    {
        std::cin >> A[i].N >> A[i].aN;
    }
    //输入多项式B的信息
    std::cin >> k2;
    for(int i = 0;i < k2;++i)
    {
        std::cin >> B[i].N >> B[i].aN;
    }

    std::sort(A.begin(), A.end(), cmp_with_N);//对A和B进行排序.
    std::sort(B.begin(), B.end(), cmp_with_N);

    for(int i = 0,j = 0;i < k1 || j < k2;)
    {
        if (A[i].N > B[j].N)
        {//将A的项加入SUM
            SUM[k3].N = A[i].N;
            SUM[k3].aN = A[i].aN;
            ++k3;
            ++i;
        }
        else if (A[i].N == B[j].N)
        {//如果A和B项的指数相等,则要将系数相加,再加入SUM
            float tmp = A[i].aN + B[j].aN;
            if(tmp != 0.0)
            {//系数为0则不用加入SUM
                SUM[k3].N = A[i].N;
                SUM[k3].aN = tmp;
                ++k3;
            }
            ++i;
            ++j;
        }
        else
        {//将B的项加入SUM
            SUM[k3].N = B[j].N;
            SUM[k3].aN = B[j].aN;
            ++k3;
            ++j;
        }
    }
    if(k3 == 0)
    {
        std::cout << '0';
        return 0;
    }
    std::cout << k3 << ' ';
    int i = 0;
    for(i = 0;i < k3-1;++i)
    {
        std::cout << SUM[i].N << ' ' << std::fixed << std::setprecision(1) << SUM[i].aN << ' ';//小数点后保留一位
    }
    std::cout << SUM[i].N << ' ' << std::fixed << std::setprecision(1) << SUM[i].aN << '\n';//小数点后保留一位

}

1003 Emergency

PTA | 程序设计类实验辅助教学平台

题目的意思是输出两个结点之间最短路径的数目,并输出最短路径所经过的城市的营救队伍数量之和的最大值。虽然知识点在数据结构中都学过,但是忘的差不多了,等复习完图在来补全代码。

1005 Spell It Right

PTA | 程序设计类实验辅助教学平台

#include <iostream>
#include <string>

void digit_to_eng(int par)
{
    switch (par)
        {
        case 0:
            std::cout << "zero";
            break;
        case 1:
            std::cout << "one";
            break;
        case 2:
            std::cout << "two";
            break;
        case 3:
            std::cout << "three";
            break;
        case 4:
            std::cout << "four";
            break;
        case 5:
            std::cout << "five";
            break;
        case 6:
            std::cout << "six";
            break;
        case 7:
            std::cout << "seven";
            break;
        case 8:
            std::cout << "eight";
            break;
        case 9:
            std::cout << "nine";
            break;
        default:
            break;
        }
}

int main()
{
    std::string str;//要输入的非负整数N,N的范围远超过基本整数类型能表示的范围,因此用字符串或字符数组来存放N。
    std::cin >> str;
    int sum = 0;
    int rem[10] = {};
    int i = 0;
    for(const auto& c : str)
    {//读每一位数,并求和
        sum += c - '0';
    }
    while (sum > 0)
    {//得到sum的每一位
        rem[i] = sum % 10;
        sum = sum / 10;
        ++i;
    }
    //将整型数字用英文输出。
    if(i == 0)
    {
        std::cout << "zero\n";
        return 0;
    }
    for(int j = i - 1;j > 0;--j)
    {
        digit_to_eng(rem[j]);
        std::cout << ' ';
    }
    digit_to_eng(rem[0]);
    
}

1006 Sign In and Sign Out

PTA | 程序设计类实验辅助教学平台

#include <iostream>
#include <climits>
#include <string>

int main()
{
    int sign_in_time = INT_MAX;//to record the minimum sign_in_time
    int sign_out_time = INT_MIN;//to record the maximum sign_out_time
    int M = 0;
    std::cin >> M;
    std::string lock, unlock;
    for(int i = 0;i < M;++i)
    {
        std::string ID_number;
        std::cin >> ID_number;
        int sith,sitm,sits,soth,sotm,sots;//sign_in_time_hour sign_in_time_minute etc.
        scanf("%d:%d:%d %d:%d:%d", &sith, &sitm, &sits, &soth, &sotm, &sots);
        int sign_in_time_in_seconds = sith * 3600 + sitm * 60 + sits;
        int sign_out_time_in_seconds = soth * 3600 + sotm * 60 + sots;
        if(sign_in_time_in_seconds < sign_in_time)
        {//update the sign_in_time and the unlock's ID_number.
            sign_in_time = sign_in_time_in_seconds;
            unlock = ID_number;
        }
        if(sign_out_time_in_seconds > sign_out_time)
        {//update the sign_out_time and the lock's ID_number.
            sign_out_time = sign_out_time_in_seconds;
            lock = ID_number;
        }
    }
    std::cout << unlock << ' ' << lock << '\n';
}

1007 Maximum Subsequence Sum

PTA | 程序设计类实验辅助教学平台

暴力破解

#include <iostream>
#include <vector>
#include <climits>

int main()
{
    //暴力求解
    int K;
    int max_sum = 0;//最大的子序列和
    int left = 0, right = 0;
    int max_input = INT_MIN;
    std::cin >> K;
    std::vector<int> nums(K);
    for(auto&& num : nums)
    {//输入K个整数数字.并得到最大的数字
        std::cin >> num;
        if(num > max_input)
        {
            max_input = num;
        }
    }
    if(max_input < 0)
    {//所有的数都小于0
        std::cout << 0 << ' ' << nums.at(0) << ' ' << nums.at(K-1) << '\n';
        return 0;
    }
    if(max_input == 0)
    {//当最大的数为0时,由于下面的for循环中,current_sum初始化为0,导致当最大的数为0时,不会更新下标,因此要添加这段代码。
        std::cout << 0 << ' ' << 0 << ' ' << 0 << '\n';
        return 0;
    }
    for(int i = 0;i < K;++i)
    {
        int current_sum = 0;//当前的子序列和,从Ni到Nj
        for(int j = i;j < K;++j)
        {
            current_sum += nums.at(j);
            if(current_sum > max_sum)
            {
                max_sum = current_sum;//更新max_sum
                left = i;//更新最大子序列的第一个元素下标
                right = j;//更新最大子序列的最后一个元素的下标
            }
        }
    }
    std::cout << max_sum << ' ' << nums.at(left) << ' ' << nums.at(right) << '\n';
}

1008 Elevator

PTA | 程序设计类实验辅助教学平台

#include <iostream>

int main()
{
    //简单模拟,总时间=电梯上升的层数*6 + 下降的层数*4 + 停留的层数*5
    int N = 0;
    std::cin>>N;
    int nums[100];
    int result = 0;
    for(int i = 0; i < N; i++)
    {
        std::cin>>nums[i];
    }

    int upNum = nums[0];//The elevator is on the 0th floor at the beginning
    int downNum = 0;

    for(int i = 0; i < N-1; i++)
    {
        if(nums[i]<=nums[i+1])
        {
            upNum += (nums[i+1]-nums[i]); 
        }
        else
        {
            downNum -= (nums[i+1]-nums[i]);
        }
    }
    
    result = upNum*6 + downNum*4 + N*5;
    
    std::cout<<result;
    
    return 0;
}

1009 Product of Polynomials

PTA | 程序设计类实验辅助教学平台

大致思路就是模拟多项式相乘的过程。多项式A的项与多项式B的项逐个相乘,指数相加,系数相乘,并会产生K1*K2个项,这需要两个for循环实现;之后再将这K1*K2个项化简合并,指数相等则系数相加,若系数为0,则要去除;最后输出结果。

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>

struct Term
{
    int exponent;
    float coefficient;
};

bool cmp_with_expo(const Term& a,const Term& b)
{
    return a.exponent > b.exponent;
}

int main()
{
    int K1 = 0, K2 = 0;//A和B的多项式的项数
    //输入K1,K2以及多项式A和B
    std::cin >> K1;
    std::vector<Term> A(K1);
    for(auto&& term : A)
    {
        std::cin >> term.exponent >> term.coefficient;
    }
    std::cin >> K2;
    std::vector<Term> B(K2);
    for(auto&& term : B)
    {
        std::cin >> term.exponent >> term.coefficient;
    }
    
    int K3 = K1 * K2;//会产生K1*K2个项
    std::vector<Term> temp_product(K3);//存放多项式A*B后还未化简的乘积,之后要进行多项式的合并与化简
    std::vector<Term> product(K3);//化简后的乘积
    int index = 0;
    //直接一项一项地互相乘,并且不化简
    for(const auto& termA : A)
    {
        for(const auto& termB: B)
        {
            temp_product.at(index).exponent = termA.exponent + termB.exponent;//指数相加
            temp_product.at(index).coefficient = termA.coefficient * termB.coefficient;//系数相乘
            ++index;
        }
    }
    
    //多项式化简、合并
    std::sort(temp_product.begin(), temp_product.end(), cmp_with_expo);//排序,按指数从大到小排列
    int index_product = 0;
    
    for(int i = 0;i < K3-1;++i)
    {
        if(temp_product[i].exponent == temp_product[i+1].exponent)
        {
            temp_product[i+1].coefficient += temp_product[i].coefficient;//如果指数相等,则将当前项的系数与后一个项的系数相加,存放到后一个项的系数中
        }
        else
        {
            if(temp_product[i].coefficient == 0)
            {//若系数为0,则不存放到product中
                continue;
            }
            product[index_product] = temp_product[i];
            ++index_product;
        }
    }
    product[index_product] = temp_product[K3-1];
    
    //输出
    if(product.empty())
    {//product为空,则输出0
        std::cout << '0' << std::endl;
        return 0;
    }
    std::cout << index_product+1 << ' ';
    for(int i = 0;i < index_product;++i)
    {
        std::cout << product[i].exponent << ' ' << std::fixed << std::setprecision(1) <<product[i].coefficient << ' ';
    }
    std::cout << product[index_product].exponent << ' ' << std::fixed << std::setprecision(1) <<product[index_product].coefficient << std::endl;


}

1010 Radix

PTA | 程序设计类实验辅助教学平台

调试了老半天,还是有一个测试用例出错。。。可能是unsigned long long还是会有溢出。

#include <iostream>
#include <string>
#include <cmath>

unsigned long long int to_decimal(int radix, std::string N)
{
    unsigned long long int N_decimal = 0;
    const int len = N.length();
    for(int i = 0;i < len;++i)
    {
        unsigned int digit = 0;
        if(N.at(i) >= '0' && N.at(i) <= '9')
        {
            digit = N.at(i) - '0';
        }
        else if(N.at(i) >= 'a' && N.at(i) <= 'z')
        {
            digit = N.at(i) - 'a' + 10;
        }
        
        N_decimal += digit * pow(radix,len-i-1);
    }
    return N_decimal;
}

int get_min_radix(std::string N)
{
    int max_digit = -1;
    int len = N.length();
    for(int i = 0;i < len;++i)
    {
        int digit = 0;
        if(N.at(i) >= '0' && N.at(i) <= '9')
        {
            digit = N.at(i) - '0';
        }
        else if(N.at(i) >= 'a' && N.at(i) <= 'z')
        {
            digit = N.at(i) - 'a' + 10;
        }
        if(max_digit < digit)
        {
            max_digit = digit;
        }
    }
    return max_digit + 1;
}

int binary_search(std::string N, unsigned long long target_decimal, unsigned long long int start, unsigned long long int end)
{
    while(start <= end)
    {
        unsigned long long int mid = (end - start) / 2 + start;
        if(to_decimal(mid, N) > target_decimal)
        {
            end = mid - 1;
        }
        else if(to_decimal(mid, N) < target_decimal)
        {
            start = mid + 1;
        }
        else
        {
            return mid;
        }
    }
    return -1;
}

int main()
{
    //the scope of a digit is from 0 to 35.
    //radix的下限是digit+1
    //可以使用二分搜索来找到最小的符合条件的radix。
    std::string N1;
    std::string N2;
    int tag, radix;
    int ans_radix;
    std::cin >> N1 >> N2 >> tag >> radix;

    unsigned long long int N_decimal;
    if(tag == 1)
    {
        N_decimal = to_decimal(radix, N1);
    }
    else if(tag == 2)
    {
        N_decimal = to_decimal(radix, N2);
        N2 = N1;
    }
    else
    {
        return 0;
    }
    int min_radix = get_min_radix(N2);
    unsigned long long int max_radix;
    if(N_decimal > min_radix)
    {
        max_radix = N_decimal;
    }
    else
    {
        max_radix = min_radix;
    }
    ans_radix = binary_search(N2, N_decimal, min_radix, max_radix);

    if(ans_radix == -1)
    {
        std::cout << "Impossible" << std::endl;
        return 0;
    }
    std::cout << ans_radix << std::endl;
    
}

1011 World Cup Betting

PTA | 程序设计类实验辅助教学平台

很简单的模拟题。

#include <iostream>
#include <iomanip>

int main()
{
    float max_profit = 0.0;
    char WTL[3] = {'W','T','L'};
    float max_bets[3] = {};
    for(int i = 0;i < 3;++i)
    {
        float best_bet = 0.0;
        char c = 'W';
        for(int j = 0;j < 3;++j)
        {
            float cur;
            std::cin >> cur;
            if(cur > best_bet)
            {
                best_bet = cur;
                c = WTL[j];
            }
        }
        std::cout << c << ' ';
        max_bets[i] = best_bet;
    }
    max_profit = (max_bets[0] * max_bets[1] * max_bets[2] * 0.65 - 1) * 2;
    std::cout << std::fixed << std::setprecision(2) << max_profit << std::endl;
    
    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fantasy`

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值