題目來源: 藍橋杯練習系統(寫博客日期為2019.3.23,所以可能讀者看到的時候,更新了新的題) 這裡只提供每道題的我的解題代碼,僅供參考。這裡不會寫解題思路和詳解,如果有需要的話,請留言給我,我會在留言區回覆。vip題目來源dotcpp(順序跟練習系統一樣,只不過我沒有vip,所以在dotcpp ...
題目來源:
藍橋杯練習系統(寫博客日期為2019.3.23,所以可能讀者看到的時候,更新了新的題)
這裡只提供每道題的我的解題代碼,僅供參考。這裡不會寫解題思路和詳解,如果有需要的話,請留言給我,我會在留言區回覆。vip題目來源dotcpp(順序跟練習系統一樣,只不過我沒有vip,所以在dotcpp網站中看題並解題)。
試題集截圖:
入門訓練:
1-1 BEGIN-1 A+B問題
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a+b);
}
}
1-2 BEGIN-2 序列求和
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
double n = in.nextInt();
double s = (1 + n) / 2;
double t = s * n;
System.out.println((long)t);
}
}
1-3 BEGIN-3 圓的面積
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
final double PI = 3.14159265358979323;
double a = in.nextInt();
double c = a*a*PI;
System.out.printf("%.7f", c);
}
}
1-4 BEGIN-4 Fibonacci數列
public class Main {
public static void main(String[] ars){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n < 3){
System.out.println(1);
}else {
int[] a = new int[n + 1];
a[1] = 1 % 10007;
a[2] = 1 % 10007;
for (int i = 3; i <= n; i++) {
a[i] = (a[i - 1] + a[i - 2]) % 10007;
}
System.out.println(a[n]);
}
}
}
基礎練習:
2-1 BASIC-1 閏年判斷
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int y = in.nextInt();
if((y%4 == 0 && y%100 != 0) || (y%400 == 0)){
System.out.println("yes");
}else {
System.out.println("no");
}
}
}
2-2 BASIC-2 01字串
public class Main {
public static void main(String[] args){
for(int i=0; i<32; i++) {
String str = Integer.toBinaryString(i);
System.out.printf("%05d", Integer.valueOf(str));
System.out.println();
}
}
}
2-3 BASIC-3 字母圖形
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
String s = "BCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuilder str = new StringBuilder(s);
str = str.reverse();
str.append("A");
str.append(s);
int a = str.indexOf("A");
int b = a+m;
for(int i=0; i<n; i++){
System.out.println(str.substring(a, b));
a--;
b--;
}
}
}
2-4 BASIC-4 數列特征
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] s = new int[n];
for(int i=0; i<n; i++){
s[i] = in.nextInt();
}
int max = s[0];
int min = s[0];
int sum = 0;
for(int j=0; j<s.length; j++){
if(s[j] > max){
max = s[j];
}
if(s[j] < min){
min = s[j];
}
sum = sum + s[j];
}
System.out.println(max);
System.out.println(min);
System.out.println(sum);
}
}
2-5 BASIC-5 查找整數
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] s = new int[n];
for(int i=0; i<n; i++){
s[i] = in.nextInt();
}
int a = in.nextInt();
in.close();
int count = 0;
for(int j=0; j<s.length; j++){
if(s[j] == a){
count = j+1;
break;
}
}
if(count == 0){
System.out.println(-1);
}else {
System.out.println(count);
}
}
}
2-6 BASIC-6 楊輝三角形
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][n];
if(n == 1){
System.out.println(1);
}else{
a[0][0] = 1;
for(int i=1; i<n; i++){
a[i][0] = 1;
a[i][i] = 1;
if(n != 2){
for(int j=1; j<=i-1; j++){
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
}
}
// 顯示
for(int i=0; i<n; i++){
for(int j=0; j<=i; j++){
System.out.print(a[i][j]);
if(i!=j){
System.out.print(' ');
}
}
System.out.println();
}
}
}
2-7 BASIC-7 特殊的數字
public class Main {
public static void main(String[] args){
for(int dd=100; dd<=999; dd++){
String data = dd + "";
char d0 = data.charAt(0);
char d1 = data.charAt(1);
char d2 = data.charAt(2);
double sum1 = Math.pow((d0-'0'), 3) + Math.pow((d1-'0'), 3) + Math.pow((d2-'0'), 3);
if((int)sum1 == dd){
System.out.println(dd);
}
}
}
}
2-8 BASIC-8 迴文數
public class Main {
public static void main(String[] args){
StringBuilder a = new StringBuilder();
for(int i=1; i<=9; i++){
for(int j=0; j<=9; j++){
a.append(i);
a.append(j);
a.append(j);
a.append(i);
System.out.println(a.toString());
a.delete(0, a.length());
}
}
}
}
2-9 BASIC-9 特殊迴文數
2-9-1 解法一
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i=10000; i<=999999; i++){
String data = "" + i;
int sum = 0;
for(int k=0; k<data.length(); k++){
sum = sum + (data.charAt(k)-'0');
}
int len = data.length();
int count = len/2;
// 計算相等的個數,如果是2,3則滿足
StringBuilder flag = new StringBuilder();
for(int j=0; j<count; j++){
if(data.charAt(j) == data.charAt(len-j-1)){
flag.append(1);
}else{
flag.append(0);
}
}
if((flag.toString().equals("111") || flag.toString().equals("11")) && (n == sum)){
System.out.println(i);
}
else{
continue;
}
}
}
}
2-9-2 解法二
import java.util.Scanner;
public class Main {
public static void f5(int n){
/* 五位數處理代碼 */
StringBuilder anser1 = new StringBuilder();
// 測試第1位的值
for(int i=1; i<=9; i++){
// 測試第2位的值
for(int j=0; j<=9; j++){
// 計算第3位的值
int third_number = n - 2*i - 2*j;
if(third_number>=0 && third_number<=9){
anser1.append(i);
anser1.append(j);
anser1.append(third_number);
anser1.append(j);
anser1.append(i);
System.out.println(anser1);
anser1.delete(0, anser1.length());
}else{
continue;
}
}
}
}
public static void f6(int n){
/* 六位數處理代碼 */
StringBuilder anser2 = new StringBuilder();
// temp: 前三位相加的值
int temp = n/2;
// 測試第1位的值
for(int i=1; i<=9; i++){
// 測試第2位的值
for(int j=0; j<=9; j++){
// 計算第3位的值
int third_number2 = temp-i-j;
if(third_number2>=0 && third_number2<=9){
anser2.append(i);
anser2.append(j);
anser2.append(third_number2);
anser2.append(third_number2);
anser2.append(j);
anser2.append(i);
System.out.println(anser2);
anser2.delete(0, anser2.length());
}else{
continue;
}
}
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
f5(n);
// 偶數才可能是6位
if(n%2==0){
f6(n);
}
}
}
2-10 BASIC-10 十進位轉十六進位
import java.util.Scanner;
public class Main {
public static char f(long a){
char temp = ' ';
if(a<=9){
temp = (char)(a+48);
}else {
switch ((int) a) {
case 10:
temp = 'A';
break;
case 11:
temp = 'B';
break;
case 12:
temp = 'C';
break;
case 13:
temp = 'D';
break;
case 14:
temp = 'E';
break;
case 15:
temp = 'F';
break;
}
}
// System.out.println(temp);
return temp;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
long a = in.nextLong();
StringBuilder ss = new StringBuilder();
if(a == 0){
ss.append(0);
}else {
while (a != 0) {
long number = a % 16;
char s = f(number);
ss.append(s);
a = a / 16;
}
}
System.out.println(ss.reverse().toString());
}
}
2-11 BASIC-11 十六進位轉十進位
import java.util.*;
public class Main {
public static int f(char a){
int x=0;
if(a>='0' && a<='9'){
x = a-'0';
}else{
switch (a){
case 'A': x = 10;break;
case 'B': x = 11;break;
case 'C': x = 12;break;
case 'D': x = 13;break;
case 'E': x = 14;break;
case 'F': x = 15;break;
default:break;
}
}
return x;
}
public static void h(String str){
int len = str.length();
double x = 0;
for(int i=0; i<len; i++){
int value = f(str.charAt(i));
x = value*Math.pow(16, len-1-i) + x;
}
System.out.println((long)x);
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String a = in.next();
in.close();
h(a);
}
}
2-12 BASIC-12 十六進位轉八進位
import java.util.Scanner;
public class Main {
public static String f(String s){
int len = s.length();
StringBuilder str = new StringBuilder();
for(int i=0; i<len; i++){
switch (s.charAt(i)){
case '0': str.append("0000");break;
case '1': str.append("0001");break;
case '2': str.append("0010");break;
case '3': str.append("0011");break;
case '4': str.append("0100");break;
case '5': str.append("0101");break;
case '6': str.append("0110");break;
case '7': str.append("