13年蓝桥杯javaB组
1.末世纪的星期天2.马虎的算式3.振兴中华4.黄金分数割5.有理数类6.三部排序7.错误票据8.幸运数
1.末世纪的星期天
曾有邪教称1999年12月31日是世界末日。当然该谣言已经不攻自破。
还有人称今后的某个世纪末的12月31日,如果是星期一则会是世界末日
有趣的是,任何一个世纪末的年份的12月31日都不可能是星期一!!
于是,“谣言制造商”又修改为星期日是世界末日
1999年的12月31日是星期五,
请问:未来哪一个离我们最近的一个世纪末年(即xx99年)的12月31日正好是星期天(即星期日)?
普通方法
public class Main {
public static void main(String
[] args
) {
int week
=5;
for(int i
=2000;i
<10000;i
=i
+1) {
if(isTrue(i
)) {
week
= 366%7+week
;
}else {
week
= 365%7+week
;
}
if(week
>7) {
week
= week
%7;
}
if(week
==7) {
if(i
%100==99) {
System
.out
.println(i
);
break;
}
}
}
}
public static boolean isTrue(int year
) {
if(year
%4==0&&year
%100!=0||year
%400==0){
return true;
}else{
return false;
}
}
}
快捷方法
package 末世纪的星期
;
import java
.util
.Calendar
;
import java
.util
.GregorianCalendar
;
public class Main {
public static void main(String
[] args
) {
int day
= -1;
for(int i
=2099;i
<5000;i
=i
+100){
Calendar calendar
= new GregorianCalendar(i
,11,31);
day
= calendar
.get(Calendar
.DAY_OF_WEEK
);
if(day
==1) {
System
.out
.println(i
);
break;
}
}
}
}
2.马虎的算式
小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了。
有一次,老师出的题目是:36 x 495 = ?
他却给抄成了:396 x 45 = ?
但结果却很戏剧性,他的答案竟然是对的!!
因为 36 * 495 = 396 * 45 = 17820
类似这样的巧合情况可能还有很多,比如:27 * 594 = 297 * 54
假设 a b c d e 代表1~9不同的5个数字(注意是各不相同的数字,且不含0)
能满足形如: ab * cde = adb * ce 这样的算式一共有多少种呢
public class Main {
public static void main(String
[] args
) {
int count
= 0;
for(int a
=1;a
<10;a
++) {
for(int b
=1;b
<10;b
++) {
for(int c
=1;c
<10;c
++) {
for(int d
=1;d
<10;d
++) {
for(int e
=1;e
<10;e
++) {
if(a
==b
||a
==c
||a
==d
||a
==e
||b
==c
||b
==d
||b
==e
||c
==d
||c
==e
||d
==e
) {
continue;
}
int temp_1
= (a
*10+b
)*(c
*100+d
*10+e
);
int temp_2
= (a
*100+d
*10+b
)*(c
*10+e
);
if(temp_1
== temp_2
) {
count
++;
}
}
}
}
}
}
System
.out
.println(count
);
}
}
3.振兴中华
public class Main {
public static void main(String
[] args
) {
int count
=f(0,0);
System
.out
.println(count
);
}
private static int f(int i
,int j
) {
if(i
==3||j
==4) return 1;
return f(i
+1,j
)+f(i
,j
+1);
}
}
public class Main {
public static char[][] cArr
= {{'从','我','做','起','振'},
{'我','做','起','振','兴'},
{'做','起','振','兴','中'},
{'起','振','兴','中','华'}};
public static int count
=0;
public static void main(String
[] args
) {
int x
=0,y
=0;
String str
= "";
Fun(str
, x
, y
);
System
.out
.println(count
);
}
public static void Fun(String str
,int x
,int y
) {
str
= str
+ cArr
[x
][y
];
if(x
==3&&y
==4){
if("从我做起振兴中华".equals(str
)) {
count
++;
}
}else {
if(x
<3) {
Fun(str
, x
+1, y
);
}
if(y
<4) {
Fun(str
, x
, y
+1);
}
}
}
}
4.黄金分数割
package 黄金分割数
;
import java
.math
.BigDecimal
;
public class Main {
public static BigDecimal res1
= new BigDecimal(1);
public static BigDecimal res
= new BigDecimal(0);
public static void main(String
[] args
) throws Exception
{
f(300);
res
= res
.setScale(100, BigDecimal
.ROUND_HALF_UP
);
System
.out
.println(res
);
}
public static BigDecimal
f(int n
) {
if (n
== 1) {
return res1
;
}
BigDecimal a
= new BigDecimal(1);
return res
= a
.divide(a
.add(f(n
- 1)), 1000, BigDecimal
.ROUND_HALF_UP
);
}
}
5.有理数类
package 有理数类
;
public class Main {
public static void main(String
[] args
) {
Rational a
= new Rational(1,3);
Rational b
= new Rational(1,6);
Rational c
= a
.add(b
);
System
.out
.println(a
+ "+" + b
+ "=" + c
);
}
}
package 有理数类
;
class Rational
{
private long ra
;
private long rb
;
private long gcd(long a
, long b
){
if(b
==0) return a
;
return gcd(b
,a
%b
);
}
public Rational(long a
, long b
){
ra
= a
;
rb
= b
;
long k
= gcd(ra
,rb
);
if(k
>1){
ra
/= k
;
rb
/= k
;
}
}
public Rational
add(Rational x
){
return new Rational(ra
*x
.rb
+rb
*x
.ra
, rb
*x
.rb
);
}
public Rational
mul(Rational x
){
return new Rational(ra
*x
.ra
, rb
*x
.rb
);
}
public String
toString(){
if(rb
==1) return "" + ra
;
return ra
+ "/" + rb
;
}
}
6.三部排序
package 三部排序
;
public class Main {
public static void main(String
[] args
) {
int x
[]= {25,18,-2,0,16,-5,33,21,0,19,-16,25,-3,0};
sort(x
);
for(int i
=0;i
<x
.length
;i
++) {
System
.out
.print(x
[i
]+" ");
}
}
public static void sort(int[] x
)
{
int p
= 0;
int left
= 0;
int right
= x
.length
-1;
while(p
<=right
){
if(x
[p
]<0){
int t
= x
[left
];
x
[left
] = x
[p
];
x
[p
] = t
;
left
++;
p
++;
}
else if(x
[p
]>0){
int t
= x
[right
];
x
[right
] = x
[p
];
x
[p
] = t
;
right
--;
}
else{
p
++;
}
}
}
}
7.错误票据
注意一点list集合判断是否相等要用equal方法不能用=
import java
.util
.ArrayList
;
import java
.util
.Collections
;
import java
.util
.List
;
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner e
= new Scanner(System
.in
);
int r1
=-1,r2
=-1;
int len
= Integer
.valueOf(e
.nextLine());
ArrayList
<Integer> list
= new ArrayList<>();
for(int i
=0;i
<len
;i
++) {
String str
= e
.nextLine().trim();
String
[] arr
= str
.split(" ");
for(int j
=0;j
<arr
.length
;j
++) {
list
.add(Integer
.valueOf(arr
[j
]));
}
}
Collections
.sort(list
,Collections
.reverseOrder());
for(int i
=list
.size()-2;i
>=0;i
--) {
if(list
.get(i
).equals((list
.get(i
+1)))) {
r2
=list
.get(i
);
}
if(list
.get(i
)!=(list
.get(i
+1)+1)) {
if(list
.get(i
)!=(list
.get(i
+1))) {
if(list
.get(i
-1)!=(list
.get(i
))) {
r1
=list
.get(i
)-1;
}
}
}
}
System
.out
.println(r1
+" "+r2
);
}
}
8.幸运数
题目描述
幸运数是波兰数学家乌拉姆命名的。它采用与生成素数类似的“筛法”生成。
首先从1开始写出自然数1,2,3,4,5,6,…
1 就是第一个幸运数。
我们从2这个数开始。把所有序号能被2整除的项删除,变为:
1 _ 3 _ 5 _ 7 _ 9 …
把它们缩紧,重新记序,为:
1 3 5 7 9 … 。这时,3为第2个幸运数,然后把所有能被3整除的序号位置的数删去。注意,
是序号位置,不是那个数本身能否被3整除!! 删除的应该是5,11, 17, …
此时7为第3个幸运数,然后再删去序号位置能被7整除的(19,39,…)
最后剩下的序列类似:
1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, …
本题要求:
输入两个正整数m n, 用空格分开 (m < n < 1000*1000)
程序输出 位于m和n之间的幸运数的个数(不包含m和n)。
例如:
用户输入:
1 20
程序输出:
5
例如:
用户输入:
30 69
程序输出:
8
资源约定:
峰值内存消耗(含虚拟机) < 64M
CPU消耗 < 2000ms
请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。
import java
.util
.ArrayList
;
import java
.util
.Arrays
;
import java
.util
.List
;
import java
.util
.Scanner
;
public class Main {
public static void main(String
[] args
) {
Scanner e
= new Scanner(System
.in
);
int m
= 30;
int n
= 74;
int count
= 0;
int[] arr
= new int[n
];
int[] temp
= new int[n
];
List
<Integer> rs
= new ArrayList<Integer>();
int leng
= arr
.length
;
for(int i
=0;i
<n
;i
++) {
arr
[i
]=i
+1;
}
rs
.add(arr
[count
++]);
System
.out
.println(Arrays
.toString(arr
)+"-------------------"+arr
[count
-1]+" "+count
);
if(n
%2==0) {
leng
= n
/2;
}else {
leng
= n
/2+1;
}
for(int i
=0;i
<leng
;i
++) {
temp
[i
] = arr
[2*i
];
}
arr
= temp
;
rs
.add(arr
[count
++]);
System
.out
.println(Arrays
.toString(arr
)+"-------------------"+arr
[count
-1]+" "+count
);
while(true) {
temp
= new int[arr
.length
];
if(arr
[arr
[count
-1]-1]==0) {
break;
}else {
int len
= 0;
for(int i
=0;i
<arr
.length
;i
++) {
if((i
+1)%arr
[count
-1]==0) {
continue;
}else {
temp
[len
++] = arr
[i
];
}
}
arr
= temp
;
rs
.add(arr
[count
++]);
}
}
int number
= 0;
System
.out
.println(rs
.toString());
while(true) {
if(arr
[count
]==0) {
break;
}
rs
.add(arr
[count
++]);
}
System
.out
.println(rs
.toString());
for(int l
:rs
) {
if(l
>m
&& l
<n
) {
number
++;
}
}
System
.out
.println(number
);
}
}