小编sht*_*ken的帖子

用两个递归情况确定递归方法的Big O?

我目前正在与计算大O的递归指数函数,它的快捷方式,只要放置n%2 == 0的代码如下努力:

public static int fasterExponent(int x, int n){
    if ( n == 0 ) return 1;
    if ( n%2 == 0 ){
        int temp = fasterExponent(x, n/2);
        return temp * temp;
    }
    return x * fasterExponent(x, --n); //3
}
Run Code Online (Sandbox Code Playgroud)

我理解,如果没有(n%2 == 0)的情况,这个递归指数函数将是O(n).包含(n%2 == 0)的情况会加快执行时间,但我不知道如何确定它的复杂性和它的证据c的值.

java algorithm recursion big-o

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

是什么导致我的循环只在第一次迭代中忽略这个"\ t"?

出于某种原因,我的循环只在我的for循环的第一次迭代后输出"\ t".这是我的循环代码:

input = -1;
private String[] types = {"none", "vanilla", "hazelnut", "peppermint", "mocha", "caramel"};
while ( (input < 0) || (input > (types.length - 1)) ){ //gets user's latte flavor input
        System.out.println("Enter the number corresponding to the latte type you would like:");
        for ( int i = 0; i < types.length; i++ ){
            if ( i <= (types.length - 2) ){ //prints two options per line
                System.out.println(i + ": " + types[i] + "\t" + (i + 1) + …
Run Code Online (Sandbox Code Playgroud)

java system.out

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

为什么编译器认为我的对象声明是函数声明?

我有一个HashTable <Customer>作为另一个类的成员.

HashTable <T>的构造函数采用int值来确定HashTable数组的大小.

HashTable(int numItems) { ... } //constructor
Run Code Online (Sandbox Code Playgroud)

以下声明

HashTable<Customer> customers(10000); //doesn't call constructor???
Run Code Online (Sandbox Code Playgroud)

收到10000下面的"预期的类型说明符"错误.当我删除10000时,我收到错误"找不到客户的函数定义".这让我相信编译器将我的对象声明视为函数声明.

当我使用动态分配声明我的HashTable时,

HashTable<Customer> * customers = new HashTable<Customer>(10000); //works
Run Code Online (Sandbox Code Playgroud)

与编译器没有混淆.

为什么动态分配工作,而不是其他?

编辑:这是一个具有上述相同问题的最小代码.

#ifndef _BUSINESS_LOGIC
#define _BUSINESS_LOGIC

#include "HashTable.h"

class BusinessLogic
{
public:
    BusinessLogic();
    ~BusinessLogic();
    void start(); 

private:
    HashTable<int> * custom = new HashTable<int>(10000); //works
    HashTable<int> customers(10000); //error
};

#endif


#ifndef _HASH_TABLE
#define _HASH_TABLE

template<class T>
class HashTable
{
public:
    HashTable(int numItems) {
        if (numItems <= 0) {
            throw std::invalid_argument("Invalid HashTable size");
        }
        currItems = …
Run Code Online (Sandbox Code Playgroud)

c++ object declare

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

标签 统计

java ×2

algorithm ×1

big-o ×1

c++ ×1

declare ×1

object ×1

recursion ×1

system.out ×1