小编Vai*_*los的帖子

"寻找后续元素最大和"算法的分析

如果可能的话,我希望有人对算法进行分析性解释.

例如,给定序列

-2, 4, -1, 3, 5, -6, 1, 2 
Run Code Online (Sandbox Code Playgroud)

最大子序列总和

4 + -1 + 3 + 5 = 11
Run Code Online (Sandbox Code Playgroud)

我正在考虑的这个算法是一种分而治之的算法.

该算法是O(nlogn)复杂度.

实际上,我试图看到该算法产生的所有步骤的示例.上述序列可用于该示例.

arrays algorithm sequences

6
推荐指数
1
解决办法
3609
查看次数

在字符串对象中搜索换行符'\n'

我有一般形式的字符串对象string line = "yadayada\nyadaya".我循环遍历字符串,试图"捕获"换行符.

        for (int i = 1; i < line.length(); i++)
        {
             if ( ( line[i]== ' \ ') && ( line[i+1] == 'n' ) ) 
             {
                  buffer.insertChar('\n');
                  i = i+2;
             }
             else
             {

                  buffer.insertChar(line[i]);
             }

        }
Run Code Online (Sandbox Code Playgroud)

正如你所看到我循环遍历字符串字符,我将字符逐个插入另一个称为缓冲区的对象(与问题无关).

在第一个,如果if ( ( line[i]== ' \ ') && ( line[i+1] == 'n' ) )我试图"捕获"换行符,并在内部,如果正在我将索引i递增2,以便它将在下一个循环中跳过字符'\'和'n'.问题是这个循环从不捕获换行符但总是在缓冲区中插入两个单独的字符'\'和'n'.

重要说明:我使用索引i = 1启动循环,因为第一个字符就像一个命令,正在被特殊处理.

更新:我修改了上面的代码,但仍然没有运气我想要完成的

                for (int i = 1; i < line.length(); i++)
                {
                    if ( (line[i]== '\n') )   
                    {

                        buffer.insertChar('\n');
                        i …
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
2
解决办法
3万
查看次数

我在以下函数中得到错误与operator >>不匹配.怎么了?

void GetarrayElements(int a[]){
  int k=0;

  while (true){
    cout <<"to exit just type a value which is above 100 like ex. 101" << endl;
    cout<< "give me the "<< k <<"th element ";
    cin >> a[k] >> endl;
    if (a[k]<=100 && a[k]>=0){
      k+=1;
    }
    else{
      break;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我试图将一些介于0和100之间的输入值读入数组,我得到了这个错误."不匹配运营商>>".有什么不对?

c++

4
推荐指数
2
解决办法
150
查看次数

我如何初始化动态分配的字符数组,所以我不会得到那些奇怪的字符?

我想在我的"刽子手"游戏中使用那个字符数组来查看用户当前的进度.

#include <iostream>
#include "randword.h"
#include <fstream>
#include <time.h>
#include <cstdlib>

using namespace std;



int main()
{
InitDictionary();
string tixaio=Randomword();
int m = tixaio.length();
int guesses=8;
char *charptr= new char[m];





for(int aa=0;aa<m;aa++){
charptr[aa]='-';

}

cout << "The word now looks like this: "<<charptr;


}
Run Code Online (Sandbox Code Playgroud)

一旦我尝试cout << charptr我的数组我得到了通常的"---------"加上一些奇怪的字符.我该如何防止这些角色出现?

c++

2
推荐指数
1
解决办法
1851
查看次数

错误:在类方法声明中无效使用void表达式

这是交易.我正在尝试构建一个EditorBuffer类(用于单行的文本编辑).

该类由两个字符堆组成,(stack<char> before , stack<char> after)其中前堆栈表示位于"游标"之前的所有字符,而后堆栈表示"游标"之后的所有字符.在下面显示的声明中,我得到的错误error: Invalid use of void expression对我来说完全不同.

这是方法声明:

void EditorBuffer::moveCursorToEnd()
{
    while (!after.empty())
    {
        before.push(after.pop());
    }

}
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
1612
查看次数

为什么我得到这种输出,因为我知道数组的非初始化整数元素的默认值是0?

#include <iostream>

using namespace std;

void RemoveZeroElements(int arr1[],int arr2[],int &i){
    int n=0;
    int m=0;
    while(n<14) {
        switch(arr1[n]) {
            case 0:
                n+=1;
            break;
            default:
                arr2[m]=arr1[n];
                m+=1;
                n+=1;
                i+=1;
             break;

         }
      }
}


int main()
{
    int ar1[14]={2,4,5,0,7,-9,0,0,11,23,44,0,13,999};
    int ar2[14];
    int efsize=0;

    RemoveZeroElements(ar1,ar2,efsize);

    cout<<"the new array without the zeros has an effective size of "<< efsize << endl;

    for (int i=0;i<14;i++) {

        if(ar2[i]!=0) {
            cout << "the new array has its " << (i+1)<< "th element set to " << 
            ar2[i]<< endl; …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
2
解决办法
80
查看次数

ifstream输出上的奇怪字符

#include <iostream>
#include <string>
#include <fstream>
#include <cstring>

using namespace std;

int main(){
  char a;

  cout << "give me the filename: ";
  cin >> filename;

  ifstream caroll;
  caroll.open(filename.c_str());

  while (a=caroll.get() && !caroll.eof()){
    cout << a << "    ";
  }

  caroll.close();
}
Run Code Online (Sandbox Code Playgroud)

我的输出充满怪异的字符.它们就像是填充了2 0和2 1的小方块.

c++

-1
推荐指数
1
解决办法
758
查看次数

标签 统计

c++ ×6

algorithm ×1

arrays ×1

sequences ×1