tctony

Focus on linux,emacs,c/c++,python,algorithm...

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  17 随笔 :: 0 文章 :: 7 评论 :: 0 Trackbacks

Fibonacci Again

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1911    Accepted Submission(s): 905


Problem Description
There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
 

Input
Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000).
 

Output
Print the word "yes" if 3 divide evenly into F(n).

Print the word "no" if not.
 

Sample Input
0
1
2
3
4
5
 

Sample Output
no
no
yes
no
no
no
#include<iostream>
using namespace std;

int main(){
    
int n;
    
while(cin>>n){
        
if(n==0||n==1)
            cout
<<"no"<<endl;
        
else {
            
if((n-2)%4==0)
                cout
<<"yes"<<endl;
            
else 
                cout
<<"no"<<endl;
        }

    }

    
return 0;
}

简单题,不过不是最好的算法。15ms0k内存。。
0ms0k内存是怎么做出来的呢?
posted on 2007-12-04 07:40 tctony 阅读(1035) 评论(3)  编辑 收藏 引用

评论

# re: HDU 1021 2010-04-14 23:27 蓝色书虫
有点小小的问题想请教下:
看我测试了100组数据:代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main(){

int i=0;
long a[10000] ={0};
a[0] =7;
a[1] =11;

for(i=0;i<=100;i++){
//第0组,第一组数据直接输出
if(i==0||i==1){
if(a[0]%3==0)
printf("%d %d yes\n",i,i%8);
else printf("%d %d no\n",i,i%8);
continue;
}
//用a【i】存储F(i)
a[i] =a[i-1]+a[i-2];
if(a[i]%3==0)
printf("%d %d yes\n",i,i%8);
else printf("%d %d no\n",i,i%8);
}//for

getch();
return 0;
}//main


当i=43之后,规律就打破了。不知道什么原因??这能说明这规律出了问题吗?  回复  更多评论
  

# re: HDU 1021 2010-04-15 08:24 tctony
long类型溢出了  回复  更多评论
  

# re: HDU 1021 2010-04-15 10:25 蓝色书虫
哦。。。是的。。。谢谢  回复  更多评论
  


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