QUT——XiaoCai (签到未成功)

来源:互联网 发布:淘宝的销售金额 编辑:程序博客网 时间:2024/06/08 03:41

总结

签到题,最水的一道题,WA了5小时,求心里阴影面积。

下次注意 x / 2 尽量以 x / 2.0 代替 (直接将计算结果带入计算 而不是另申请变量保存)

上题目

题目描述
Network association recruiting is over, WangXiaoCai go to network association training room looking for a position.

  Training room is now sit n individuals, because the body width of each person is different, so the position and size of different also.  Because of WangXiaoCai's timid, he want a position that can touch someone but do not overlap(This is obviously). Now WangXiaoCai knows the location of all people  and their body width,Would you help him find the amount of possible positions?

输入
The first line of the input data contains numbers n and m (1≤n,m≤1000). Then there follow n lines,each of them contains two
space-separated integer numbers: xi wi, where xi — x-coordinate of the
centre of the i-th people, and wi — width of its body (-1000≤xi≤1000,
1≤wi≤1000).

输出
Output the amount of possible positions of the position.

样例输入 2 2 0 4 6 2

2 3 0 4 5 2

2 2 0 4 5 2 样例输出 4 2 3 提示 m为小菜的宽度

来源
oyy(欧阳学长这题………………)

分析

主要就是求相邻两人之间的距离

如果大于王小菜的宽度就可以有两种坐法

如果等于王小菜的宽度就有1中坐法

如果小于没有

坑爹提示: 人的身体宽度可以为奇数 没错占半个格

上代码


WA了5小时的代码

#include <stdio.h>struct bu{    int people;    int wide;}a[10000];int dis[10000], wi, n, count1;void insert1(int num){    int i, j, k;    bu ntemp;    for (i = 0; i<num; i++)    {        ntemp = a[i];        for (j = 0; j <= i; j++)        {            if (a[i].people>ntemp.people)            {                for (k = i; k>j; k--)                    a[k] = a[k - 1];                a[j] = ntemp;                break;            }        }    }}int main(){    while (scanf_s("%d%d", &n, &wi) != EOF)    {        count1 = 0;        for (int i = 0; i<n; i++)            scanf_s("%d%d", &a[i].people, &a[i].wide);        insert1(n);        for (int i = 1; i<n; i++)        {            dis[i] = a[i].people - a[i - 1].people - ((a[i].wide + a[i - 1].wide) / 2);            if (dis[i]>wi)                count1 += 2;            else if (dis[i] == wi)                count1++;        }        printf("%d\n", count1 + 2);    }    return 0;}

答案代码

#include <stdio.h>#include <stdlib.h>typedef struct {    int x;    int a;} house;int cmp(const void *a, const void *b){    return ((house *)a)->x - ((house *)b)->x;}int main(){    int n, t;    //  freopen("in.txt","r",stdin);    // freopen("out.txt","w",stdout);    while (scanf("%d %d", &n, &t) != EOF){        int sum = 2, i;        house h[1000];        for (i = 0; i < n; i++) scanf("%d %d", &h[i].x, &h[i].a);        qsort(h, n, sizeof(house), cmp);        for (i = 0; i < n - 1; i++) {            int f = 0;            if (h[i].x + h[i].a / 2.0 + t <= h[i + 1].x - h[i + 1].a / 2.0) {                sum++; f++;            }            if (h[i + 1].x - h[i + 1].a / 2.0 - t >= h[i].x + h[i].a / 2.0) {                sum++; f++;            }            if (f == 2) {                if (h[i].x + h[i].a / 2.0 + t == h[i + 1].x - h[i + 1].a / 2.0) {                    sum--;                }            }        }        printf("%d\n", sum);    }    return 0;}
0 0
原创粉丝点击