任何人都可以帮我这个C ++程序吗? [英] Can anyone help me with this C++ program?

查看:102
本文介绍了任何人都可以帮我这个C ++程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在两个方向上读取相同的正整数序列,则它是回文结构。序列

23,45,23和23,45,56,23,23,56,45,23是回文的例子。序列23,45,56不是一个回文。序列23,32也不是回文序列。长度为1的序列总是一个

回文。

给定的整数序列可以分解成各个部分,使得它们都是回文。

考虑序列34,45,34,56,34。这可以分为3个回文序列,其中

34,45,34构成第一个,56个构成第二个和34个构成第三个。它还可以在每个包含一个数字的5个回文序列中打破
。因此,可能有许多不同的方法将给定序列分解为回文序列。我们想确定

最小数C,以便给定序列可以分解为C回文序列。

观察任何回文序列,C的值为1对于序列34,45,34,56,34

答案是3.你的目标是编写一个程序来计算任何给定序列的这个数字。



输入格式

•第一行包含N序列中的值数。

•后面是包含N正数的行由空格分隔的整数给出序列的值





输出格式

输出一个整数给出最小的数字C,以便给定的序列可以分解为
到C回文序列。



数据

您可以假设输入中的所有整数都在1到10 ^ 8

范围内。

子任务1(100标记)1≤N≤300。



样本inp ut

5

34 45 34 56 34



样品输出

3

A sequence of positive integers is a palindrome if it reads the same in both directions. The sequences
23, 45, 23 and 23, 45, 56, 23, 23, 56, 45, 23 are examples of palindromes. The sequence 23, 45, 56 is not
a palindrome. The sequence 23, 32 is not a palindrome either. A sequence of length 1 is always a
palindrome.
A given sequence of integers can be broken up into parts such that each of them is a palindrome.
Consider the sequence 34, 45, 34, 56, 34. This can be broken up into 3 palindrome sequences with
34, 45, 34 constituting the first, 56 constituting the second and 34 constituting the third. It can also
be broken in 5 palindrome sequences each containing a single number. Thus, there may be many
different ways to break up a given sequence into palindrome sequences. We want to determine the
smallest number C such that the given sequence can be broken up into C palindrome sequences.
Observe that for any palindrome sequence the value of C is 1. For the sequence 34, 45, 34, 56, 34
the answer is 3. Your aim is to write a program that computes this number for any given sequence.

Input format
• The first line contains N the number of values in the sequence.
• This is followed by a line containing N positive integers separated by space giving the values
of the sequence.

Output format
Output a single integer giving the smalllest number C so that the given sequence can broken up
into C palindrome sequences.

Data
You may assume that all integers in the input are in the range 1 to 10^8
inclusive.
Subtask 1 (100 Marks) 1 ≤ N ≤ 300.

Sample input
5
34 45 34 56 34

Sample output
3

推荐答案

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



并阅读规范,弄清楚它涉及的内容并且设计解决方案是您开始编码之前采取的重要步骤。



亲自尝试,您可能会发现它并不像您想象的那么难!



如果您遇到特定问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

And reading the specification, working out what it involves and designing a solution are important steps you take before you start coding.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


这个问题的关键在于挑战你的分析技能。

所以:

The whole point of such a problem is to challenge your analyse skills.
So:
引用:

我只是想让某人解释这个程序的基本逻辑

I just want someone to explain the basic logic for this program

就像给你一个答案因为这是问题的难点部分。



当你解决问题时,总会有两个步骤:

- 第1步是发现问题逻辑,这是困难的部分。

- 第2步是将逻辑转换为所选择的语言,几乎是机械的。



我建议你提高你的分析技能:

- 学习一种或多种分析方法,我建议 EW Djikstra自上而下的分析方法,这是一个良好的开端。

https ://en.wikipedia.org/wiki/Top-down_and_bottom-up_design [ ^ ]

https://en.wikipedia.org/ wiki / Structured_programming [ ^ ]

https://en.wikipedia.org/wiki/Edsger_W._Dijkstra [https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF [ ^ ]

is like giving you the answer since it is the difficult part of the problem.

When you solve a problem, there is always 2 steps:
- step 1 is discovering the problem logic, it is the difficult part.
- step 2 is translating the logic into the chosen language, almost mechanical.

I recommend you to sharpen your analyse skills:
- Learn one or more analyse methods, I recommend E.W. Djikstra top-Down analyse method, it is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]


这篇关于任何人都可以帮我这个C ++程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆