小编Rob*_*obᵩ的帖子

不同语言中相同算法的不同输出

Java源代码:

package n1_problem;

/**
 *
 * @author nAwS
 */
public class loop {

    int loop(long i)
    {
        long n=i;
        int count=1;
        while(n>1){
            if(n%2==0){
                n=n/2;
            }
            else{
                n=3*n+1;
            }
            count++;
        }
       return count; 
    }

    int max_cycle(long j,long k){

        int max=-1;
        for(long i=j;i<=k;i++){
            int count=loop(i);
            if(count>max){
                max=count;
            }
        }
        return max;
    }


}


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        loop lp=new loop();
        System.out.println("Max Cycle:"+lp.max_cycle(1,1000000));
    }

}
Run Code Online (Sandbox Code Playgroud)

C源代码:

int main() …
Run Code Online (Sandbox Code Playgroud)

c java algorithm

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

从循环中的列表中删除元素而不中断

在我的游戏代码中,我想从a中删除一些元素list,
这些元素发生在循环中.我遇到的唯一问题是,当我使用时,
list::erase我必须在该函数之后断开,因为我认为
list变得"过时"了.这会导致一点点闪烁,我想
尝试删除它.

目前的代码是这样的:

for(list<Arrow*>::iterator it = arrows.begin(); it != arrows.end(); it++)
{
    Arrow* a = (*it);

    if(a->isActive() == true)
    {
        a->update();
    }
    else
    {
        arrows.erase(it);
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)

先感谢您!

编辑:对不起,我对矢量和列表感到困惑.得到了答案,谢谢!

c++

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

声明集合中向量的大小.C++

using namespace std;

vector<vector<T> > vec_collection(3);

vec_collection[0]=vector<T>(12);
vec_collection[1]=vector<T>(3);
vec_collection[2]=vector<T>(14);
Run Code Online (Sandbox Code Playgroud)

假设我有3个向量的空集合,并且在初始化之后,我希望第一个,第二个和第三个向量具有特定大小12,3和14.以上代码段是否是声明其大小的正确方法?

c++ vector

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

在32位为什么地址空间限制为2 ^ 31?

在32位为什么地址空间限制为2 ^ 31?而不是2 ^ 32?

windows

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

设置查找成员与使用查找列表

由于标准库集合容器中的项目已排序,因此使用集合中的查找成员通常比在排序列表中的相同项目上使用查找算法执行速度更快吗?

由于列表是线性的,并且通常使用排序树来实现集合,因此似乎set-find应该更快.

c++ std set find

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

c ++,对每个数组成员执行函数

我有一个关于c ++和数组的问题.

假设我有一个名为CustomArray的类,它只不过是具有大小和容量属性的通用数组,以使数组动态化.定义为:

template<typename T>
class CustomArray
{
public:
    int capacity, size;
    T* items;
    //constructor

    //destructor

    //function1

    //function2

    //etc...
};
Run Code Online (Sandbox Code Playgroud)

现在我有点卡住,我想实现一个像以下的功能:"

void performOnAllItems(/*function?*/)
{
    for(int i = 0; i < size; i++)
    {
        //perform function on element
    }
}
Run Code Online (Sandbox Code Playgroud)

将另一个函数作为参数(如果可能的话?)并在所有元素上执行它.那可能吗?如果是的话......怎么样?

提前致谢.

c++ arrays delegates function-pointers

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

重载模板化函数

我怀疑这种方法是否可行,假设你想要两种方法同时调用一个函数,一种是返回一个对象而另一种是通过引用返回参数:

// ...  
template <class T> void func(Foo<T>& f, const T n) 
{ 
    f.a = Something(f.a + n); 
    f.b = Something(f.b + n); 
}

template <class T> Foo<T> func(const Foo<T>& f, const T n) 
{ 
    return Foo<T>( Something(f.a + n), Something(f.b + n) ); 
}
// ...


// main
Foo<int> foo(1, 1);

func(foo, 2);
Foo<int> foo2 = func(foo, 2);
Run Code Online (Sandbox Code Playgroud)

第一个参数中的const字是否影响方法的签名?

c++ overloading

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

C++上的未知错误,错误:';'之前的预期主表达式 代币

C++上的未知错误,错误:';'之前的预期主表达式 令牌.这是我用C++编写的代码:

 #include <iostream>
 #include <math.h>
 #include <stdio.h>
 #define G 6.674E-11
 using namespace std;

 int main()
 {
 //Ms = Mass of sun, Me = Mass of Earth, Fg = Gravitational force between them, As =                                   Acceleration of Sun, Ae = Acceleration of Earth, Ve_x
 // = initial velocity of Earth in x direction, Ve_y = initial velocity of Earth in y          direction, Vs_x = initial velocity of the Sun in x direction
 // Vs_y = initial velocity of sun in …
Run Code Online (Sandbox Code Playgroud)

c++

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

已排序的不是优先级队列的容器

我们需要一个容器,在附加一个新元素时,按元素的优先级对其元素进行排序,该元素能够在给定id的情况下检索元素.

(优先级队列的问题是它不能让你根据id而不是优先级检索元素)

谢谢

c++ priority-queue

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

防止makefile重新编译所有源文件

我已经尝试了一个多小时来修复我的makefile但是我打破了比我修复更多的东西.无论如何,我对Makefiles非常(非常)新手,我几乎没有经验.我的问题是我的makefile重新编译每个源文件,即使已经构建了目标文件(并且源尚未更新).因为makefile非常像声明性编程,所以我不知道我做错了什么.

对不起,简短说明买我在这个领域的知识是非常有限的...

#---------------------------------------------------------
# Compiler and linker flags
#---------------------------------------------------------

CC      =   g++
CMNFLAGS    =   -ggdb3 -Wextra -Wall -Wno-int-to-pointer-cast -Wno-reorder -Wno-    write-strings -DOPT_TYPE="\"debugging\""
CFLAGS      =   $(CMNFLAGS) -fPIC
LDFLAGS     =   $(CMNFLAGS) -shared -ldl -lm -static-libgcc

#---------------------------------------------------------
# All the different directories
#---------------------------------------------------------

BUILD       =   build
SOURCE      =   source
INCLUDE     =   include
TARGET      =   $(BUILD)/$(shell basename $(CURDIR))_mm_i386.so

#---------------------------------------------------------
# Directory and include variables/files
#---------------------------------------------------------

SRCSDK      =   ../sdk
METADIR     =   $(INCLUDE)/metafiles

INSTDIR     =   /usr/local/test/$(shell basename $(CURDIR))/dlls
INCLUDES    =   -I$(INCLUDE) -I$(METADIR) -I$(SRCSDK)/engine -I$(SRCSDK)/common \
        -I$(SRCSDK)/pm_shared …
Run Code Online (Sandbox Code Playgroud)

makefile compilation

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