目录
1.淘宝网店
链接:淘宝网店__牛客网
来源:牛客网
[编程题]淘宝网店
- 热度指数:1556 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
- 算法知识视频讲解
NowCoder在淘宝上开了一家网店。他发现在月份为素数的时候,当月每天能赚1元;否则每天能赚2元。
现在给你一段时间区间,请你帮他计算总收益有多少。输入描述:
输入包含多组数据。 每组数据包含两个日期from和to (2000-01-01 ≤ from ≤ to ≤ 2999-12-31)。 日期用三个正整数表示,用空格隔开:year month day。
输出描述:
对应每一组数据,输出在给定的日期范围(包含开始和结束日期)内能赚多少钱。示例1
输入
2000 1 1 2000 1 31 2000 2 1 2000 2 29输出
62 29
- 全部代码
// write your code here
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int year1 = sc.nextInt();
int month1 = sc.nextInt();
int day1 = sc.nextInt();
int year2 = sc.nextInt();
int month2 = sc.nextInt();
int day2 = sc.nextInt();
int money = 0;
if(year2-year1 == 0){
for(int i = month1;i <= month2;i++){
int x = primeMonth(i);
if(month1 == month2) {
money += (day2-day1+1) * x;
}
else {
if (i == month1) {
//当是第一个月
money += (getDaysByMonth(year1, month1) - day1 + 1) * x;
} else if (i == month2) {
//当是最后一个月
money += (day2) * x;
} else {
//中间月份
if (i > month1 && i < month2) {
money += getDaysByMonth(year1, i) * x;
}
}
}
}
}else{
//不同年份
for (int i = month1; i <= 12; i++) {//第一年
int x = primeMonth(i);
if (i == month1) {
money += (getDaysByMonth(year1, month1) - day1 + 1) * x;
} else {
money += getDaysByMonth(year1, i) * x;
}
}
if (year2 - year1 > 1) {
int tmp = year1 + 1;
while (tmp < year2) {
for (int i = 1; i <= 12; i++) {
int x = primeMonth(i);
money += getDaysByMonth(tmp, i)*x;
}
tmp++;
}
}
for (int i = 1; i < month2; i++) {//最后一年
int x = primeMonth(i);
money += getDaysByMonth(year2, i) * x;
}
int x = primeMonth(month2);
money += day2*x;
}
System.out.println(money);
}
}
public static int getDaysByMonth(int year,int month) {
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 29;
} else {
return 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else {
return 31;
}
}
public static int primeMonth(int month){
if(month == 2 || month == 3 || month == 5 || month == 7 || month == 11){
return 1;
}
return 2;
}
}
2.斐波那契凤尾
链接:斐波那契凤尾__牛客网
来源:牛客网
[编程题]斐波那契凤尾
- 热度指数:11695 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32M,其他语言64M
- 算法知识视频讲解
NowCoder号称自己已经记住了1-100000之间所有的斐波那契数。
为了考验他,我们随便出一个数n,让他说出第n个斐波那契数。当然,斐波那契数会很大。因此,如果第n个斐波那契数不到6位,则说出该数;否则只说出最后6位。输入描述:
输入有多组数据。 每组数据一行,包含一个整数n (1≤n≤100000)。
输出描述:
对应每一组输入,输出第n个斐波那契数的最后6位。示例1
输入
1<br/>2<br/>3<br/>4<br/>100000输出
1<br/>2<br/>3<br/>5<br/>537501
- 全部代码,要注意输出要加print("%06d\n",arr[n])
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[100001];
arr[0] = 1;
arr[1] = 1;
for(int i = 2;i < arr.length;i++){
arr[i] = (arr[i-1]+arr[i-2])%1000000;
}
while (sc.hasNext()) {
int n = sc.nextInt();
if (n<26) {
System.out.println(arr[n]);
}else {
System.out.printf("%06d\n",arr[n]);
}
}
}
}