第一题只有70%,是递归导致超时
import java.util.Scanner;
public class Main {
public static int ans = Integer.MIN_VALUE;
public static void func(int start, int end, int next, int count, int[][] list, int listflag)
{
for(int i=next;i<listflag;i++)
{
if(list[i][0] >= start)
{
count++;
func(list[i][1]+1, end, next+1, count, list, listflag);
count--;
}
}
if(count > ans)
{
ans = count;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] nums = new int[n];
for(int i=0;i<n;i++)
{
nums[i] = scan.nextInt();
}
int[][] list = new int[n][2];
int listflag = 0;
for(int i=0;i<n;i++)
{
int temp = nums[i];
if(temp == 0)
{
list[listflag][0] = i;
list[listflag][1] = i;
listflag++;
continue;
}
for(int j=i+1;j<n;j++)
{
temp ^= nums[j];
if(temp == 0)
{
list[listflag][0] = i;
list[listflag][1] = j;
listflag++;
break;
}
}
}
/********得到最大的区间数********/
func(0, n-1, 0, 0, list, listflag);
System.out.println(ans);
}
}
第二题丑数剑指offer原题 AC 100%
#include<stdio.h>
/*****丑数****/
/*
* 得到三个数中的最小值
*/
int getThreeMostMinFlag(int num1, int num2, int num3)
{
int min = num1 < num2 ? num1 : num2;
min = min < num3 ? min : num3;
return min;
}
/*
* 得到第n个丑数
*/
int getNthUglyNumber(int n)
{
int* uglyNums = new int[n];
uglyNums[0] = 1;
int nextUgly = 1;
int multiply2 = 0;//当前由乘2得出的最大丑数
int multiply3 = 0;//当前由乘3得出的最大丑数
int multiply5 = 0;//当前由乘5得出的最大丑数
while(nextUgly < n)
{
int min = getThreeMostMinFlag(uglyNums[multiply2]*2, uglyNums[multiply3]*3, uglyNums[multiply5]*5);
uglyNums[nextUgly] = min;
while(uglyNums[multiply2]*2 <= uglyNums[nextUgly])
{
multiply2++;
}
while(uglyNums[multiply3]*3 <= uglyNums[nextUgly])
{
multiply3++;
}
while(uglyNums[multiply5]*5 <= uglyNums[nextUgly])
{
multiply5++;
}
nextUgly++;
}
int ugly = uglyNums[nextUgly - 1];
delete [] uglyNums;
return ugly;
}
int main()
{
int n;
scanf("%d", &n);
int ugly = getNthUglyNumber(n);
printf("%d", ugly);
}