回文日期
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
// static int end = 89991231; //这个只是输入的最大值,而不知输出结果的最大值,所以根本不需要加这个限制。
static int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static boolean check_valid(int date)
{
//20110101
int year = date /10000;
int month = date % 10000 / 100;
int day = date % 100;
if(month == 0 || month > 12) return false;
if(day == 0 || month != 2 && day > days[month]) return false;
if(month == 2)
{
int leap = 0;
if(year % 100 != 0 && year % 4 ==0 || year % 400 == 0) leap = 1;
if(day > 28 + leap) return false;
}
return true;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
int start = Integer.parseInt(input);
boolean flag1 = false;
for (int i = 1000; i < 10000; i++)
{
int date = i, x = i;
for (int j = 0; j < 4; j++)
{
date = date * 10 + x % 10;
x /= 10;
}
if(date >= start + 1 && check_valid(date)) //不能加 date <= end
{
if(!flag1)
{
System.out.println(date);
flag1 = true;
}
if((i / 100 == i % 100) && (i / 1000 != i % 10)) //蓝桥官网题干没说明白,应该加上 A != B
{
System.out.println(date);
break;
}
}
}
}
}
成绩分析
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static int n;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
n = Integer.parseInt(input[0]);
int max = -0x3f3f3f3f, min = 0x3f3f3f3f;
double sum = 0; //必须double,不能int
for (int i = 0; i < n; i++)
{
input = br.readLine().split(" ");
int cur = Integer.parseInt(input[0]);
max = Math.max(max, cur);
min = Math.min(min, cur);
sum += cur;
}
double avg = sum / n;
System.out.println(max);
System.out.println(min);
System.out.println(String.format("%.2f", avg)); //%.2f四舍五入了
}
}
平面切分
找规律但没找全 WA 骗了30分
原题链接
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.TreeSet;
public class Main {
static int N = 1010;
static int n;
static double[] a = new double[N], b = new double[N];
static Comparator<double[]> cmp = new Comparator<double[]>() {
@Override
public int compare(double[] o1, double[] o2) {
if(o1[0] != o2[0]) return (int)(o1[0] - o2[0]);
return (int)(o1[1] - o2[1]);
}
};
static TreeSet<double[]> set = new TreeSet<>(cmp);
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
n = Integer.parseInt(input[0]);
for (int i = 0; i < n; i++)
{
input = br.readLine().split(" ");
a[i] = Double.parseDouble(input[0]);
b[i] = Double.parseDouble(input[1]);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if(i != j)
{
if(a[i] != a[j])
{
double x = (b[j] - b[i]) / (a[i] - a[j]);
double y = a[i] * x + b[i];
double[] point = {x, y};
set.add(point);
}
}
}
int cnt = set.size();
if(cnt == 0) System.out.println(n + 1);
else if(cnt <= n - 1) System.out.println(n * 2);
else System.out.println(n + cnt + 1);
}
}
正确写法100分,参考了一篇写的很好的题解
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.TreeSet;
public class Main {
static int n, res = 1; //本来就有一部分
static double a, b;
static Comparator<double[]> cmp = new Comparator<double[]>() {
@Override
public int compare(double[] o1, double[] o2) {
if(o1[0] != o2[0])
{
if(o1[0] - o2[0] >0) return 1;
if(o1[0] - o2[0] <0) return -1;
}
else if(o1[1] != o2[1])
{
if(o1[1] - o2[1] >0) return 1;
if(o1[1] - o2[1] <0) return -1;
}
return 0;
// if(o1[0] != o2[0]) return (int)(o1[0] - o2[0]);
// return (int)(o1[1] - o2[1]);
}
};
static TreeSet<double[]> set1 = new TreeSet<>(cmp);
static TreeSet<double[]> set2 = new TreeSet<>(cmp);
static void compute(double a, double b)
{
set2.clear();
double x, y; //交点坐标
for(double[] p : set1) //枚举的是斜率、边距,而不是点!
{
if(a != p[0])
{
x = (p[1] - b) / (a - p[0]);
y = a * x + b;
set2.add(new double[]{x, y});
}
}
res += set2.size();
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
n = Integer.parseInt(input[0]);
while(n-- >0)
{
input = br.readLine().split(" ");
a = Double.parseDouble(input[0]);
b = Double.parseDouble(input[1]);
int size1 = set1.size();
set1.add(new double[]{a, b});
if(size1 < set1.size()) //不是重边,数目必+1
{
res++;
compute(a, b);
}
}
System.out.println(res);
}
}
子串分值和
暴力 TLE 60分
原题链接
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
public class Main {
static int N = 100010;
static int[] s = new int[N];
static long res;
static HashSet<Character> hs= new HashSet<>(); //char的封装类型
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] str = br.readLine().toCharArray();
int start = 0, len = str.length;
while(true)
{
for(int i = start;i < len; i++)
{
hs.add(str[i]);
res += hs.size();
}
start++;
hs.clear();
if(start >= len) break;
}
System.out.println(res);
}
}
正确写法100分,很难想。参考了一篇写的很好的题解
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
public class Main {
static int N = (int) (1e5+10);
static char[] s = new char[N];
static long f[] = new long[N]; //预处理会爆int
static int[] last = new int[30];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
s = ("0"+br.readLine()).toCharArray(); //把第一个字符占了
int n = s.length - 1;
long ans = 0;
//预处理没有任何重复的情况;
for(int i=1;i<=n-i+1;i++) f[i] = f[n-i+1] = (n-i+1)*(long)i; //注意乘的时候一定要转long
//统计总贡献,并去重
for(int i = 1;i<=n;i++)
{
int t = s[i]-'a';
if(last[t] == 0)
{ //判断有无重复部分
ans += f[i];
}
else
{
ans += f[i] - f[i] / i*last[t]; //减去重复部分;
}
last[t] = i; //记录当前字母最后出现位置
}
System.out.println(ans);
}
}
字串排序
不会做…
原题链接