*斐波那契凤尾
NowCoder号称自己已经记住了1-100000之间所有的斐波那契数。
为了考验他,我们随便出一个数n,让他说出第n个斐波那契数。当然,斐波那契数会很大。因此,如果第n个斐波那契数不到6位,则说出该数;否则只说出最后6位。输入描述: 输入有多组数据。 每组数据一行,包含一个整数n (1≤n≤100000)。
输出描述: 对应每一组输入,输出第n个斐波那契数的最后6位。
import java.util.*;
public class Main{
public static void main(String[] args){
int border=-1;
long[] ans=new long[100000];
ans[0]=1;
ans[1]=2;
for(int i=2;i<100000;i++){
long res=ans[i-1]+ans[i-2];
if(border==-1 && res>=1000000){
border=i+1;
}
ans[i]=res%1000000;
}
Scanner scan=new Scanner(System.in);
while(scan.hasNext()){
int n=scan.nextInt();
long f=ans[n-1];
if(n<border){
System.out.printf("%d\n",f);
}else{
System.out.printf("%06d\n",f);
}
}
}
}
*淘宝网站
NowCoder在淘宝上开了一家网店。他发现在月份为素数的时候,当月每天能赚1元;否则每天能赚2元。
现在给你一段时间区间,请你帮他计算总收益有多少。输入描述: 输入包含多组数据。
每组数据包含两个日期from和to (2000-01-01 ≤ from ≤ to ≤ 2999-12-31)。
日期用三个正整数表示,用空格隔开:year month day。
输出描述: 对应每一组数据,输出在给定的日期范围(包含开始和结束日期)内能赚多少钱。
[题目解析] :
这是一个变相的日期计算器。只不过2. 3. 5、7. 11月算1天,其他7个月算2天.
[解题思路] :
既然是一个变相的日期计算器,那就写一个日期计算器,然后加以修改即可.那么,日期计算器怎么写 呢?
日期计算器的话,我们将会把日期计算分为三个部分:第一个不足-年的年份,最后一个不足一年的年份,和中间的足年年份。足年年份我们只需要判断闰年后加365或366就行了。不足年,我们就要求出这 个日期是这一年的第几天。
假设要求的是1994年5月27日到2003年4月29日, 那么,我们就要先求出5月27日是这一年的第几天,然后判断1994年不是闰年,不是,所以用365减去这个天数,就得到结果了。 本题中第一天也要算,所以还要加上这一天。
然后再算出4月29日是2003年的第几天,就可以解决问题 了。所以,我们需要-个函数,功能是给出一个年月日,求出这是这一年的第几天。
这些功能全部实现后,再去改造使得1、4、6、8、9.410.12月的天数翻倍,那么程序就全部完成了。
import java.util.*;
public class Main{
private static boolean isLeapYear(int year){
return year%400==0 || (year%4==0&&year%100!=0);
}
private static int profitofYear(int year){
return 2*31
+ 1*28
+ 1*31
+ 2*30
+ 1*31
+ 2*30
+ 1*31
+ 2*31
+ 2*30
+ 2*31
+ 1*30
+ 2*31
+(isLeapYear(year)? 1:0);
}
private static boolean isPrime(int month){
return month==2||month==3||month==5||month==7||month==11;
}
private static int profitThisYear(int year,int month,int day){
int profit=0;
if(!isPrime(month)){
profit=day*2;
}else{
profit=day;
}
while(--month>0){
switch(month){
case 1: case 8: case 10: case 12:
profit+=62;
break;
case 3: case 5: case 7:
profit+=31;
break;
case 4: case 6: case 9:
profit+=60;
break;
case 11:
profit+=30;
break;
default:
profit+=(28+(isLeapYear(year)? 1:0));
break;
}
}
return profit;
}
public static void main(String[] args){
int year1,month1,day1,year2,month2,day2;
int profit=0;
Scanner scan=new Scanner(System.in);
while(scan.hasNext()){
year1=scan.nextInt();
month1=scan.nextInt();
day1=scan.nextInt();
year2=scan.nextInt();
month2=scan.nextInt();
day2=scan.nextInt();
profit=profitofYear(year1)-profitThisYear(year1,month1,day1-1);
profit+= profitThisYear(year2,month2,day2);
if(year1==year2){
profit-=profitofYear(year1);
}
for(int i=year1+1;i<year2;i++){
profit+=profitofYear(i);
}
System.out.println(profit);
}
}
}