回文序列
来源:互联网 发布:coc女王升级数据2017 编辑:程序博客网 时间:2024/06/10 04:35
如果一个数字序列逆置之后跟原序列是一样的就称这样的数字序列为回文序列。例如:
{1, 2, 1}, {15, 78, 78, 15} , {112} 是回文序列,
{1, 2, 2}, {15, 78, 87, 51} ,{112, 2, 11} 不是回文序列。
现在给出一个数字序列,允许使用一种转换操作:
选择任意两个相邻的数,然后从序列移除这两个数,并用这两个数字的和插入到这两个数之前的位置(只插入一个和)。
{1, 2, 1}, {15, 78, 78, 15} , {112} 是回文序列,
{1, 2, 2}, {15, 78, 87, 51} ,{112, 2, 11} 不是回文序列。
现在给出一个数字序列,允许使用一种转换操作:
选择任意两个相邻的数,然后从序列移除这两个数,并用这两个数字的和插入到这两个数之前的位置(只插入一个和)。
现在对于所给序列要求出最少需要多少次操作可以将其变成回文序列。
import java.util.Scanner;public class Main { public static int count; public static int solve(int[] arr, int n) { return robot(arr, 0, n - 1); } public static int robot(int[] arr, int l, int r) { if (l > r) return count; if (arr[l] < arr[r]) { arr[l + 1] = arr[l + 1] + arr[l]; l++; count++; } else if (arr[l] > arr[r]) { arr[r - 1] = arr[r - 1] + arr[r]; r--; count++; } else { l++; r--; } return robot(arr, l, r); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } System.out.println(solve(arr, n)); }}
阅读全文
0 0
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列
- 回文序列(P1221)
- 最长回文子序列
- 最长回文子序列
- 【hihoCoder】回文字符序列
- 回文字符序列
- 回文字符序列
- 回文字符序列
- 思维导图之探寻墨菲定律
- nrf51822蓝牙芯片ble_app_proximity程序总结
- 编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
- linux下设置mysql数据库字符集utf8
- http4client rest buik elasticsearch
- 回文序列
- 【LeetCode】535. Encode and Decode TinyURL
- android实现断点续传
- summernote编辑器插件使用
- 02_选择排序
- Linux安装Mysql
- av_read_frame的过程
- 《人人都是产品经理》——第三章笔记(上)
- Python起步之读写docx文件