编程题
来源:互联网 发布:五笔for mac 编辑:程序博客网 时间:2024/06/09 19:26
如果一个数字序列逆置之后跟原序列是一样的就称这样的数字序列为回文序列。例如:
{1, 2, 1}, {15, 78, 78, 15} , {112} 是回文序列,
{1, 2, 2}, {15, 78, 87, 51} ,{112, 2, 11} 不是回文序列。
现在给出一个数字序列,允许使用一种转换操作:
选择任意两个相邻的数,然后从序列移除这两个数,并用这两个数字的和插入到这两个数之前的位置(只插入一个和)。
现在对于所给序列要求出最少需要多少次操作可以将其变成回文序列。
输入描述:输入为两行,第一行为序列长度n ( 1 ≤ n ≤ 50)第二行为序列中的n个整数item[i] (1 ≤ iteam[i] ≤ 1000),以空格分隔。输出描述:输出一个数,表示最少需要的转换次数输入例子:41 1 1 3输出例子:2import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class Main6 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int[] item = new int[num]; for(int i=0;i<num;i++){ item[i]=sc.nextInt(); } System.out.println(countTimes(item)); } public static int countTimes(int[] item){ int count =0; List<Integer> list = new ArrayList<Integer>(); for(int i=0;i<item.length;i++){ list.add(item[i]); } while (list.size()>0){ if(list.get(0)<list.get(list.size()-1)){ int a = list.get(0); int b= list.get(1); list.set(0, a + b); list.remove(1); ++count; } else if(list.get(0)>list.get(list.size()-1)){ int a = list.get(list.size()-1); int b = list.get(list.size()-2); list.set(list.size()-2,a+b); list.remove(list.size()-1); ++count; } else { list.remove(0); list.remove(list.size()-1); } } return count; }}
0 0
- 编程题
- 编程题
- 编程题?
- 编程题
- 编程题
- 编程题
- 编程题
- 编程题
- 编程题
- 编程题
- 编程题
- 编程题
- 编程题
- 编程题
- 编程题
- 编程题
- 编程题
- 编程题
- 通用Adapter的实现套路(ListView篇)
- 【JSOI2007】动态最值 Splay
- poj3067 Japan 树状数组 逆序数
- 浅谈css中浮动和清除浮动带来的影响
- JVM入门
- 编程题
- 构建二叉树的两种方式(前+中) (中+后),递归
- CoordinatorLayout的使用
- 剑指Offer4:替换空格
- 如何在腾讯云上开发一款O2O书签?
- 数据结构--基本数据结构
- 第七届蓝桥杯b组第四题-快速排序
- jaxb 序列化与反序列化是遇到的问题
- oracle创建用户