PTA甲级 1001-1003

PTA甲级 1001-1003

第一次写博客

大三了,思来想去,想了很久,决定考今年12月的pat甲级。
空说无用,总需要实际行动。所以开始做起了题目,同时也想把做题过程记录下来。当然一开始总是困难的,我也很久没有刷过题目,只有大一的时候接触了一下。
第一次写博客,希望大家多多评论,指出我的不足之处。

博客的代码参照了PAT 甲级(Advanced Level) Practice (C++)汇总

1001

1001 A+B Format (20分)

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).

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

思路

给出两个数,计算两数相加结果,并把结果按每三位一个逗号的形式输出。这题利用栈可以把结果一位位压进去,比较重要的是输出格式。

下面讲一下我的输出想法:输出栈里的第一个数据后,每隔三位打印一个逗号。

代码如下

#include <stdio.h>
#include <iostream>
#include <stack>
#include <string>
#include <math.h>
using namespace std;

stack <int> s;

int main()
{
    int a, b, i, res, term ;
    string c;

    cin>>a>>b;

    res = a + b;
    term = res;

    if (res == 0)
        cout<<"0";
    else
    {
        while (res != 0)

        {
            s.push(abs(res % 10));

            res /= 10;
        }

        if (term < 0)
            cout<<"-";
        while (!s.empty())
        {
            cout<<s.top();
            s.pop();

            if (s.size() % 3 == 0 && s.size() != 0)
                cout<<",";
        }
    }

    return 0;

}

1002

1002 A+B for Polynomials (25分)

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:

K N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ … N​K​​ a​N​K​​​​

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 1≤K≤10,0≤N​K​​<⋯<N​2​​<N​1​​≤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

思路

指数相同的,系数相加。
这题用链表比用数组方便一些。将第一行的数据存入链表中,读第二行数据时,与存入链表的数据进行比较,最终得出结果。
需要注意的是:两个系数相加可能为0,此时应把这一项删除。
对于第一行数据,“同样的指数会不会重复出现,并且按照大小顺序排列”这个点,我还留有疑问。

代码如下

#include <stdio.h>
#include <iostream>
#include <stack>
#include <string>
#include <math.h>
#include <list>

using namespace std;

typedef struct{
    int exp;
    double res;
}poly;

list <poly> L;

int main()
{
    int i, k;
    poly p;

    list<poly>::iterator it;

    cin>>k;
    for (i = 0; i < k; i++)
    {
        cin>>p.exp>>p.res;
        L.push_back(p);
    }

    cin>>k;
    for (i = 0; i < k; i++)
    {
        cin>>p.exp>>p.res;

        for (it = L.begin(); it != L.end() && p.exp < (*it).exp; ++it)
        {

        }

        if (it == L.end())
            L.push_back(p);
        else
        {
            if (p.exp > (*it).exp)
            {
                L.insert(it, p);
            }
            else if (p.exp == (*it).exp)
            {
                if (p.res + (*it).res == 0)
                    L.erase(it);
                else
                    (*it).res += p.res;
            }
        }

    }

    cout<<L.size();
    for (it = L.begin(); it != L.end(); ++it)
    {
        //cout<<" "<<(*it).exp<<" "<<(*it).res;
         printf(" %d %0.1lf", (*it).exp, (*it).res);
    }

    return 0;
}

1003

1003 Emergency (25分)

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, C​1​​ 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 c​1​​, 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 C​1​​ and C​2​​, 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.

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

思路

经典的最短路径问题,这里用的是dijkstra算法。
需要指出的是这一题的样例中可能会有多条最短路径,我们需要计算出有几条最短路径,并计算出几条最短路径中最大的结点权重值
还有一点,起点和终点可能是同一个城市,这种情况要特殊考虑。

代码如下

#include <stdio.h>
#include <iostream>
#include <stack>
#include <string>
#include <math.h>
#include <list>

using namespace std;

const int MAX = 505;
#define INF 0x0fffff
int weight[MAX];
int c[MAX][MAX];
int n, m, StartCity, EndCity;
int dis[MAX], we[MAX], num[MAX];

bool vis[MAX] = {0};

void dijkstra()
{
    int i, j, mind, v;

    vis[StartCity] = 1;

    for (i = 0; i < n; i++)
    {
        dis[i] = c[StartCity][i];//起点到各个城市的距离
        //如果连通,加上起点到城市的距离,最短路径+1
        if (dis[i] < INF)
        {
            we[i] = weight[StartCity] + weight[i];
            num[i] = 1;
        }
        else //如果不连通,则不加,最短路径不+1
        {
            we[i] = 0;
            num[i] = 0;
        }
    }

    for (i = 1; i < n; i++)
    {
        mind = INF;


        for (j = 0; j < n; j++)
        {
            //如果当前节点没被遍历过,并且更新最短路径
            if (!vis[j] && mind > dis[j])
            {
                mind = dis[j];
                v = j;
            }
        }
        vis[v] = 1; //节点已被访问
        if(v == EndCity) //如果节点就是终点,打印输出
        {
            cout<<num[v]<<" "<<we[v];
            break;
        }

        for ( j = 0; j < n; j++)
        {
            if (!vis[j])
            {
                int x = dis[v] + c[v][j];
                int y = we[v] + weight[j];

                if (x < dis[j])
                {
                    dis[j] = x;
                    we[j] = y;
                    num[j] = num[v];
                }
                else if (x == dis[j])
                {
                    num[j] += num[v];
                    if (y > we[j])
                        we[j] = y;
                }
            }
        }
    }

}

int main()
{
    int  i, j;
    int termx, termy, termw;

    cin>>n>>m>>StartCity>>EndCity;

    for (i = 0; i < MAX; i++)
    {
        for (j = 0; j < MAX; j++)
            c[i][j] = INF;
    }

    for (i = 0; i < n; i++)
        cin>>weight[i];

    for (i = 0; i < m; i++)
    {
        cin>>termx>>termy>>termw;
        c[termx][termy] = termw;
        c[termy][termx] = termw;
    }

    if (StartCity == EndCity)
        cout<<"1"<<" "<<weight[StartCity];
    else
        dijkstra();


    return 0;
}

最后

思想很重要,输入输出格式很重要,特殊情况也很重要。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值