[Login|Register]
New post

Show post

Search forum

Read topic
求助这段哪儿有问题吗,实在是找不到问题了 2013-03-02 19:58:13 Himdeng
#include<stdio.h>
int main()
{
    int a,b;
    int tempa,tempb;
    int temp,k;
    int i=1,j=1;
    do
    {
        scanf("%d %d",&a,&b);
        if(a>b)
        {
            tempa=b;
            tempb=a;
        }
        else
        {
            tempa=a;
            tempb=b;
        }
        for(k=tempa;k<=tempb;k++)
        {
            temp=k;
            while(temp!=1)
            {
                if(temp%2==0)
                {
                    temp=temp/2;
                    j++;
                }
                else
                {
                    temp=temp*3+1;
                    j++;
                }
                if(j>i) i=j;
            }
            j=1;
        }
        printf("%d %d %d\n",a,b,i);
        i=1;
    }while(getchar()!=EOF);
    return 0;
}
Re:求助这段哪儿有问题吗,实在是找不到问题了 2013-03-05 20:56:47 MyCodeStyle
#include<stdio.h>
int main()
{
    int a,b;
    int tempa,tempb;
    int temp,k;
    int i=1,j=1;
while(scanf("%d %d",&a,&b)!=EOF)
    {
       
        if(a>b)
        {
            tempa=b;
            tempb=a;
        }
        else
        {
            tempa=a;
            tempb=b;
        }
        for(k=tempa;k<=tempb;k++)
        {
            temp=k;
            while(temp!=1)
            {
                if(temp%2==0)
                {
                    temp=temp/2;
                    j++;
                }
                else
                {
                    temp=temp*3+1;
                    j++;
                }
                if(j>i) i=j;
            }
            j=1;
        }
        printf("%d %d %d\n",a,b,i);
        i=1;
    }
    return 0;
}
把输入部分改了一下,主要是你的输入多吸收了一个字符。
标准的输入输出程序应该是这样的
#include<stdio.h>
int main()
{
    int a,b;
    int tempa,tempb;
    int temp,k;
    int i=1,j=1;
while(scanf("%d %d",&a,&b)!=EOF)
    {
       
        if(a>b)
        {
            tempa=b;
            tempb=a;
        }
        else
        {
            tempa=a;
            tempb=b;
        }
        for(k=tempa;k<=tempb;k++)
        {
            temp=k;
            while(temp!=1)
            {
                if(temp%2==0)
                {
                    temp=temp/2;
                    j++;
                }
                else
                {
                    temp=temp*3+1;
                    j++;
                }
                if(j>i) i=j;
            }
            j=1;
        }
        printf("%d %d %d\n",a,b,i);
        i=1;
    }
    return 0;
}
另外,你的程序还可以优化一下,在1 1000000范围上运算已经很慢了,几乎无法接受。
Re:求助这段哪儿有问题吗,实在是找不到问题了 2013-03-05 21:11:33 MyCodeStyle
后面的代码没贴好,标准的输入输出应该是这样的。
#include<stdio.h>
int main()
{
  long p,q,r;
  while(scanf("%ld %ld",&p,&q)!=EOF)
    {
      if(q>r) r=q-p;
      else r=p-q;
      printf("%ld",r);
    }
    return 0;
}
Reply
Title
Message
(64K)
University of Science and Technology of China
Online Judge for ACM/ICPC
Processed in 3.6ms with 1 query(s).