欧拉计划第2题

本文探讨如何计算斐波那契数列中不超过四百万的所有偶数值之和。通过使用向量存储记录,实现高效计算,并附带自定义函数验证数列元素是否为偶数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* 
*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; } }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值