今天的太难了,直接开摆
山(字符串反转比较,拆分判断是否递增)
package sel4fil3;
/*
* 求区间内的回文数字
* String.compareTo(StringBuider.reverse().toSting()) == 0
* 字符串反转是否与之前相等,且前2/n为递增
*
*/
public class shan {
public static void main(String[] args) {
// TODO Auto-generated method stub
long ans=0;
for(long i=2022;i<=2022222022;i++) {
if(check(i)) {
ans++;
}
}
System.out.println(ans);
}
private static boolean check(long i) {
//判断是否回文
String string = String.valueOf(i);
StringBuilder sBuilder = new StringBuilder(string);
if(string.compareTo(sBuilder.reverse().toString())==0) { //是回文数
for(int j=0;j<string.length()/2;j++) {
int pre = Integer.valueOf(string.charAt(j));
int aft = Integer.valueOf(string.charAt(j+1));
if(aft<pre)
return false;
}
return true;
}
return false;
}
}
2。最少刷题数
太变态了
package sel4fil3;
/*请你计算他至少还要再刷多少道题,
* 才能使得全班刷题比他多的学生数不超过刷题比他少的学生数。
*
*/
import java.util.*;
public class shuati {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n+1];
int[] b = new int[n+1];
for (int i = 1; i <= n; i++) {
a[i] = sc.nextInt();
b[i] = a[i];
}
Arrays.sort(a);
int x = a[n/2+1];
int min=0;
int max=0;
for(int i=1;i<=n;i++) {
if(a[i] < x) {
min++;
}else if(a[i]>x) {
max++;
}
}
int s =0;
if(min < max ) {
x = x+1;
}else if(min == max) {
s=1;
}
for(int i=1;i<=n;i++) {
if(s==1 && b[i] !=x) {
System.out.print(Math.max(0, x+1-b[i])+ " ");
}else {
System.out.print(Math.max(0, x-b[i])+" ");
}
}
}
}
3.9算数
package sel4fil3;
/*全排列:
*长度为n,求所有长度为n的排列
*
*
*/
public class suanshu {
static boolean[] used = new boolean[10];
static int[] nums = new int[10];
static int count = 0;
public static void main(String[] args) {
dfs(0);
System.out.println(count);
}
static void dfs(int pos) {
if (pos == 10) {
int num1 = nums[0] * 1000 + nums[1] * 100 + nums[2] * 10 + nums[3];
int num2 = nums[4] * 10000 + nums[5] * 1000 + nums[6] * 100 + nums[7] * 10 + nums[8];
int product = num1 * num2;
if (product >= 100000000 && product < 1000000000) {
boolean[] usedInProduct = new boolean[10];
int digit = product % 10;
usedInProduct[digit] = true;
for (int i = 1; i < 9; i++) {
product /= 10;
digit = product % 10;
if (usedInProduct[digit]) {
return;
} else {
usedInProduct[digit] = true;
}
}
count++;
}
return;
}
for (int i = 1; i <= 9; i++) {
if (!used[i]) {
used[i] = true;
nums[pos] = i;
dfs(pos + 1);
used[i] = false;
}
}
}
}
这篇文章包含三个编程问题:一是寻找2022至2022222022之间的回文数字并检查其前半部分是否递增;二是计算学生刷题策略,确保刷题数量排名居中的学生前后相差不大;三是生成并检查特定条件的全排列数字对的算术产品。
2081

被折叠的 条评论
为什么被折叠?



