The first question is called palindrome sequence if the inverse sequence of a number is the same as the original sequence. For example:
{1, 2, 1}, {15, 78, 78, 15}, {112} are palindrome sequences.
{1, 2, 2}, {15, 78, 87, 51}, {112, 2, 11} are not palindrome sequences.
Now we give a sequence of numbers that allow for a conversion operation:
Select any two adjacent numbers, then remove the two numbers from the sequence, and insert the sum of the two numbers into the position before the two numbers (insert only one sum).
Now, it is necessary to determine how many operations are needed to turn a given sequence into a palindrome sequence.
Input Description:
The input is two lines, and the length of the first sequence of actions is n (1 < n < 50)
The n integer items [i] (1 < iteam[i] < 1000) in the second behavior sequence are separated by spaces.
Output description:
Output a number to indicate the minimum number of conversion required
Example 1
input
4
1 1 1 3
output
2
public class HuiwenTest {
/**
* The idea is: for a sequence, first determine the size of the first and last elements
* When the first element > the last element, the first element adds to the second, adds back to the array, and deletes the second element.
* The first element < the last element, then the last element is added to the penultimate, added back to the array, and deleted the penultimate element.
* If the first element = the last element, skip the two elements and continue the comparison in the sequence.
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int[] item = new int[n];
for (int i = 0; i < n; i++) {
item[i] = sc.nextInt();
}
System.out.println(leastTimeToHuiwen(n,item));
}
}
public static int leastTimeToHuiwen(int n, int[] item) {
int leastTime = 0;//Define the minimum number of operations
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
list.add(item[i]);
}
while(list.size()>1){
if(list.get(0)<list.get(list.size()-1)){//If the first element > the last element, the first element adds to the second.
int a = list.get(0);
int b = list.get(1);
list.set(0, a+b);//Add back arrays
list.remove(1);//And delete the second element
leastTime++;
}else if(list.get(0)>list.get(list.size()-1)){//The first element < the last element
int a = list.get(list.size()-1);
int b = list.get(list.size()-2);
list.set(list.size()-2, a+b);//Then the last element is added to the penultimate and added back to the array
list.remove(list.size()-1);//And delete the penultimate element
leastTime++;
}
else{//If the first element = the last element, skip the two elements and continue the comparison in the sequence.
list.remove(0);
list.remove(list.size()-1);
}
}
return leastTime;
}
}
The second problem is that Xiaoyi has a circle whose center is at the origin of the coordinate. Xiaoyi knows the square of the radius of the circle. Xiao Yi thinks that the points on the circle and the points with integer horizontal and vertical coordinates are elegant. Now Xiao Yi wants to find an algorithm to calculate the number of elegant points. Please help him.
For example, if the radius square is 25
The elegant points are (+/-3, +/-4), (+/-4, +/-3), (0, +/-5) (+/-5, 0), a total of 12 points.
Input Description:
The input is an integer, that is, the square of the radius of the circle, which is in the range of 32 bit int.
Output description:
The output is an integer, that is, the number of elegant points.
Input Example 1:
25
Output Example 1:
12
The code is as follows
public class Point {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
if (n <= 0) {
System.out.println("Input integer n Non-conformity");
return;
}
System.out.println(count(n));
}
}
private static int count(int n) {
int count = 0;
for (int i = 0; i <= Math.sqrt(n); i++) {
double j = Math.sqrt(n - i * i);
if ((int) j == j) {
if (i == 0 || j == 0) {
count = count + 2;
} else {
count = count + 4;
}
}
}
return count;
}
}