- 门牌制作
public class Main {
static int n = 0;
public static void main(String[] args) {
for (int i = 1; i <= 2020; i++) {
f(i);
}
System.out.println(n);
}
private static void f(int x) {
int m;
while (x != 0) {
if (x % 10 == 2) {
n++;
}
x = x / 10;
}
}
}
- 既约分数
public class Main {
public static int gcd(int a, int b) {;
if (b == 0)
return a;
return gcd(b, a % b);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int count = 0;
for(int i = 1; i <= 2020; i++) {
for(int j = 1; j <= 2020; j++) {
if(gcd(i, j) == 1) {
count++;
}
}
}
System.out.println(count);
}
}
- 蛇形填数
public class Main {
public static void main(String[] args) {
int res = 1, t = 4;
for(int i=2; i<=20; ++i) {
res += t;
t += 4;//1+4+8+12+16
}
System.out.println(res);
}
}
- 跑步锻炼
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
int sum=2;
Calendar c1=Calendar.getInstance();
SimpleDateFormat a=new SimpleDateFormat("yyyyMMdd");
c1.set(Calendar.YEAR, 2000);
c1.set(Calendar.MONTH, 0);
c1.set(Calendar.DAY_OF_MONTH, 1);
for (int i = 1; i <=10000; i++) {
c1.add(Calendar.DAY_OF_YEAR,1);
sum++;
String date=a.format(c1.getTime());
char b[]=date.toCharArray();
if (c1.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY || (b[6]=='0' && b[7]=='1'))
{
sum=sum+1;
}
if (date.equals("20201001")) {
break;
}
}
System.out.println(sum);//sum=8879
}
}
- 七段码
import java.util.*;
public class Main{
int x,y;
public e(int x,int y) {
this.x=x;
this.y=y;
}
}
public static void main(String[] args) {
e ed[]=new e[10];
ed[0]=new e(1,2);
ed[1]=new e(2,3);
ed[2]=new e(3,4);
ed[3]=new e(4,5);
ed[4]=new e(5,6);
ed[5]=new e(6,3);
ed[6]=new e(6,1);
int total=0;
int num=0;
for (int k = 0;; k++) {
dfs(0);
if(flag==1) break;
int v[]=new int[7];
for (int i = 1; i <=6; i++) {
v[i]=i;
}
int s=0,t5=0;
int a[]=new int[7];
for (int i = 0; i < 7; i++) {
if(vis[i]==1) {
t5=ed[i].x;
if(a[t5]==0) s++;
a[t5]=1;
t5=ed[i].y;
if(a[t5]==0) s++;
a[t5]=1;
}
}
for (int i = 0; i < a.length; i++) {
a[i]=0;
}
s-=1;
for (int i = 0; i < 7; i++) {
if(vis[i]==1) {
int t1=find(ed[i].x,v);
int t2=find(ed[i].y,v);
if(t1!=t2) {
join(t1,t2,v);
s--;
if(s==0) break;
}
}
}
if(s==0) total++;
}
System.out.println(total);
}
private static void join(int t1, int t2,int[] v) {
if(t1>t2) v[t1]=t2;
else v[t2]=t1;
}
private static int find(int x,int[] v) {
int r=x;
while(v[r]!=r) {
r=v[r];
}
int p=x,q=0;
while(v[p]!=p) {
q=v[p];
v[p]=r;
p=q;
}
return r;
}
private static void dfs(int k) {
if(t==0) {flag=1;return;}
if(vis[k]==1) {
vis[k]=0;
t++;
dfs(k+1);
}else {
vis[k]=1;
t--;
}
}
}
- 成绩统计
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
double sum = 0;
for(int i=0; i<n; ++i) {
int t = sc.nextInt();
sum += t;
max = t>max?t:max;
min = t<min?t:min;
}
System.out.println(max);
System.out.println(min);
System.out.printf("%.2f",sum/n);
}
}
- 回文日期
import java.util.Scanner;
public class Main {
public static String str="";
public static boolean isLeap(int year){
return (year%4==0&&year%100!=0)||year%400==0;
}
public static boolean check(int a,int b,int c){
//月份效验
if(b<1||b>12) return false;
if(c<1||c>31) return false;
//日效验
switch (b){//日期校验
case 2:if(isLeap(a)&&c>29) return false;
if(!isLeap(a)&&c>28) return false;
break;
case 4:
case 6:
case 9:
case 11:
if(c>30) return false;
break;
default:
break;
}
if(str.charAt(0)==str.charAt(2)
&&str.charAt(2)==str.charAt(5)
&&str.charAt(5)==str.charAt(7)&&
str.charAt(1)==str.charAt(3)
&&str.charAt(3)==str.charAt(4)&&
str.charAt(4)==str.charAt(6))
{
return true;
}
return false;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int cur = scanner.nextInt();
int sum=0;
for (int i = cur; i <= 89991231; i++) {
str="";
str+=i;
if(check(Integer.parseInt(str.substring(0,4)),
Integer.parseInt(str.substring(4,6)),
Integer.parseInt(str.substring(6,8)))){
sum++;
System.out.println(i);
if(sum==2){
break;
}
}
}
}
}
- 子串分值和
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
Set<Character> set = new HashSet<Character>();
int sum=0;
for (int i = 0; i < str.length(); i++) {
for (int j = 0; j < str.length() - i; j++) {
set.clear();
for (int k = i; k <= i + j; k++) {
set.add(str.charAt(k));
}
sum+=set.size();
}
}
System.out.println(sum);
}
}
- 平面切分
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
static int ans=0; //平面的个数
static boolean[] cb=new boolean[1001];
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[][] line=new int[n][2];
for(int i=0;i<line.length;i++) {
for(int j=0;j<2;j++) {
line[i][j]=sc.nextInt();
}
}
for(int i=0;i<n;i++) {
Set<String> points=new HashSet<>();
for(int j=0;j<i;j++) {
if(cb[j]) continue;
if(line[i][0]==line[j][0]) {
if(line[i][1]==line[j][1]) {
cb[i]=true;
break;
}else {
continue;
}
}
double x=(line[j][1]-line[i][1])/(line[i][0]-line[j][0]);
double y=line[i][0]*x+line[i][1];
String str=x+""+y;
points.add(str);
}
if(!cb[i]) ans += points.size()+1;
}
System.out.println(ans + 1);
}
}
- 字串排序
import java.util.*;
public class Main {
public static Set<String> set=new TreeSet<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()){
set.clear();
String s = scanner.nextLine();
for (int i = 0; i < s.length(); i++)
set.add(s.substring(i));
for(String set1:set){
System.out.println(set1);
}
}
}
}