2020年第十一届蓝桥杯javaA组答案解析(部分)
作者:互联网
文章目录
一、门牌制作
package _2020_Java_A;
import java.util.*;
public class _01门牌 {
private static int ans;
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<=2020;i++) {
ans += num(i);
}
System.out.print(ans);
}
private static int num(int n) {
// TODO Auto-generated method stub
int ans = 0;
while(n!=0) {
int m = n%10;//取最低位
if(m==2) ans ++;
n = n/10;
}
return ans;
}
//624
}
二、分数
package _2020_Java_A;
import java.util.*;
public class _02分数 {
private static int ans;
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=1;i<=2020;i++) {
for(int j=1;j<=2020;j++) {
if(gcd(i,j)==1) ans ++;
}
}
System.out.print(ans);
}
private static int gcd(int m, int n) {
// TODO Auto-generated method stub
if(m==0)return n;
else return gcd(n%m,m);
}
//2481215
}
三、蛇形填数
package _2020_Java_A;
public class _03C填数 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n = 20;
int ans = 0;
for(int i=1;i<=2*n-2;i++) {
//前面的2n-2长
ans += i;
}
ans += n;
System.out.print(ans);
}
//761
}
六、成绩分析
package _2020_Java_A;
import java.util.*;
public class _06F成绩 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] s = new int[n];//存放成绩
float avg;
float total = 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for(int i=0;i<n;i++) {
s[i] = sc.nextInt();
if(s[i]>max) max = s[i];
if(s[i]<min) min = s[i];
total += s[i];
}
avg = total/n;
System.out.println(max);
System.out.println(min);
System.out.printf("%.2f\n",avg);
sc.close();
}
}
七、回文日期
package _2020_Java_A;
import java.util.*;
import java.text.*;
public class _07G回文日期 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.next();
Calendar c = Calendar.getInstance();
int year = Integer.parseInt(str.substring(0, 4));
int month = Integer.parseInt(str.substring(4, 6));
int date = Integer.parseInt(str.substring(6, 8));
c.set(year, month,date);
boolean flag = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
while(true) {
c.add(Calendar.DAY_OF_YEAR, 1);
String ans = sdf.format(c.getTime());
if(huiwen(ans)) {
if(!flag) {
System.out.println(ans);
flag = true;
}
if(AB(ans)) {
System.out.println(ans);
break;
}
}
}
sc.close();
}
private static boolean AB(String ans) {
// TODO Auto-generated method stub
return ans.charAt(0)==ans.charAt(2)&&ans.charAt(1)==ans.charAt(3);
}
private static boolean huiwen(String ans) {
// TODO Auto-generated method stub
StringBuffer res = new StringBuffer(ans);
return ans.equals(res.reverse().toString());
}
}
八、字串分值
package _2020_Java_A;
import java.util.*;
public class _08H字串分值 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String str = sc.next();
int ans = f(str);
System.out.print(ans);
sc.close();
}
private static int f(String str) {
// TODO Auto-generated method stub
char[] ch = str.toCharArray();
int n = ch.length;
int res = 0;
int i,j,k;
for (i = 0; i < n; i++) {
for (j = i - 1; j >= 0; j--) {
if (ch[j] == ch[i])
break;
}
for (k = i + 1; k < n; k++) {
if (ch[k] == ch[i])
break;
}
res += (k-i)*(i-j);
}
return res;
}
}
我是小菜鸟,我只会暴力
标签:javaA,String,int,蓝桥,2020,ans,static,TODO,public 来源: https://blog.csdn.net/weixin_48345739/article/details/115798761