PAT-Thare's Road

1002. A+B for Polynomials  ★

  Tips:

  1. double数组必须初始化为0.默认虽为0,但是是不准确的0.
  2. 第2行输入的数据在加之前要判断num[i]是不是0,加之后也要判断下是否为0,再对count进行响应操作.
  3. 最后打印数据的时候空格放在数据前打印,若放在后面会造成最后一个数据也打印一个空格,从而格式错误.

int main() {
    double num[LEN];
    int k,a,count,i;
    double b;
    while(scanf("%d",&k) != EOF){
        count = 0;
        for(i = 0;i < LEN;i++)
            num[i] = 0;
        for(i = 0;i < k;i++){
            scanf("%d",&a);
            scanf("%lf",&b);
            num[a] += b;
            count ++;
        }
        scanf("%d",&k);
        for(i = 0;i < k;i++){
            scanf("%d",&a);
            scanf("%lf",&b);
            if(num[a] == 0)
                count ++;
            num[a] += b;
            if(num[a] == 0)
                count --;
        }
        printf("%d",count);
        if(count != 0){
            for(i = LEN - 1;i >= 0;i--){
                if(num[i] != 0){
                    printf(" %d %.1lf",i,num[i]);
                    num[i] = 0;
                }
            }
        }
        printf("\n");
    }
}

 

PAT-Thare's Road

时间: 2024-10-29 21:22:22

PAT-Thare's Road的相关文章

PAT 1072. Gas Station (30)

A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible.  However it must guarantee that all the houses are in its service range. Now given the map o

【PAT】Emergency(最短路条数-SPFA)

[PAT]Emergency(最短路条数-SPFA) 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 betw

PAT 1009 说反话 C语言

给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格. 输出格式:每个测试用例的输出占一行,输出倒序后的句子. 输入样例: Hello World Here I Come 输出样例: Come I Here World Hello 1 #include<stdio.h> 2 #

PAT 1006 换个格式输出 C语言

让我们用字母B来表示"百".字母S表示"十",用"12...n"来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例如234应该被输出为BBSSS1234,因为它有2个"百".3个"十".以及个位的4. 输入格式:每个测试输入包含1个测试用例,给出正整数n(<1000). 输出格式:每个测试用例的输出占一行,用规定的格式输出n. 输入样例1: 234 输出样例1: BBSSS1

poj 3204 Ikki&#39;s Story I - Road Reconstruction

Ikki's Story I - Road Reconstruction 题意:有N个顶点和M条边N, M (N ≤ 500, M ≤ 5,000)  ,试图改变图中的一条边使得从0到N-1的流量增加:问这样的边有几条? 思路:刚最大流入门,之后一看就觉得满流的边就是答案..真是太天真了.之后看了题解,发现满流只是前提(即使满流是几次残量的叠加也是一样),还有一个条件是,该路径的最大流量只受该边影响:即可以从S和T遍历到该边的两个端点,这就是为什么之后还要dfs给点涂色的原因:涂色前要对残余网络

hdoj 1596 find the safest road 【dijkstra】

find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9144    Accepted Submission(s): 3225 Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1

俺也晒晒pat代码!

最近在练习pat,很多题目不是很好做,一直找不到思路.通过google题目名称可以找到很多前辈的做题记录,这极大的方便了后来者.在此默默谢谢他们! 不过这同时也带了一些问题,有些前辈可能是急于出结果,所以使用了非常麻烦的方法把题目做出来了,有些后来者不经思考草草学习了就也这么做了...俺觉得这不应当是刷题所追求的目标,俺觉得既然练习旧的题目就应当追求超越前人,多想想有没有更巧妙,更简便的方法.否则科学技术也就停滞不前了... 不过俺又想了想或许很多“大牛”的确使用了简单的方法,只是他们没有时间或

HDU4081 Qin Shi Huang&#39;s National Road System【Kruska】【次小生成树】

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3979    Accepted Submission(s): 1372 Problem Description During the Warring States Period of ancient China(4

pat 1068 动态规划/Fina More Conis

1068. Find More Coins (30) Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special re

Restoring Road Network

D - Restoring Road Network Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads. The following are