蓝桥杯练习题1
【求和】
//暴力求解过程 测试案例只能通过50% 因为数据量过大 会超时
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] arr = new int[n];
for(int i= 0;i<n;i++){
arr[i] = scan.nextInt();
}
Long sum=0L;
for(int i=0;i<n-1;i++) {
for(int j=i+1;j<n;j++) {
sum+=arr[i]*arr[j];
}
}
System.out.println(sum);
scan.close();
}
}
//优解
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//定义一个数组
Long [] arr = new Long[n];
Long sum = 0l;
//对数组赋值
for( int i = 0 ; i < arr.length ; i++ ){
arr[i] = sc.nextLong();
sum += arr[i]; //对数组求和
}
//结果
Long res = 0l;
for( int i = 0 ; i < arr.length - 1 ; i++ ){
//依次减去当前索引所指的数
sum -= arr[i];
//累加求和
res += sum * arr[i];
}
System.out.println(res);
}
}
//考察了数学知识,重点是将数学语言转换成编程语言
//假如有5个数:a0,a1,a2,a3,a4
//求解:
// sum0 = a0 + a1 + a2 + a3 + a4
// 用乘法分配律进行分组求和
// sum1 = ( a1 + a2 + a3 + a4 ) * a0 = (sum0 - a0 ) * a0
// sum2 = ( a2 + a3 + a4 ) * a1 = (sum0 - a0 - a1 ) * a1
// sum3 = ( a3 + a4 ) * a2 = (sum0 - a0 - a1 - a2 ) * a2
// sum4 = ( a4 ) * a3 = (sum0 - a0 - a1 - a2 - a3 ) * a3
// res = sum1 + sum2 + sum3 + sum4
//可以看到非常的有规律
//我们求出数组的和(sum)后,在遍历求和时,依次减去当前索引所指的数,再与这个被减的数相乘
//用常规方法,双重for循环遍历数组,时间复杂度为 O(n2)。显然,只能拿30%的分
//如果运用些数学知识,只用一个for循环即可完成求解,时间复杂度为 O(n)
【寻找整数】
package test;
import java.math.BigInteger;
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class test2 {
//代表10的17次方1e17
static final long N = (long)1e17;
static int[] arr = new int[]{0,0,
1,2,1,4,5,4,1,2,9,0,5,10,
11,14,9,0,11,18,9,11,11,15,17,9,
23,20,25,16,29,27,25,11,17,4,29,22,
37,23,9,1,11,11,33,29,15,5,41,46};
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// 尽可能的寻找最大步伐 肯定是11和17的倍数,以11*17进行步长
// 寻找满足除43到49余数满足的两个数
// twoSovle - oneSovle 就是尽可能最大的步长
long oneSovle = 0, twoSovle = 0;
// for (long i = 11*17L; i <= 2e15; i+=187L) {
// if (i%49 == arr[49] && i%48 == arr[48] &&
// i%47 == arr[47] && i%46 == arr[46] &&
// i%45 ==arr[45] && i%44 == arr[44] &&
// i%43 == arr[43]) {
// if (oneSovle == 0) {
// oneSovle = i;
// }else {
// twoSovle = i;
// break;
// }
// }
// }
// 以下两个数是通过上方注释的代码计算出来的
oneSovle = 198015606569L;
twoSovle = 504680691449L;
// 计算步长,从oneSovle开始以step步长进行判断是否满足余数结果,满足则输出结果
long step = twoSovle - oneSovle;
for (long i = oneSovle; i <= N; i+=step) {
for (int j = 2; j <= 49; j++) {
if (i%j != arr[j]) {
break;
}
if (j == 49) {
System.out.print(i);
break;
}
}
}
scan.close();
}
}
//自己写的。。。。
// 从11 17 的最小公倍数里找
// 起点是11*17=187
// static boolean condition(Long t) {
// if (t % 2 != 1) {
// return false;
// }
// if (t % 3 != 2) {
// return false;
// }
// if (t % 4 != 1) {
// return false;
// }
// if (t % 5 != 4) {
// return false;
// }
// if (t % 6 != 5) {
// return false;
// }
// if (t % 7 != 4) {
// return false;
// }
// if (t % 8 != 1) {
// return false;
// }
// if (t % 9 != 2) {
// return false;
// }
// if (t % 10 != 9) {
// return false;
// }
// if (t % 12 != 5) {
// return false;
// }
// if (t % 13 != 10) {
// return false;
// }
// if (t % 14 != 11) {
// return false;
// }
// if (t % 15 != 14) {
// return false;
// }
// if (t % 16 != 9) {
// return false;
// }
// if (t % 18 != 11) {
// return false;
// }
// if (t % 19 != 18) {
// return false;
// }
// if (t % 20 != 9) {
// return false;
// }
// if (t % 21 != 11&&t % 22 != 11) {
// return false;
// }
// if (t % 23 != 15) {
// return false;
// }
// if (t % 24 != 17) {
// return false;
// }
// if (t % 25 != 9) {
// return false;
// }if (t % 26 != 23) {
// return false;
// }if (t % 27 != 20) {
// return false;
// }if (t % 28 != 25) {
// return false;
// }if (t % 29 != 16) {
// return false;
// }if (t % 30 != 29) {
// return false;
// }if (t % 31 != 27) {
// return false;
// }if (t % 32 != 25) {
// return false;
// }if (t % 33 != 11) {
// return false;
// }if (t % 34 != 17) {
// return false;
// }if (t % 35 != 4) {
// return false;
// }if (t % 36 != 29) {
// return false;
// }if (t % 37 != 22) {
// return false;
// }if (t % 38 != 37) {
// return false;
// }if (t % 39 != 23) {
// return false;
// }if (t % 40 != 9) {
// return false;
// }if (t % 41 != 1) {
// return false;
// }if (t % 42 != 11&&t % 43 != 11) {
// return false;
// }if (t % 44 != 33) {
// return false;
// }if (t % 45 != 29) {
// return false;
// }if (t % 46 != 15) {
// return false;
// }if (t % 47 != 5) {
// return false;
// }if (t % 48 != 41) {
// return false;
// }if (t % 49 != 46) {
// return false;
// }
// return true;
// }
//
// public static void main(String[] args) {
// Long i = 11L;
// boolean flag = true;
// while (flag) {
// if (condition(i)) {
// flag = false;
// }
// i += 187;
// }
// System.out.println(i);
// }
//}
【特殊日期】
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static int sum_shuwei(int a){
int sum=0;
while(a>0){
sum+=a%10;
a/=10;
}
return sum;
}
//闰年判断
static boolean leap_year(int y){
if(y%400==0||(y%4==0&&y%100!=0)){
return true;
//一年366天
}
return false;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//365天的 1 3 5 7 8 10 12是31天 4 6 9 11是30天 2是28天
int[] a = {0,31,28,31,30,31,30,31,31,30,31,30,31};
//366天
int[] b = {0,31,29,31,30,31,30,31,31,30,31,30,31};
int count = 0;
int sum_L=0;
//年份
for(int i=1900;i<10000;i++){
sum_L = sum_shuwei(i);
for(int j=1;j<13;j++) {
//"日"的各个数位和
int date = sum_L-sum_shuwei(j);
if(leap_year(i)) {
for(int k=1;k<=b[j];k++) {
if(date==sum_shuwei(k)) {
count++;
}
}
}else {
for(int k=1;k<=a[j];k++) {
if(date==sum_shuwei(k)) {
count++;
}
}
}
}
}
System.out.println(count);
scan.close();
}
}
【快排】
实现一个数组的快排;
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static void swap(int[] a,int i,int j){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
//“二分”的思想
static int partition(int[] a,int left,int right){
//left是数组左边的坐标
//right是数组右边的坐标
//要求left<right
if(left>right){
return -1;
}
int i,j,temp,t;
i=left;
j=right;
//基准元素
temp=a[left];
while(i<j){
//j左移的条件是 j所在位置比基准元素大/相等
//相等是个很重要的条件
while(temp<=a[j]&&i<j){
j--;
}
//i右移的条件是 i所在位置比基准元素小/相等
while(temp>=a[i]&&i<j){
i++;
}
//如果在i<j的条件下
//说明退出以上两个while循环是出现了 a[j]>=temp a[i]<=temp
//满足交换的条件
if(i<j){
swap(a,i,j);
}
//到这里已经完成一轮交换
}
//退出以上循环说明i=j
//交换基准元素和i
swap(a,left,i);
return i;
}
static void quicksort(int[] a,int l,int r){
if(l<r){
int i = partition(a,l,r);
//将基准元素和i/j相等位置的元素交换
//调用左边
quicksort(a,l,i-1);
//调用右边
quicksort(a,i+1,r);
}
}
//在一个函数内实现
// static void quickSort(int[] arr,int low,int high){
// int i,j,temp,t;
// if(low>high){
// return;
// }
// i=low;
// j=high;
// //temp就是基准位
// temp = arr[low];
// while (i<j) {
// //先看右边,依次往左递减
// while (temp<=arr[j]&&i<j) {
// j--;
// }
// //再看左边,依次往右递增
// while (temp>=arr[i]&&i<j) {
// i++;
// }
// //如果满足条件则交换
// if (i<j) {
// t = arr[j];
// arr[j] = arr[i];
// arr[i] = t;
// }
// }
// //最后将基准为与i和j相等位置的数字交换
// arr[low] = arr[i];
// arr[i] = temp;
// //递归调用左半数组
// quickSort(arr, low, j-1);
// //递归调用右半数组
// quickSort(arr, j+1, high);
// }
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] a = {50,49,48,48,47,46,1,3,5,2,4,6,30,30,30,30};
int N=16;
//数组长度是16,数组下标从0到15
quicksort(a,0,N-1);
for(int i=0;i<N;i++){
System.out.printf("%d.",a[i]);
}
scan.close();
}
}
【括号问题】
import java.util.*;
public class Main
{
public static boolean isGoodBracket(String s)
{
//栈结构
Stack<Character> a = new Stack<Character>();
for(int i=0; i<s.length(); i++)
{
char c = s.charAt(i);
if(c=='(') a.push(')');
if(c=='[') a.push(']');
if(c=='{') a.push('}');
if(c==')' || c==']' || c=='}')
{
//检测到右括号的时候 此时栈内大小是<=0的时候 返回false;
if(a.size()<=0) return false; // 填空
if(a.pop() != c) return false;
}
}
//检测完全部的括号后 此时栈内的大小还有容量 说明还有左括号没有匹配到
if(a.size()>0) return false; // 填空
return true;
}
public static void main(String[] args)
{
System.out.println( isGoodBracket("...(..[.)..].{.(..).}..."));
System.out.println( isGoodBracket("...(..[...].(.).){.(..).}..."));
System.out.println( isGoodBracket(".....[...].(.).){.(..).}..."));
System.out.println( isGoodBracket("...(..[...].(.).){.(..)...."));
}
}
【矩阵拆分】
连续的行(a) 连续的列(b)都未知
从矩阵的【首行地址,首列地址】开始遍历连续a行,b列的矩阵,求和;
//因为牵扯到好几个 for 循环,所以将求解矩阵内元素个数的解决方案放在函数中,这样求得元素个数之后就可以提早 return 退出这个函数,如果将该方法放在主函数中,不容易退出 for循环,需要完成全部遍历才退出,导致运行超时;
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static int find(int[][]num){
//寻找
for(int i=0;i<100;i++){
for(int j=0;j<100;j++){
int a = 0; //连续多少行
int b = 0; //连续多少列
for(a=0;a<100-i;a++){
for(b=0;b<100-j;b++){
//构造新的矩阵时sum归零
int sum=0;
//以下是矩阵内各个元素的和
//p代表矩阵的行起点 - a代表跨越的行距
//q代表矩阵的列起点 - b代表跨越的列距
for(int p=i;p<i+a;p++){
for(int q=j;q<j+b;q++){
sum+=num[p][q];
}
}
if(sum==2022){
// System.out.println(a*b);
return a*b;
// break;
}
}
}
}
}
return 0;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[][] num = new int[100][100];
//初始化num
for(int i=0;i<100;i++){
for(int j=0;j<100;j++){
if(i==0 && j==0){
num[i][j]=1;
}else {
//不是最左侧的列
if(j!=0){
num[i][j] = num[i][j-1]+2;
}else{
num[i][j] = num[i-1][j]+1;
}
}
}
}
int result = find(num);
System.out.println(result);
scan.close();
}
}
【序列个数】
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count=0;
for(int i=1;i<11;i++){
for(int j=i;j<11;j++){
for(int a=j;a<11;a++){
for(int b=a;b<11;b++){
for(int c=b;c<11;c++){
count++;
}
}
}
}
}
System.out.println(count);
scan.close();
}
}
【回文日期】
【串逐位和】
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static int f(char[] s, int begin, int end) {
int mid;
if (end - begin == 1)
return s[begin] - '0';
mid = (begin + end) / 2;
return f(s, begin, mid) + f(s, mid, end);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = "82791276526645328666454364634222489763382233";
char[] s = str.toCharArray();
System.out.printf("%d\n", f(s, 0, s.length));
scan.close();
}
}
【好数】
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static boolean haoshu(int m){
//求出数字的长度
// int len = String.valueOf(m).length();
// int count = 0;
int flag = 1;//奇数位置
//System.out.println("len:"+len);
while(m!=0){
int last = m%10;
if(flag==1&&last%2!=0){//奇数位置是奇数
flag = 0; //变成偶数
m/=10;
// count+=1;
}else if(flag==0&&last%2==0) {
flag = 1; //变成奇数
m/=10;
// count+=1;
}else{
return false;
}
}
// if(len==count) {
// return true;
// }
return true;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int num=0;
for(int i=1;i<N+1;i++){
if(haoshu(i)){
num++;
}
}
System.out.println(num);
scan.close();
}
}
【回文字符串】
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static boolean huiwen(String m){
int len = m.length();
for(int i=0;i<len/2;i++){
if(m.charAt(i)!=m.charAt(len-1-i)){
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String str[] = new String[n];
for(int i=0;i<n;i++){
str[i] = scan.next();
}
// ^[lqb]*$ 代表的是整个字符串
// [lqb]* 代表字符串的一部分
String regex = "[lqb]*";
for(int i=0;i<n;i++){
str[i] = str[i].replaceAll(regex,"");
//System.out.println("去掉正则表达式之后的数值:"+str[i]);
if(huiwen(str[i])){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
// for(int i=0;i<n;i++){
// System.out.println(str[i]);
// }
scan.close();
}
}
【最近距离】
import java.util.*;
class MyPoint
{
private double x; // 横坐标
private double y; // 纵坐标
public MyPoint(double x, double y)
{
this.x = x;
this.y = y;
}
public static double distance(MyPoint p1, MyPoint p2)
{
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return Math.sqrt(dx*dx + dy*dy);
}
/*
lst中含有若干个点的坐标
返回其中距离最小的点的距离
*/
public static double getMinDistance(List<MyPoint> lst)
{
if(lst==null || lst.size()<2) return Double.MAX_VALUE;
//记录最短距离-r
double r = Double.MAX_VALUE;
//从表中不断取出头元素进行比较
MyPoint p0 = lst.remove(0);
for(int i=0; i<lst.size(); i++)
{
MyPoint p = lst.get(i);
double d = MyPoint.distance(p0,p);
if(d<r) r=d; // 填空1
}
//递归调用
double d2 = getMinDistance(lst); // 填空2
return d2 < r ? d2 : r;
}
}
public class Maiin
{
public static void main(String[] args)
{
List<MyPoint> t = new Vector<MyPoint>();
t.add(new MyPoint(1,1));
t.add(new MyPoint(2,2));
t.add(new MyPoint(3,5.1));
t.add(new MyPoint(2.3,1.5));
System.out.println(MyPoint.getMinDistance(t));
}
}
【不同子串】
substring(int beginIndex,int endIndex)
此方法中的beginIndex表示截取的起始索引,截取的字符串中包括起始索引对应的字符;endIndex表示结束索引,截取的字符串中不包括结束索引对应的字符;
hashSet 结构:去重;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = "0100110001010001";
// String s = "0100";
//0 1 01 10 00 010 100 0100 共8个
//length -
s = s+"x";
//length 是 5
//index 从0-4
int count=0;
HashSet<String> hashSet = new HashSet<String>();
//i-每个字串的个数
//从1个到4个
for(int i=1;i<s.length();i++){
//i=2 j从0到2
for(int j=0;j<s.length()-i;j++) {
String str =s.substring(j,j+i);
hashSet.add(str);
}
}
count=hashSet.size();
System.out.println(count);
}
【单词分析】
int 数组求长度 数组名.length ;
String 数组求长度 数组名.length() ;
另外字符与数字做运算结果是数字,如果需要字符显示,需要强制类型转换(char);
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.next();
int[] count = new int[26];
for(int i=0;i<26;i++){
count[i]=0;
}
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
//count下标从1-25
count[c-'a']++;
}
//标记出现频率最高的字母出现的下标位置
int j=0;
int max=0;
for(int i=0;i<count.length;i++){
if(count[i]>max){
max=count[i];
j=i;
}
}
System.out.println((char)('a'+j));
System.out.println(max);
scan.close();
}
}
【次数差】
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.next();
int[] count = new int[26];
for(int i=0;i<26;i++){
count[i]=0;
}
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
//count下标从1-25
count[c-'a']++;
}
//标记出现频率最高的字母出现的下标位置
int j=0;
//标记出现频率最低的字母出现的下标位置
int k=0;
int max=0;
int min=s.length();
for(int i=0;i<count.length;i++){
if(count[i]>max){
max=count[i];
j=i;
}
if(count[i]!=0&&count[i]<min){
min=count[i];
k=i;
}
}
System.out.println(max-min);
scan.close();
}
}
【灌溉】
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static void irrigation(int[][] m, int row, int col, int k) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (m[i][j] == k) {
if ((i - 1) >= 0) {
m[i - 1][j] = k + 1;
}
if ((j - 1) >= 0) {
m[i][j - 1] = k + 1;
}
if ((j + 1) < col) {
m[i][j + 1] = k + 1;
}
if ((i + 1) < row) {
m[i + 1][j] = k + 1;
}
}
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
// 水管个数
int t = scan.nextInt();
// 标记水管位置
int[][] location = new int[n][m];
// 初始化灌溉情况
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
location[i][j] = 0;
}
}
for (int i = 0; i < t; i++) {
int p = scan.nextInt();
int q = scan.nextInt();
location[p - 1][q - 1] = 1;
}
// k分钟灌溉
int k = scan.nextInt();
for (int i = 1; i <= k; i++) {
irrigation(location, n, m, i);
}
// 统计结果
int num = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (location[i][j] != 0) {
num++;
}
}
}
System.out.println(num);
scan.close();
}
}
【长草】
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static void grassland(int[][] m, int row, int col, int k) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (m[i][j] == k) {
if ((i - 1) >= 0 && m[i - 1][j]==0) {
m[i - 1][j] = k + 1;
}
if ((j - 1) >= 0 && m[i][j-1]==0) {
m[i][j - 1] = k + 1;
}
if ((j + 1) < col && m[i][j+1]==0) {
m[i][j + 1] = k + 1;
}
if ((i + 1) < row && m[i+1][j]==0) {
m[i + 1][j] = k + 1;
}
}
}
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
// 标记草位置
char[][] location = new char[n][m];
int[][] copy = new int[n][m];
String[] str = new String[n];
char[] ch = new char[m];
// 初始化长草情况
for (int i = 0; i < n; i++) {
str[i] = scan.next();
ch = str[i].toCharArray();
for (int j = 0; j < m; j++) {
location[i][j] = ch[j];
if (location[i][j] == 'g') {
copy[i][j] = 1;
} else {
copy[i][j] = 0;
}
}
}
// k分钟
int k = scan.nextInt();
for (int i = 1; i <= k; i++) {
grassland(copy, n, m, i);
}
// 统计结果
int num = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (copy[i][j] == 0) {
location[i][j] = '.';
} else {
location[i][j] = 'g';
}
}
}
// 输出
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(location[i][j]);
}
System.out.println("\n");
}
scan.close();
}
}
【排列字母】
使用的是冒泡排序;
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.next();
char[] ch = s.toCharArray();
for(int i=0;i<ch.length-1;i++){
for(int j=i+1;j<ch.length;j++){
if(ch[j]<ch[i]){
char temp = ch[j];
ch[j] = ch[i];
ch[i] = temp;
}
}
}
for(int i=0;i<ch.length;i++){
System.out.print(ch[i]);
}
scan.close();
}
}
【纯质数】
关于质数的判断 n==1/n==2(以及在纯质数问题需要考虑n==0)
高效判断质数
在纯质数中考虑到时间成本,因此可以缩小范围,只考虑奇数部分;
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
static boolean prime(int m) {
//对于m==0也要判断
if (m == 1|| m==0) {
return false;
} else if (m == 2) {
return true;
}
// 对n>=3以上整数
//i < = Math.sqrt(m) 注意等于号
for (int i = 2; i <= Math.sqrt(m); i++) {
if (m % i == 0) {
return false;
}
}
return true;
}
static boolean pure_prime(int m) {
int n = 0;
while (m > 0) {
n = m % 10;
//在质数的判断上要加上n!=0
if (!prime(n)) {
return false;
}
//返回fakse 的时候 n=1/4/6/8/9/0
// if (n != 2 && n != 3 && n != 5 && n != 7) {
// return false;
// }
m /= 10;
}
return true;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num = 1; // 统计2是纯质数
//只对奇数进行纯质数判断
for (int i = 3; i < 20210606; i += 2) {
if (pure_prime(i) && prime(i)) {
// System.out.println(i);
num++;
}
}
System.out.println(num);
scan.close();
}
}
最大公约数
//辗转相除法--欧几里得算法
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
互质判断
public static boolean isCoprime(int a, int b) {
return gcd(a, b) == 1;
}
// 辗转相除法求GCD(推荐)
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
(布布:easy easy~版
【奇怪的数组】
public class Main {
static String jishu(String s){
int len = s.length();
//System.out.println("len:"+len);
//System.out.println("s:"+s);
String result = "";
//几个几 p个q
int p=0;
char q,r;
q = s.charAt(0);
if(len==1){
// result = 1+q+"";
result = String.valueOf(1)+q+"";
//System.out.println("len==1,result:"+result);
return result;
}
//x作为最后的标志
s = s+"x";
//System.out.println("s更新后:"+s);
for(int i=0;i<len+1;i++){
r = s.charAt(i);
if(r==q){
p++;
}else{
// result = p+q+"";
result = result.concat(String.valueOf(p)+String.valueOf(q)+"");
q = r;
p = 1;
}
}
//System.out.println("Result:"+result);
return result;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int m = scan.nextInt();
String str = ""+m;
//System.out.println("m:"+m);
//变换次数
int n = scan.nextInt();
for(int i=0;i<n;i++){
str = jishu(str);
}
System.out.println(str);
scan.close();
}
}
【饮料换购】
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//初始的饮料
int n = scan.nextInt();
int sum = n;
int add = 0;
while(n>=3){
add = n/3;
sum+=add;
n = add + n%3;
}
System.out.println(sum);
scan.close();
}
【数位递增的数】
static boolean dizeng(int m){
String str = m+"";
char[] num = str.toCharArray();
//递增
for(int i=0;i<num.length-1;i++) {
for(int j=i+1;j<num.length;j++) {
if(num[j]<num[i]) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int count = 0;
//检测从1-n
for(int i=1;i<n+1;i++){
if(dizeng(i)){
count++;
}
}
System.out.println(count);
scan.close();
}
【反倍数】
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
int count = 0;
for(int i = 1;i<n+1;i++){
if(i%a!=0&&i%b!=0&&i%c!=0){
count++;
}
}
System.out.println(count);
scan.close();
}
【洁净数】
static boolean is_not2(int m){
int n;
while(m>0){
n = m%10;
if(n==2){
return false;
}
m = m/10;
}
return true;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int count =0;
for(int i=1;i<n+1;i++){
if(is_not2(i)){
count++;
}
}
System.out.println(count);
scan.close();
}
【字符计数】
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
int yuan=0,fu=0;
for(int i=0;i<str.length();i++){
char ch =str.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'){
yuan++;
}else{
fu++;
}
}
System.out.println(yuan);
System.out.println(fu);
scan.close();
}
【2021公约数】
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count=0;//记录个数
for(int i = 2;i<2022;i++){
//j是公因数
for(int j =2;j<=i;j++)
{
if(i%j==0&&2021%j==0){
count++;
break;
}
}
}
System.out.println(count);
scan.close();
}
}
【项数】
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//为了保证准确度 要用double 而不是float
double sum = 1.0f;
int i=1;
while(sum < 12){
i++;
//除法的时候要用1.0做被除数
sum += 1.0/i;
}
System.out.println(i);
scan.close();
}
【轮换】
static void shift(char[] s, int n)
{
int len = s.length;
char[] temp = new char[len];
if(len==0) return;
if(n<=0 || n>=len) return;
//n-右移的位置
for(int i=0;i<len;i++) {
temp[i] = s[i];
}
for(int i=0;i<len;i++) {
if(i+n>=len) {
s[(i+n)%len] = temp[i];
}else {
s[i+n] = temp[i];
}
}
}
public static void main(String[] args) {
String s = "abcdefg";
//转换成字符数组
char x[] = s.toCharArray();
shift(x,2);
// 输出转换后的字符数组
for (char c:x) {
System.out.print(c);
}
}
【棋盘放麦子】
BigInteger数据对象类型
对象创建:valueOf(1) || new BigInteger("1")
运算:调用方法add() || subtract() || divide() || multiply() || mod()
import java.math.BigInteger;
public class Main {
public static void main(String args[]) {
BigInteger sum = BigInteger.valueOf(1);
BigInteger base = BigInteger.valueOf(2);
for(int i=2;i<=64;i++){
sum = sum.add(base.pow(i-1));
}
System.out.println(sum);
}
}
【成绩统计】
Math.round():接收一个
float
或double
类型的参数,用于对数字进行四舍五入,即返回一个离传入参数最近的整数(如果传入参数是float
返回int
类型结果,如果传入参数是double
返回long类型结果)Math.ceil():接收一个
double
类型的参数,向上取整;Math.floor():接收一个
double
类型的参数,向下取整;
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//考试人数
int n = scan.nextInt();
int[] score = new int[n];
for(int i=0;i<n;i++){
score[i] = scan.nextInt();
}
int num_A=0;
int num_B=0;
for(int i=0;i<n;i++){
if(score[i]>=60){
num_B++;
if(score[i]>=85){
num_A++;
}
}
}
System.out.println(Math.round(num_B/(double)n*100)+"%");
System.out.println(Math.round(num_A/(double)n*100)+"%");
scan.close();
}
}
【时间显示】
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//毫秒
long ms = scan.nextLong();
// long ms = scan.nextInt();
//秒
long s = ms/1000;
//分钟
long min = s/60;
s = s%60;
//时
long hour = min/60;
min = min%60;
hour = hour%24;
System.out.printf("%02d:%02d:%02d",hour,min,s);
scan.close();
}
}
输入:对于 long 数字类型,键盘输入的时候是 nextLong();
格式化输出:①%d ; ②%2d:2位数输出,不足位用空格补充;
③%02d:2位数输出,不足位用 ‘0‘ 补充;
【时长计算】
split(String regex) 是划分String字符串的,最后的结果返回 String 字符串数组;
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String begin = scan.next();
String end = scan.next();
String regex = ":";
String[] str1 = begin.split(regex);
String[] str2 = end.split(regex);
int a = Integer.valueOf(str1[0])*60*60+Integer.valueOf(str1[1])*60+Integer.valueOf(str1[2]);
int b = Integer.valueOf(str2[0])*60*60+Integer.valueOf(str2[1])*60+Integer.valueOf(str2[2]);
int diff = b-a;
int min =diff/60;
int s = diff%60;
int hour = min/60;
min = min%60;
System.out.printf("%02d:%02d:%02d",hour,min,s);
scan.close();
}
}
【编码】