PAT甲级第一次真题练习

1001 A+B Format (20)(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
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
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

作者: CHEN, Yue
单位: PAT联盟
时间限制: 400ms
内存限制: 64MB
代码长度限制: 16KB

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int main(void)
{
    int a,b,c;
    cin >> a >> b;
    c = a + b;
    if(c==0)
    {
        cout << "0";
        return 0;
    }
    if( c<0 ) 
    {
        c=-c;
        cout << "-";
    }
    int ans[20],len=0;
    bool flag[20];
    while(c != 0)
    {
        ans[++len]=c%10;
        c = c/10;
    }
    memset(flag,0,sizeof(flag));
    
    for(int i=2;i<len;i++) if(i%3==0) flag[i] = true;
    for(int i=len;i>=1;i--)
    {
        cout << ans[i];
        if(flag[i-1]) cout <<",";
    } 
    return 0;
}

1002 A+B for Polynomials (25)(25 分)提问
This time, you are supposed to find A+B where A and B are two polynomials.
Input
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 100 <= NK < ... < N2 < N1 <=1000.
Output
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

作者: CHEN, Yue
单位: PAT联盟
时间限制: 400ms
内存限制: 64MB
代码长度限制: 16KB

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath> 
#include<cstring>
using namespace std;

double N1[1001],N2[1001],N[1001];
typedef struct 
{
    int  x;
    double a;
}P; 
P p[1001];
int main(void)
{
    int k1,k2,k=0,x;
    cin >> k1;
    for(int i=1;i<=k1;i++)
    {
        cin >> x;
        cin >> N1[x];
    } 
    cin >> k2;
    for(int i=1;i<=k2;i++)
    {
        cin >> x;
        cin >> N2[x];
    }
    for(int i=1000;i>=0;i--) 
    {
        N[i]=N1[i]+N2[i];
        if(fabs(N[i])>=1e-5) 
        {
            k++;
            p[k].x=i;
            p[k].a=N[i];
        }
    }
    cout << k ;
    for(int i=1;i<=k;i++)
    {
        printf(" %d %.1lf",p[i].x,p[i].a);
    }
    return 0;
}

1003 Emergency (25)(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
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 C2 - 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, c2 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 C1 to C2.
Output
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.
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

作者: CHEN, Yue
单位: PAT联盟
时间限制: 400ms
内存限制: 64MB
代码长度限制: 16KB
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
using namespace std;
const int INF = 0x3FFFFFFF;
typedef long long LL;
LL N,M,C1,C2;
bool vis[500];
LL team[500],dis[500],num[500],w[500];
struct P{
    
    LL to;
    LL weight;
};

vector<P>p[500];
  
void dijstra(int s)
{
    fill(dis,dis+N,INF);
    fill(num,num+N,0);
    fill(w,w+N,0);
    dis[s]=0;
    num[s]=1;
    w[s]=team[s];
    for(int i=1;i<=N;i++)
    {
        int from = -1,Min = INF;
        for(int j=0;j<N;j++)
        {
            if(vis[j]==false && Min>dis[j]) 
            {
                from = j;
                Min = dis[j];
            }
        }
        if(from==-1) return ;
        vis[from] = true;
        for(int j=0;j<p[from].size();j++)
        {
            int to = p[from][j].to;
            int len = p[from][j].weight;
            if(vis[to]==false)
            {
                if(dis[to]>dis[from]+len)
                {
                    dis[to]=dis[from]+len;
                    num[to]=num[from];
                    w[to]=w[from]+team[to];
                }
                else if(dis[to]==dis[from]+len)
                {
                    num[to]+=num[from];
                    if(w[to]<w[from]+team[to])
                    {
                        w[to]=w[from]+team[to];
                    }
                }
            }
        }
    }
    cout << num[C2]<<" "<<w[C2];
}

int main(void)
{
    cin >>  N >> M >> C1 >> C2;
    for(int i=0;i<N;i++) cin >> team[i];
    for(int i=0;i<M;i++)
    {
        int from,to,len;
        cin >> from >> to >> len;
        P temp;
        temp.to=to;
        temp.weight=len;
        p[from].push_back(temp); 
        temp.to=from;
        p[to].push_back(temp); 
    }
    dijstra(C1); 
    return 0; 
} 
a.cpp: In function ‘void dijstra(int)’:
a.cpp:42:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j=0;j<p[from].size();j++)
               ~^~~~~~~~~~~~~~~

1004 Counting Leaves (30)(30 分)提问
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input
Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:
ID K ID[1] ID[2] ... ID[K]

where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.
Output
For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.
The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output "0 1" in a line.
Sample Input
2 1
01 1 02

Sample Output
0 1

作者: CHEN, Yue
单位: PAT联盟
时间限制: 400ms
内存限制: 64MB
代码长度限制: 16KB
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
int N,M,num[100],depth;
vector<int>tree[100];

void dfs(int dep,int index)
{    
    if(tree[index].size()==0) 
    {
        if(dep>depth) depth = dep;
        num[dep]++;
        return ;
    }
    for(int i=0;i<tree[index].size();i++)
    {
        int next = tree[index][i];
        dfs(dep+1,next);
    }
}
int main(void)
{
    cin >> N >> M;
    for(int i=0;i<M;i++)
    {
        int ID,k,data;
        cin >> ID >> k;
        for(int j=0;j<k;j++)
        {
            cin >> data;
            tree[ID].push_back(data);
        }
    }
    dfs(1,1);
    for(int i=1;i<=depth;i++)
    {
        cout << num[i];
        if(i!=depth) cout <<" ";
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zuimeiyujianni/p/9388088.html
今日推荐