/*
*Copyright(c) 2011,烟台大学计算机学院学生
*All rights reserved.
*文件名称:欧拉计划第二题
*题目:Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
说明:利用向量存储了记录,便于以后也许能用,也是为了练洗一下向量的操作
*作 者:Mr_chang
*完成日期: 2012年 12月16 日
*/
package test;
import java.util.Vector;
public class Fieboo
{ private static int sum=2;
public static void main(String[] args)
{
boolean b=true;
int a=4000000;
Vector<Integer> v=new Vector<Integer>();
v.add(1);
v.add(2);
while(b)
{
v.add(v.get(v.size()-1)+v.get(v.size()-2));
if(v.get(v.size()-1)>=a)
{
v.remove(v.size()-1);
b=false;
break;
}
if(jugValue(v.get(v.size()-1),v))
sum+=v.get(v.size()-1);
}
System.out.println(sum);
System.out.println(v);
}
public static boolean jugValue(int i,Vector<Integer> v)
{
String sr=Integer.toString(v.get(v.size()-1));
int tem=sr.charAt(sr.length()-1);
if(tem%2==0)
return true;
else
return false;
}
}
欧拉计划第2题
最新推荐文章于 2022-04-10 20:58:05 发布