解题思路:分别列举分子分母,判断分子分母的余数是否为1;统计对应的个数
import java.util.*;
public class Main {
public static void main(String[] args) {
int num=0;
for (int i = 1; i <= 2020; i++) {
for (int j = 1; j <=2020; j++) {
if (dis(i,j)==1) {
num++;
}
}
}
System.out.println(num);
}
private static int dis(int a, int b) {
if (b==0) {
return a;
}else {
return dis(b, a%b);
}
}
}
测试 40分;内存超限
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int a[][] = new int[1001][1001] ;
int res=0;
for (int i = 1; i <= 1000; i++) {
if (res !=0) {
break;
}
for (int j = 1; j <=1000; j++) {
if (i==1 || j==1 || i==j) {
a[i][j] =1;
}else {
a[i][j] = a[i-1][j-1]+a[i-1][j];
}
if (a[i][j] == N) {
res= (i*(i-1))/2+j;
System.out.println(res);
break;
}
}
}
}
}
60分;时间超限
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); //序列长度
int m = sc.nextInt(); //操作次数,row
int arr[] = new int[n];
for (int k = 1; k<=n; k++) { //创建数组
arr[k-1] = k;
}
int a[] = new int[m];
int b[] = new int[m];
for (int i = 0; i < m; i++) { //存储每行输入的整数
a[i] = sc.nextInt();
b[i] = sc.nextInt();
}
for (int j = 0; j < m; j++) {
if (a[j] == 0) { //降序
desc(arr,b[j]);
}else { //升序
Arrays.sort(arr, b[j]-1, arr.length);
}
}
for (int e = 0; e < n; e++) {
System.out.print(arr[e]+" ");
}
}
private static void desc(int[] arr, int index) {
Arrays.sort(arr, 0, index);
int left = 0;
int right = index-1;
while (left<right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
long count =0 ;
for (int i = 2022; i <= 2022222022; i++) {
if (isMount(i)) {
count++;
}
}
System.out.println(count);
}
private static boolean isMount(int i) {
String str = i+"";
StringBuilder stringBuilder = new StringBuilder(str);
if (stringBuilder == stringBuilder.reverse()) {
for (int j = 0; j < str.length()/2; j++) {
int a = Integer.valueOf(str.charAt(j));
int b = Integer.valueOf(str.charAt(j+1));
if (a>b) {
return false;
}
}
return true;
}
return false;
}
}
解题思路:将字符添加到map中,统计出现的次数;再遍历一遍将出现次数最多的添加到数组中;最后数组记得重新排序,打印出来。
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.next();
int a[] = new int[input.length()];
for (int i = 0; i < input.length(); i++) {
char ch = input.charAt(i);
a[ch-'A']++;
}
int max= Integer.MIN_VALUE;
for (int i = 0; i < a.length; i++) {
if (max < a[i]) {
max = a[i];
}
}
for (int j = 0; j < a.length; j++) {
if (max == a[j]) {
char res = (char) (j+65);
System.out.print(res);
}
}
}
}
解题思路:将数组进行排序,找出中间数,比较各个数与中间数的大小,比中间数小的要多刷题。
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
int b[] = new int[n];
int index = n/2;
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
b[i] = a[i];
}
Arrays.sort(b);
int left = 0; //找出左右分别有多少数是和中间数一样的
int right = 0;
for (int j = 0; j < b.length; j++) {
if (j<index && b[j]==b[index]) {
left++;
}
if (j>index && b[j]==b[index]) {
right++;
}
}
int same = left+right+1; //总的相同中间值的数
int rightplus = a.length-1-index-right; //右边不同中间值的数
int leftplus = index-left;
for (int k = 0; k < a.length; k++) {
if (same >1 && rightplus>=leftplus) { //右边数比左边多时
if (a[k] <= b[index]) { //刷题数比中间值少时
a[k] = b[index]-a[k] +1;
}else { //刷题数比中间值多时,不用刷题
a[k] = 0;
}
}else if (same >1 && leftplus > rightplus) { //左边数比右边数多
if (a[k] < b[index]) {
a[k] = b[index]-a[k]+1;
}else{
a[k] = 0;
}
}else if (same ==1 && rightplus >= leftplus) {
if (a[k] < b[index]) {
a[k] = b[index]-a[k]+1;
}else {
a[k] =0;
}
}else if(same ==1 && rightplus < leftplus){
if (a[k] < b[index]) {
a[k] = b[index] - a[k]+1;
}else {
a[k] =0;
}
}
if (k==b.length-1) {
System.out.print(a[k]);
}else {
System.out.print(a[k]+" ");
}
}
}
}
先写出求阶乘的函数,再以此判断是否符合条件
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
long left=1,right = Long.MAX_VALUE-5;
while (left < right) {
long mid = (left+right)/2;
if (n <= cacl(mid)) {
right= mid;
}else {
left = mid+1;
}
}
if (cacl(right) != n) {
System.out.print(-1);
}else {
System.out.print(right);
}
}
private static long cacl(long x) {
long res=0;
while (x!= 0) {
res = res+ x/5;
x/=5;
}
return res;
}
}
import java.util.*;
public class Main{
public static void main(String[] args) {
int MOD = 1000000007;
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] num = new int[n + 1];
for (int i = 1; i <= n; i++) {
num[i] = scanner.nextInt();
}
int[] f = new int[n + 1];
f[0] = 1; // 初始化
for (int i = 1; i <= n ;i++) {
int max = num[i];
int min = num[i];
for (int j = i; j <= n; j++) {
max = Math.max(num[j], max);
min = Math.min(min, num[j]);
if (j - i == max - min ) { // 如果当前的最大值和最小值之差等于当前的数字个数,则说明当前的数字可以切分
f[j] = (f[j] + f[i - 1]) % MOD; // 切分的方法数量为当前的数字个数加上之前的方法数量
}
}
}
System.out.println(f[n]);
}
}