九十八.蓝桥杯Java 省赛真题总结(一)
作者:互联网
第一题:世纪末的星期
解法一:EXCEL
解法二:
import java.util.Calendar;
public class Main{
public static void main(String[] args){
Calendar cl = Calendar.getInstance();
for(int year = 1999; year<10000; year+=100){
cl.set(year,11, 31);
if(cl.get(Calendar.DAY_OF_WEEK)==1){
System.out.println(year);
break;
}
}
}
}
答案为:2299
第二题:马虎的算式
public class Main{
public static void main(String[] args){
int cnt = 0;
for(int a = 1; a <= 9; a++){
for(int b = 1; b <= 9; b++){
for(int c = 1; c <= 9; c++){
for(int d = 1; d <= 9; d++){
for(int e = 1; e <= 9; e++){
if(a!=b&&a!=c&&a!=d&&a!=e&&b!=c&&b!=c&&b!=d&&b!=e&&c!=d&&c!=e&&d!=e){
if(((a*10+b)*(c*100+d*10+e))==((a*100+d*10+b)*(c*10+e))){
cnt++;
}
}
}
}
}
}
}
System.out.println(cnt);
}
}
答案是142
第三题:振兴中华
import java.util.Calendar;
public class Main{
public static void main(String[] args){
int ans = 0;
ans = dfs(0,0);
System.out.println(ans);
}
public static int dfs(int i, int j){
if(i==3||j==4)
return 1;
return dfs(i+1,j) + dfs(i,j+1);
}
}
答案是35
第四题:黄金连分数
import java.math.BigDecimal;
import java.math.BigInteger;
public class Main{
public static void main(String[] args){
BigInteger a = BigInteger.ONE;
BigInteger b = BigInteger.ONE;
for(int i = 3; i < 500; i++){
BigInteger t = b;
b = a.add(b);
a = t;
}
BigDecimal divide = new BigDecimal(a,110).divide(new BigDecimal(b,110),BigDecimal.ROUND_HALF_DOWN);
System.out.println(divide.toPlainString().substring(0,103));
}
}
答案是:0.61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113748
第六题:错误票据
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
int N = in.nextInt();
in.nextLine();
for(int i = 0; i < N; i++){
String line = in.nextLine();
String[] split = line.split(" ");
for(int j = 0; j < split.length; j++){
list.add(Integer.parseInt(split[j]));
}
}
Collections.sort(list);
int m=0,n=0;
for(int i = 1; i < list.size(); i++){
if(list.get(i) - list.get(i-1) == 2){
m = list.get(i) - 1;
}
if(list.get(i).equals(list.get(i-1))){
n = list.get(i);
}
}
System.out.println(m + " " + n);
}
}
第七题:武功秘籍
答案是7
第八题:切面条
答案是1025
第九题:猜字母
解法一:
答案是p
解法二:
public class Main{
public static void main(String[] args){
char []arr = new char[2014];
int index = 0;
for(int j = 0; j < 106; j++){
for(int i = 0; i < 19; i++){
arr[index++] = (char) ('a' + i);
}
}
int len = 2014;
while(len!=1){
int k = 0;
for(int i = 1; i < len; i+=2){
arr[k++] = arr[i];
}
len -= k;
}
System.out.println(arr[0]);
}
}
第十题:买不到的数目
解法一:裴蜀定理
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
System.out.println(m * n - m - n);
}
}
解法二:
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
Set<Integer> ss = new HashSet<Integer>();
int max = m * n;
for(int x = 0; m*x < max; x++){
for(int y = 0; m*x+n*y < max; y++){
ss.add(m*x + n*y);
}
}
for(int i = max - 1; i>=0; i--){
if(!ss.contains(i)){
System.out.println(i);
break;
}
}
}
}
第十一题:奇怪的分式
public class Main{
public static void main(String[] args){
int cnt = 0;
for(int a = 1; a < 10; a++){
for(int b = 1; b < 10; b++){
if(a==b) continue;
for(int c = 1; c < 10; c++){
for(int d = 1; d < 10; d++){
if(c==d) continue;
int gcd1 = gcd(a*c, b*d);
int gcd2 = gcd(a*10+c, b*10+d);
if(((a*c/gcd1)==((a*10+c)/gcd2)) && ((b*d/gcd1)==((b*10+d)/gcd2))){
cnt++;
}
}
}
}
}
System.out.println(cnt);
}
private static int gcd(int i, int j) {
if(j==0)
return i;
return gcd(j, i%j);
}
}
答案是14。
第十二题:带分数
import java.util.Scanner;
public class Main{
static int N;
private static int ans;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
N = in.nextInt();
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
f(arr, 0);
System.out.println(ans);
}
public static void f(int[] arr, int k){
if(k==9){ //全部确认
check(arr);
return;
}
//选第k位
for(int i = k; i < arr.length; i++){
//将第i位和第k位交换
int t = arr[i];
arr[i] = arr[k];
arr[k] = t;
//移交下一层去确认k+1位
f(arr, k+1);
//回溯(换回来)
t = arr[i];
arr[i] = arr[k];
arr[k] = t;
}
}
private static void check(int[] arr) {
//+前的字符数最多是7
for(int i = 1; i <= 7; i++){
int num1 = toInt(arr, 0, i); //+前面的一段整数
if(num1 >= N) continue; //如果此时+前的数额已经超过了N。没必要验算
// / 前面的字符数
for(int j = 1; j <= 8-i; j++){
int num2 = toInt(arr, i ,j);
int num3 = toInt(arr, i+j, 9-i-j);
if(num2 % num3 == 0 && num1 + num2 / num3 == N){
ans++;
}
}
}
}
private static int toInt(int[] arr, int pos, int len) {
int t = 1;
int ans = 0;
for(int i = pos + len -1; i >= pos; i--){
ans += arr[i]*t;
t *= 10;
}
return ans;
}
}
第十三题:扑克序列
import java.util.HashSet;
import java.util.Set;
public class Main{
public static void main(String[] args){
char[] a = {'A', 'A', '2', '2', '3', '3', '4', '4'};
f(a,0);
for(String s : set){
System.out.println(s);
}
}
static Set<String> set = new HashSet<String>();
private static void f(char[] a, int k) {
if(k == a.length){
String s = new String(a);
if(check(s))
set.add(s);
}
for(int i = k; i < a.length; i++){
char t = a[i];
a[i] = a[k];
a[k] = t;
f(a,k+1);
t = a[i];
a[i] = a[k];
a[k] = t;
}
}
private static boolean check(String s) {
if(s.lastIndexOf('A') - s.indexOf('A') == 2 &&
s.lastIndexOf('2') - s.indexOf('2') == 3 &&
s.lastIndexOf('3') - s.indexOf('3') == 4 &&
s.lastIndexOf('4') - s.indexOf('4') == 5){
return true;
}
return false;
}
}
答案是:2342A3A4
第十四题:分糖果
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = in.nextInt();
}
int ans = 0;
while(true){
int t = a[0];
for(int i = 0; i <= n-2; i++){
a[i] -= a[i]/2;
a[i] += a[i+1]/2;
if((a[i] & 1) == 1){
ans++;
a[i]++;
}
}
a[n - 1] -= a[n - 1] / 2;
a[n - 1] += t / 2;
if(((a[n - 1] & 1) == 1)){
ans++;
a[n - 1]++;
}
if(check(a)){
System.out.println(ans);
return;
}
}
}
private static boolean check(int[] a) {
int t = a[0];
for(int i = 1; i < a.length; i++){
if(a[i] != t){
return false;
}
}
return true;
}
}
第十五题:地宫取宝
import java.util.Scanner;
public class Main{
private static int n;
private static int m;
private static int k;
private static int[][] data;
private static int MOD = 1000000007;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
k = in.nextInt();
data = new int[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
data[i][j] = in.nextInt();
}
}
for(int a = 0; a < 51; a++){
for(int b = 0; b < 51; b++){
for(int c = 0; c < 14; c++){
for(int d = 0; d < 14; d++){
cache[a][b][c][d] = -1;
}
}
}
}
long ans = dfs(0, 0, -1, 0);
System.out.println(ans);
}
static long[][][][] cache = new long[51][51][14][14];
private static long dfs(int x, int y, int max, int cnt) {
if(cache[x][y][max+1][cnt] != -1) return cache[x][y][max+1][cnt];
if(x == n || y == m || cnt > k){
return 0;
}
int cur = data[x][y];
int ans = 0;
if(x == n - 1 && y == m - 1){
if(cnt == k || (cnt == k-1 && cur > max)){
return 1;
}
return ans;
}
if(cur > max){
ans += dfs(x, y + 1, cur, cnt + 1);
ans += dfs(x + 1, y, cur, cnt + 1);
}
ans += dfs(x, y + 1, max, cnt);
ans += dfs(x + 1, y, max, cnt);
cache[x][y][max+1][cnt] = ans % MOD ;
return ans;
}
}
第十六题:三角形面积
如图1所示。图中的所有小方格面积都是1。
那么,图中的三角形面积应该是多少呢?
请填写三角形的面积。不要填写任何多余内容或说明性文字。
答案是28
8*8 - 16-12-8 = 64-36=28
第十七题:立方变自身
public class Main{
public static void main(String[] args){
int cnt = 0;
for(int n = 1; n<99; n++){
int t = (int) Math.pow(n, 3);
int sum = 0;
String s = Integer.toString(t);
for(int i =0; i<s.length(); i++){
char a = s.charAt(i);
sum += Integer.parseInt(String.valueOf(a));
}
if(n==sum){
System.out.print(n +", ");
cnt++;
}
}
System.out.println();
System.out.println(cnt);
}
}
答案是6。
第十八题:三羊献瑞
题目描述
观察下面的加法算式:
其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。
请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。
public class Main{
public static void main(String[] args){
for(int b = 2; b < 9; b++){
for(int d = 2; d < 9; d++){
if(b == d) continue;
for(int g = 2; g < 9; g++){
if(g == b || g == d) continue;
int c = b + 1;
if(c == b || c == d || c == g) continue;
if(c + g <= 10) continue;
int sum = 9000 + b*100 + c*10 + d + 1000 + g*10 + b;
for(int i = 2; i < 9; i++){
if(i==b || i==d || i==g || i==c) continue;
if((sum<=10000+c*100+b*10+i) && (sum>=10000+c*100+b*10+i)){
System.out.printf("%d%d%d%d",1,0,g,b);
}
}
}
}
}
}
}
第十九题:加法变乘法
public class Main{
public static void main(String[] args){
for(int i = 1; i <= 46; i++){
for(int j = i + 2; j <= 48; j++){
if((i*(i+1)-(i+i+1))+(j*(j+1)-(j+j+1)) == 2015-1225){
System.out.println(i + " , " + j);
}
}
}
}
}
答案是16。
第二十题:牌型种数
解法一:
public class Main{
private static int ans;
public static void main(String[] args){
f(0,0);
System.out.println(ans);
}
private static void f(int k, int cnt) {
if(k > 13 || cnt > 13)return;
if(k == 13 && cnt == 13){
ans++;
return;
}
for(int i = 0; i < 5; i++){
f(k + 1 , cnt + i);
}
}
}
解法二:
public class Main{
public static void main(String[] args){
int[]a = new int[13];
int num = 0;
for(a[0] = 0; a[0] <= 4;a[0]++)
for(a[1] = 0; a[1] <= 4;a[1]++)
for(a[2] = 0; a[2] <= 4;a[2]++)
for(a[3] = 0; a[3] <= 4;a[3]++)
for(a[4] = 0; a[4] <= 4;a[4]++)
for(a[5] = 0; a[5] <= 4;a[5]++)
for(a[6] = 0; a[6] <= 4;a[6]++)
for(a[7] = 0; a[7] <= 4;a[7]++)
for(a[8] = 0; a[8] <= 4;a[8]++)
for(a[9] = 0; a[9] <= 4;a[9]++)
for(a[10] = 0; a[10] <= 4;a[10]++)
for(a[11] = 0; a[11] <= 4;a[11]++)
for(a[12] = 0; a[12] <= 4;a[12]++)
{
if(a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9]+a[10]+a[11]+a[12]==13){
num++;
}
}
System.out.println(num);
}
}
答案是:3598180
标签:Java,String,int,九十八,System,蓝桥,++,static,public 来源: https://blog.csdn.net/JiangYu200015/article/details/115528195