蓝桥杯java第二题省赛
第十一届2020-2寻找2020(5分)
import java.util.Scanner;
public class b2020 {
public static long count;
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
// public class test {
// public static void main(String[] args) {
// Scanner read = new Scanner(System.in);
// String s = "";
// for(int i=1;;i++)
// {
// s = read.nextLine();
// if(s!=null)
// System.out.println(i);
// }}}//知道有300行
String[] str = new String[300];
for(int i=0;i<300;i++)
{
str[i] = read.nextLine();
}
f(str);
System.out.println(count)
}
public static void f(String[] str)
{
for(int i=0;i<str.length;i++)
{
for(int j=0;j<str[i].length();j++)
{
if(str[i].charAt(j)=='2')//开头一定是2//charAt()方法返回指定索引位置的char值
{
//横向
if(str[i].length()-j>=4 && str[i].charAt(j+1)=='0' && str[i].charAt(j+2)=='2' && str[i].charAt(j+3)=='0')
count++;
//竖向
if(str.length-i>=4 && str[i+1].charAt(j)=='0' && str[i+2].charAt(j)=='2' && str[i+3].charAt(j)=='0')
count++;
//斜向
if(str[i].length()-j>=4 && str.length-i>=4 && str[i+1].charAt(j+1)=='0' && str[i+2].charAt(j+2)=='2' && str[i+3].charAt(j+3)=='0')
count++;
}}}}}
第十届2019-2不同子串(5分)
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
String str = "0100110001010001";
Set<String> set = new HashSet<>();
for (int i = 1; i <= str.length(); i++) {
for (int j = 0; j < str.length(); j++) {
if (j + i <= str.length()) {
set.add(str.substring(j, j + i));
}
}
}
System.out.println(set.size());
}
}
//if (!set.contains(s.substring(j, j + i)))
//System.out.println(s.substring(j, j + i));
第九届2018-2方格计数(5分)
解题思路:将圆按四个象限分为四份,遍历第一象限中方格左上角
若方格的左上角在圆中,则整个方格都在圆中
根据勾股定理判断每个方格左上角顶点到原点的距离,
若此点到原点的距离小于半径,则数目加一。
最后数目乘以四得总数。
public class GridCount {
public static void main(String args[]) {
int radius = 1000, count = 0;
for (int i = 1; i < radius; i++)
for (int j = 1; j < radius; j++)
if (Math.pow(i, 2) + Math.pow(j, 2) <= Math.pow(radius, 2))
count++;
System.out.println(count * 4);
}
}
第八届2017-2纸牌三角形(5分)
public class b2017{//全排列算法
static int num=0;
public static void f1(int a[],int k){
int q,w,e;
if(k==9){
q=a[0]+a[1]+a[2]+a[3];
w=a[3]+a[4]+a[5]+a[6];
e=a[6]+a[7]+a[8]+a[0];
if(q==w&&w==e) num++;//三边相等符合要求
}
for(int i=k;i<a.length;i++){
{int temp=a[i];a[i]=a[k];a[k]=temp;}
f1(a,k+1);
{int temp=a[i];a[i]=a[k];a[k]=temp;}//还原数组
}
}
public static void main(String[] args) {
int a[]=new int[]{1,2,3,4,5,6,7,8,9};
f1(a,0);
System.out.println(num/6);//除以6种重复
}
}
第七届2016-2生日蜡烛(5分)
public class b2016 {
public static void main(String[] args) {
for(int i = 1;i <= 100;i++) {
int sum = i;
for(int j = i + 1;j <= 100;j++) {
sum += j;
if(sum == 236)
{ System.out.println(i);
break;}
}
}
}
}