【PTA】PAT (Advanced Level) Practice 学习笔记1

(以下采用C++语言编写。)

1001 A+B Format

Calculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

题目翻译:

计算a+b,并以标准格式输出和——也就是说,必须用逗号将数字分成三组(除非小于四位数)。

Input Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −10^6 ≤a, b≤10^6. The numbers are separated by a space.

Output Specification:
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input: 输入样例

-1000000 9

Sample Output: 输出样例

-999,991

我的解法

完完全全是将a+b的结果看成是数字去解的。(步骤繁多。。。)
flag表示sum有几位数。
注意输出数字的顺序。(先按照 i 从1到flag将数字存到数组c,再按照 i 从flag到1输出c[i])

#include<iostream>
using namespace std;

int main(){
    int a,b;
    int sum=0;
    int flag=0;//有几位数
    int c[10]={0};
    cin>>a>>b;
    sum = a+b;
    if(sum<0){
        cout<<"-";
        sum=-sum;
    }
    //测试点4是和为0的情况
    if(sum==0){
        cout<<"0";
    }
    //普通情况
    for(int i=1;sum>0;i++){
    c[i]=sum%10;
    sum=sum/10;
    flag++;
    }
    for(int i=flag;i>=1;i--){
    cout<<c[i];
    if((i-1)%3==0 &&(i-1)!=0){
        cout<<",";
        }
    }
}

大神的代码

#include <iostream>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    string s = to_string(a + b);
    int len = s.length();
    for (int i = 0; i < len; i++) {
        cout << s[i];
        if (s[i] == '-') continue;
        if ((i + 1) % 3 == len % 3 && i != len - 1) cout << ","; // 神秘表达式
    }
    return 0;
}

解析:
1、to_string() 能将a+b的和从将数字常量转换为字符串
2、len就是字符串的长度
3、最后那个if语句(神秘表达式)含义:
当 i+1==len 时,表示刚好检测到字符串最后面那个数字,(在样例中就是最前面的数字1)
如果字符串长度 len 刚好是3的倍数,不可以输出“,”,不然就会变成这样子的输出 “999,”
所以输出逗号时:

i != len - 1

因为输出是按照 9->9->“,”->9->9->9->1来的,所以

(len-1)%3

表示从头开始搜索,再搜索(len-1)%3 个数字,就到达3的倍数了,这时要输出”,“。

举个例子:
s=89351,len=5,当i=1时,恰好搜索到 8 9 351 ,第二个数字9后面就要输出逗号了。

1002 A+B for 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:
在这里插入图片描述
​where K is the number of nonzero terms in the polynomial, N
i and a N i (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 描述

题目翻译:

这次,你要找到A+B,其中A和B是两个多项式。
每个样例有两行,每一行代表一个多项式,K是非零整数,Ni是指数,aNi是系数
K范围是1到10,NK范围是0到1000,输出一行,行尾不可有空格

Output Specification:
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

我的代码

因为可能有1000项,所以数字要定大一点。搜索和输出的时候要将1000项都检测一遍(for循环),看看是否系数不为零。
输出必须要精确到小数点后1位。

#include<iostream>
#include <iomanip>
using namespace std;
int main(){
    int m=0;//多项式中有m项
    int x;
    double y;//x表示指数,y表示系数
    double a[1002]={0};//a[i]存放指x^i的系数
    int flag=0;//系数不是0的项数
    
    //输入A
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y;
        a[x]+=y;
    }
    //输入B
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>x>>y;
        a[x]+=y;
    }
    //找出系数不是0的项数
    for(int i=0;i<1001;i++){
        if(a[i]!=0)
            flag++;
    }
    //输出
    cout<<flag;
    for(int i=1001;i>=0;i--){
        if(a[i]!=0){
            cout<<" "<<i<<" "<<setiosflags(ios::fixed)<<setprecision(1)<<a[i];
        }
    }
}

1003 Emergency

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

题目翻译:

作为一个城市的紧急救援队队长,你会得到一张你所在国家的特殊地图。这张地图显示了由几条道路连接起来的几个分散的城市。每个城市的救援队伍数量和任何两个城市之间的每条道路的长度都在地图上标出。当你接到来自其他城市的紧急电话时,你的工作是尽快带领你的人赶到那个地方,同时,召集尽可能多的人在路上帮忙。

Input Specification:
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C1 and C 2 the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c 2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C 1 to C 2.

Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2 , and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

测试用例,:
一行包含4个正整数:N(<=500)城市的数量(和城市编号从0到N 1), M 道路的数量,C1和c2你目前的城市,你必须前往的城市。
下一行包含N个整数,第i个整数为第i个城市的救援队数量。然后有M条线,每条线描述了一条道路,有三个整数c1, c2和L,分别是由一条道路连接的一对城市和这条道路的长度。

Sample Input:

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output:

2 4

代码

迪杰哥斯拉算法太久没用已经忘得差不多了😭只能参考别人的代码了。
参考

补充:
vector<vector > edge; 如何理解?
这样定义可以将c1, c2和L三个数字一起放入edge数组里面,方便后续作为整体进行操作。
形如:edge = [ [c1,c2,L] , [c1,c2,L] , … ]

edge(n, vector(n, INT_MAX));
表示定义edge数组有n个vector(n, INT_MAX)对象,每个对象杯初始化为INT_MAX。
注意:INT_MAX是在 #include<limits.h> 头文件里面的,用于获取有符号 int 对象的最大值。

#include<iostream>
#include<vector>
#include<limits.h>
using namespace std;
int main(){
    int n,m,c1,c2; //第一行数字
    int x,y,s;
    
    //输入
    cin>>n>>m>>c1>>c2;
    
    vector<int> a(n,0);
    vector<vector<int> > edge(n, vector<int>(n,INT_MAX)); //二维
    vector<int> dis(n, INT_MAX);
    vector<int> roads(n,0);
    vector<int> teams(n,0);
    vector<bool> visit(n,false);
    
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    
    for(int i=0;i<m;i++){
        cin>>x>>y>>s;
        edge[x][y] = edge[y][x] = s;
    }
    
    //迪杰哥斯拉算法,找最短路径
    dis[c1]=0;
    teams[c1]=a[c1];
    roads[c1]=1;
    
    for(int i=0;i<n;i++){
        int u=-1, minn=INT_MAX;
        for(int j=0;j<n;j++){
            if(visit[j]==false && dis[j]<minn){
                u=j;
                minn=dis[j];
            }
        }
        if (u == -1) break;
        visit[u] = true;
        for (int v = 0; v < n; v++) {
            if (visit[v] == false && edge[u][v] != INT_MAX) {
                if (dis[u] + edge[u][v] < dis[v]) {
                    dis[v] = dis[u] + edge[u][v];
                    roads[v] = roads[u];
                    teams[v] = teams[u] + a[v];
                } else if (dis[u] + edge[u][v] == dis[v]) {
                    roads[v] = roads[v] + roads[u];
                    if (teams[u] + a[v] > teams[v]) {
                        teams[v] = teams[u] + a[v];
                    }
                }
            }
        }
    }
    cout<<roads[c2]<<" "<<teams[c2]<<endl;
    return 0;
}

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值