心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0
题目大意:给出n个数字的序列,如果全部的相邻两个数字的差取遍从1到n-1的整数,输出"Jolly",否则输出"Not jolly"。比如1 4 2 3,相邻两数之差分别为3 2 1,于是输出"Jolly"。
使用一个bool数组:如果vis[i]==true,i出现过;否则没有出现过。要注意到差值可能很大,因此要判断是否会发生数组越界。
以下是我的代码:
#include<stdio.h>
long Abs(long x)
{
    
return (x>0?x:-x);
}
bool jolly(bool a[],long n)
{
    
for(long i=1;i<=n-1;i++)
      
if(!a[i]) return false;
    
return true;
}
int main()
{
    
const long maxn=3007;
    
long n,a[maxn];
    
bool vis[maxn];
    
while(scanf("%ld",&n)==1)
    {
       
for(long i=1;i<=n;i++) scanf("%ld",&a[i]);
       
//  Read In
       for(long i=1;i<=n;i++) vis[i]=false;
       
//  Clear
       for(long i=1;i<=n-1;i++)
       {
          
long t=Abs(a[i+1]-a[i]);
          
if(t<=n-1) vis[t]=true;
       }
       
if(jolly(vis,n)) printf("Jolly\n");
       
else printf("Not jolly\n");
    }
return 0;
}


posted on 2010-01-22 12:35 lee1r 阅读(900) 评论(2)  编辑 收藏 引用 所属分类: 题目分类:基础/模拟

FeedBack:
# re: UVa 10038 Jolly Jumper
2010-10-17 23:19 | hossein
Hi dear !
can u explain me more about how your program get the data ?
i can't understand how this program make a difference between sequences that they seperate with a '\n' .

Thanks dude :D  回复  更多评论
  
# re: UVa 10038 Jolly Jumper
2010-10-21 22:07 | Lee1R
@hossein
I am glad to receive a message from a foreign friend!
I guess you are not a Chinese,But I am.
My English is not so good...

First,you should know scanf() is a function that will return a value(you can google "scanf()").
Then,if the program fails to get data in present line, it will try next line automatically until the end of file.
That's all.  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理