| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1833 人关注过本帖
标题:一个爬虫的问题,,原题是英文的
只看楼主 加入收藏
xiaolaoshucj
Rank: 2
等 级:论坛游民
帖 子:26
专家分:15
注 册:2011-6-18
收藏
得分:0 
以下是引用beyondyf在2011-8-10 22:51:27的发言:

也来凑个热闹
#include
int main()
{
  int n, u, d;
  while(scanf("%d%d%d", &n, &u, &d), n)
    printf("%d\n", (n - d - 1) / (u - d) + 1);
  return 0;
}

楼上的代码貌似得不出题目的要求。
下面是我写的函数,
程序代码:
int countmins(int n, int u, int d)
{
    if (n < 0 || n > 100 || d > u || d < 0 || u < 0) {
        printf("input error!\n")
        return -1;
    }
    int i;
    int step = u - d;
    for (i = 1; n - 1 - i * step > step; ++i)
        ;
    return i * 2  + 1;
}

2011-08-11 10:32
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:0 
不好意思,代码是直接敲上来的,手头没编译器。笔误,少敲了一个数字。
程序代码:
#include
int main()
{
  int n, u, d;
  while(scanf("%d%d%d", &n, &u, &d), n)
    printf("%d\n", (n - d - 1) / (u - d) * 2 + 1);
  return 0;
}


 

重剑无锋,大巧不工
2011-08-11 11:44
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:0 
好象还是存在漏洞。题目中只提到d < u,但对n没有限制。当n < u时,尤其n < d + 1时,上面的代码结果是错误的。老杨的代码也存在这样的问题。再修改一下。
程序代码:
#include<stdio.h>
int main()
{
  int n, u, d;
  while(scanf("%d%d%d", &n, &u, &d), n)
    if(n <= u) printf("1\n"); else printf("%d\n", (n - d - 1) / (u - d) * 2 + 1);
  return 0;
}


 

重剑无锋,大巧不工
2011-08-11 11:54
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:0 
回复 13楼 beyondyf
楼上提醒的是  但是OJ貌似没有这样的测试数据 否则我的程序是过不了

                                         
===========深入<----------------->浅出============
2011-08-11 17:14
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
程序代码:
/*
An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u inches every minute, but then has to rest a minute
before climbing again. During the rest, it slips down d inches. The process of climbing and resting then repeats. How long before the worm
climbs out of the well? We'll always count a portion of a minute as a whole minute and if the worm just reaches the top of the well at the
end of its climbing, we'll assume the worm makes it out.

Input

 There will be multiple problem instances. Each line will contain 3 positive integers n, u and d. These give the values mentioned in the

 paragraph above. Furthermore, you may assume d < u and n < 100. A value of n = 0 indicates end of output.

Output

 Each input instance should generate a single integer on a line, indicating the number of minutes it takes for the worm to climb out of the

 well.

Sample Input

 10 2 1

 20 3 1

 0 0 0

Sample Output

 17

 19
*/

#include <stdio.h>
#include <conio.h>
#include <string.h>

int IsDigit(char Character);
void EatSpaces(char* String);
int Str2Int(char* String);

void main(void)
{
    char Buffer[1024];
    int Deep;                    // 洞的深度
    int Up;                        // 每分钟爬升的高度
    int Down;                    // 每分钟下落的高度
    double Speed;                // 速率(英寸/秒)
    int Total_Time;                // 总时间(秒)
    int Time;                    // 分段时间(秒)
    double y;                    // y轴坐标
    int Uping;                    // 处于爬升阶段

    printf_s("蠕虫爬升问题\n\n");

    for (; ;)
    {
        // 接收数据
        do
        {
            printf_s("洞的深度(英寸) [输入0结束程序] n = ");
            Buffer[0] = '\0';
            gets_s(Buffer, 79);
            EatSpaces(Buffer);
            Deep = Str2Int(Buffer);
        } while (Deep < 0);
        if (Deep == 0)
        {
            break;
        }
        do
        {
            printf_s("每分钟爬升的高度(英寸) u = ");
            Buffer[0] = '\0';
            gets_s(Buffer, 79);
            EatSpaces(Buffer);
            Up = Str2Int(Buffer);
        } while (Up <= 1);
        do
        {
            printf_s("每分钟下落的高度(英寸) d = ");
            Buffer[0] = '\0';
            gets_s(Buffer, 79);
            EatSpaces(Buffer);
            Down = Str2Int(Buffer);
        } while ((Down <= 0) || (Down >= Up));

        // 模拟爬升运动
        Total_Time = 0;
        Time = 0;
        y = -Deep + 1;            // 考虑蠕虫本身长度1英寸
        Uping = 1;
        while (y < 0)
        {
            if (Uping)
            {
                if (Time < 59)
                {
                    Speed = Up / 60.0;
                }
                else
                {
                    Uping = 0;
                    Time = 0;
                }
            }
            if (!Uping)
            {
                if (Time < 59)
                {
                    Speed = -Down / 60.0;
                }
                else
                {
                    Uping = 1;
                    Time = 0;
                }
            }
            y += Speed;
            Time++;
            Total_Time++;
        }
        printf_s("爬行总时间为%.2f分钟\n\n", Total_Time / 60.0);
    }
}

// 判断一字符是否数字字符
int IsDigit(char Character)
{
    if ((Character >= '0') && (Character <= '9'))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

// 删除字符串的空格
void EatSpaces(char* String)
{
    int i = 0;
    int j = 0;

    while (( *(String + i) = *(String + j++)) != '\0')
    {
        if (*(String + i) != ' ')
        {
            i++;
        }
    }
    return;
}

// 字符串转为整数
int Str2Int(char* String)
{
    int IsNegative = 0;
    int nVar = 0;

    if (*String == '-')
    {
        IsNegative = 1;
        String++;
    }
    while (*String && IsDigit(*String))
    {
        nVar = nVar * 10 + *String - '0';
        String++;
    }
    if (IsNegative)
    {
        nVar = -nVar;
    }
    return nVar;
}


[ 本帖最后由 TonyDeng 于 2011-8-14 04:21 编辑 ]

授人以渔,不授人以鱼。
2011-08-14 04:18
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:0 
标准库对于您来说就是浮云。
2011-08-16 19:27
快速回复:一个爬虫的问题,,原题是英文的
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040126 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved