免责声明:这个问题更具挑战性,并且帖子也很长。仅在空闲时间阅读!
基本上,假设只有一行整数输入:
32352\n // assuming single digits only and no spaces for simplification
我们必须从输入中删除重复项,然后显示它们。因此,输出应为:
After removing duplicates: 2, 3, 5 // display them in any order
但是,有一个陷阱:
编辑:我相信容器就是我的意思(谢谢弗拉德!)。
所以,我的问题是:我的实现中有什么错误,还有另一种(更好的)实现方法吗?
由于不允许使用任何数据结构,因此无法存储输入(我认为吗?)。
但是,由于它已经在输入时存储在内存中,所以这不是问题。
更多的问题是删除重复项。我们将不得不操纵输入流。
让我震惊的第一件事是我们可以对输入进行排序。那是,
32352
变成:
22335
现在,只需打印每个范围的第一个元素。
为此,我遇到了std::cin.get()
和std::cin.putback()
方法,都接受char
。
我也意识到我将不得不使用递归。
因此,代码变为(我使用了插入排序):
该sort()
函数是错误所在。它使用运行中的index
ala数组,用于唯一标识每个元素。
在每次迭代中,index_of_element
都会找到并选择该元素,然后确定需要在其余(虚拟)数组中的何处放置元素。例如,如果在我们的原始输入中:
32352 // S = sorted subarray
SU--U // U = unsorted subarray
,第一个2
被选中,我们“移位” 3
(如3 < 2
)。
现在,没有其他元素需要转移,我们“放置” 2
。
结果应为:
23352
SSU-U
(越野车)实现:
bool sort(int index_of_element, int index = 0, char prev_element = 0)
{
static char element;
char digit;
// retrieve an element from memory
std::cin.get(digit);
// If not end of input
if(digit != '\n')
{
// store the element for comparision
if(index == index_of_element)
{
element = digit;
}
// continue forward until '\n'
bool result = sort(index_of_element, index + 1, digit);
// if we are in sorted subarray
if(index <= index_of_element)
{
// If element belongs here(also if this is first element(prev_element is 0)), place it
if(element > prev_element)
{
digit = element;
// Signal that element has been placed
element = 0;
}
// Else, if element not already placed, we need to shift elements
else if(element != 0)
{
// Place the previous element here
digit = prev_element;
}
}
// Put it back in memory
std::cin.putback(digit);
// And return the result
return result;
}
// Which is generated here when end of input is reached
else
{
// If sorted all elements, break loop
if(index_of_element == index)
{
return false;
}
// Else, continue sorting
else
{
return true;
}
}
}
(有一堵代码墙,但是我不想跳过任何相关内容),它应该用作:
...
int index_of_element = 0;
while(sort(index_of_element++));
...
显示功能已准备就绪,并且可以正常工作。
我所知道的是,它陷入了无限循环,并且值丢失了。
怎么了?
我应该添加输出(帖子已经很长了)吗?
一次只可以使用功能对象来完成此操作。
#include <iostream>
#include <sstream>
#include <functional>
void print_unique_ints(std::istream & in, std::ostream & out, std::function<bool(int)> unseen) {
for (int i; in >> i;) {
if (unseen(i)) {
out << i << ' ';
print_unique_ints(in, out, [&](int j){ return (i != j) && unseen(j); });
return; // not actually needed, previous call only ends when input is exhausted
}
}
}
int main() {
print_unique_ints(std::cin, std::cout, [](int){ return true; });
}
每次调用都会print_unique_ints
跳过以前看到的int,打印看不见的int,并将其添加到过滤器中
用值代替变量;和函数调用表达式;在第一个电话
for (int i; in >> i;) { // i = 3
if (true) {
out << 3 << ' ';
print_unique_ints(...) // see below
}
}
第二
for (int i; in >> i;) { // i = 2
if ((3 != i) && true) {
out << 2 << ' ';
print_unique_ints(...) // see below
}
}
第三
for (int i; in >> i;) { // i = 3, 5
if ((2 != i) && (3 != i) && true) { // skips over the 3
out << 5 << ' ';
print_unique_ints(...) // see below
}
}
第四
for (int i; in >> i;) { // i = 2
if ((5 != i) && (2 != i) && (3 != i) && true) { // skips the 2 and finds the end of input
}
}
请注意,&& true
永远不要更改结果if
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句