小编kun*_*l18的帖子

为非常大的'n'找出第n个斐波纳契数

我想知道怎样才能找到第n个斐波那契序列的n个非常大的n值1000000.使用等级 - 学校递推方程fib(n)=fib(n-1)+fib(n-2),找到第50个学期需要2-3分钟!

谷歌搜索后,我开始了解Binet的公式,但它不适合n> 79的值,因为这里说的

有没有算法这样做就像我们找到素数一样?

algorithm math fibonacci

59
推荐指数
5
解决办法
7万
查看次数

如何使用adb.exe搜索文件?

Unix/Linux包含用于搜索文件的"find"命令,但我不知道android中的哪个命令可用于搜索文件.请举个例子.

android adb

30
推荐指数
3
解决办法
7万
查看次数

在Python中读取一行整数

我是Python的初学者,我正在CodeChef解决一个问题,我必须读取一行空格分隔的整数.这就是我在做的事情:

def main():

  t=int(raw_input())    #reading test-cases

  while t!=0:
    n, k=raw_input().split()    #reading a line of two space separated integers
    n, r=int(n), int(r)    #converting them into int
    list=[]
    #reading a line of space separated integers and putting them into a list
    list[-1:101]=raw_input().split()   
Run Code Online (Sandbox Code Playgroud)

现在我将列表中的每个元素转换为整数.有没有更好的方法来做到这一点?请建议一个我可以玩Python的在线资源,并学习提示和技巧!

python

8
推荐指数
1
解决办法
5万
查看次数

将C++字符串变量转换为long

我有一个变量:

string item;
Run Code Online (Sandbox Code Playgroud)

它在运行时初始化.我需要将它转换为long.怎么做?我已经尝试过atol()和strtol()但是我总是分别对strtol()和atol()有以下错误:

cannot convert 'std::string' to 'const char*' for argument '1' to 'long int strtol(const char*, char**, int)'

cannot convert 'std::string' to 'const char*' for argument '1' to 'long int atol(const char*)'
Run Code Online (Sandbox Code Playgroud)

c++ string long-integer

6
推荐指数
4
解决办法
3万
查看次数

查找长度大到10,000的字符串的子序列

我有一个字符串,其大小可以大到"10,000".我必须计算那些可以被9整除的后缀.

SUBSEQUENCE:子序列是一种保持给定字符串的字符顺序的安排.例如:如果给定的字符串是10292,那么它的一些子序列是1,102,10,19,12,12(12是两次2来两次),129,029,09,092等.有些数字不是给定字符串的子序列是:201(2和0不能在1之前),921,0291等.

我试图使用位移生成给定字符串的所有子序列(powerset)并检查每个字符串是否可被9整除.但只要字符串的长度<= 10,这就可以正常工作.在那之后,我没有得到适当的子序列(一些子序列显示负数).

以下是我的代码:

    scanf("%s", &str); //input string 

    int n=strlen(str); //find length of string

    //loop to generate subsequences
    for(i=1;i<(1<<n);++i){

        string subseq;

        for(j=0;j<n;++j){

            if(i&(1<<j)){

                subseq+=str[j]; // generate subsequence
            }
        }

        //convert generated subseq to int; number is 'long' tpye
        number=atol(subseq.c_str());printf("%ld\n", number); 

        //ignore 0 and check if number divisible by 9
        if(number!=0&&number%9==0)count++;
    }

        printf("%ld\n", count);
Run Code Online (Sandbox Code Playgroud)

c++ string permutation

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

C和C++中寄存器变量的地址

我知道寄存器变量的概念和它的用例,但根据我的尝试,我脑子里几乎没有问题.

  1. 我不能在C中访问寄存器变量的地址,虽然我可以做C++!为什么?访问寄存器变量的寻址有什么问题吗?

  2. 假设我在C++中声明一个字符串变量作为寄存器,那么该变量将存储在哪里?在C++中声明非数字数据类型(如'string')的存储类是什么意义?

更新: 我认为C++允许我们获取寄存器变量的地址,因为我的程序中没有出现任何错误,如下所示:

#include<iostream>
#include<time.h>

using namespace std;

clock_t beg, en;

int main(){

    int j, k=0;

    beg=clock();
    for(register int i=0;i<10000000;i++){
        /*if(k==0){
            cout<<&i<<endl;    // if this code is uncommented, then C++ rejects the recommendation to make 'i' as register
            k++;
        }*/
    }
    en=clock();

    cout<<en-beg<<endl;

    cout<<&j<<endl<<&k;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我观察到的是,如果我将变量'i'作为寄存器并且不尝试使用'&i'来打印地址,那么C++接受推荐并将'i'存储在寄存器中,这可以从运行时间到for循环,如果'i'在寄存器中,则总是大约4-12 ms.但是,如果我尝试打印变量'i'的地址,那么虽然我没有得到任何错误,但C++拒绝推荐,这可以从执行循环的时间开始,如果我没有注册,总是超过25! !

所以,基本上我不能用C和C++中的存储类作为寄存器来获取变量的地址!为什么?

c c++ gcc g++ cpu-registers

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

生成子序列

我有一个像"0189"这样的字符串,我需要生成所有子序列,但必须保留各个字符的顺序,即这里9不应该出现在0,1或8之前.例如:0,018,01 ,09,0189,18,19,019等

另一个例子是"10292",子序列将是:1,10,202,02,09,29,92等.你可能已经注意到'02'两次,因为'2'在给定的字符串中出现两次.但是,21,11,91之类的东西也是无效的,因为要维持秩序.

任何算法或伪代码,可以用C/C++实现,将不胜感激!

c c++ algorithm permutation

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

C,C++中的指针

这两个是等价的吗?

int a=10;
int *p=a; 

AND

int a=10;
int *p;
p=&a;
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,p是否保留a的地址!?

c pointers

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

程序崩溃时使用char*

在运行以下代码时,我的程序意外崩溃!

#include<stdio.h>
#include<string.h>

int main(){

    char *str = NULL;
    strcpy(str, "swami");
    printf("%s", str);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但如果我喜欢这样:

#include<stdio.h>
#include<string.h>

int main(){

    char *str;
    strcpy(str, "swami");
    printf("%s", str);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码工作正常并生成正确的输出!

我正在使用gcc编译器(codeblocks IDE).此外,这两个代码都会导致DevCpp中的程序崩溃.任何人都可以解释一下为什么会这样!?

c c++ string pointers

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

FileFilter for JFileChooser不起作用

下面是我的代码,JFileChooser点击一个按钮打开一个.我创建了一个过滤器,只允许选择.jpg文件,但我的代码不能按预期工作.所有类型的文件都显示在JFileChooser对话框中.部分代码:

MyFileFilter filter;

fPhoto=new JFileChooser();
fPhoto.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);        
fPhoto.setFileFilter(filter);
Run Code Online (Sandbox Code Playgroud)

MyFileFilter类:

public class MyFileFilter extends javax.swing.filechooser.FileFilter{

    public boolean accept(File f){

    return f.isDirectory()||(f.isFile()&&f.getName().toLowerCase().endsWith(".jpg"));
    }

    public String getDescription(){

    return ".jpg files";
    }
}
Run Code Online (Sandbox Code Playgroud)

java swing jfilechooser filefilter

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

在动态添加的按钮jQuery上注册click事件

我正在使用bootstrap

以下是我的代码,用于动态获取提要,然后通过在ID为"feeds"的部门内附加内容将其添加到页面上:

$.ajax({
    url: 'http://api.feedzilla.com/v1/categories/'+newsId+'/articles.json',
    type: 'GET',
    dataType: 'json',

    // to execute when successfully got json response
    success: function(response){
        $.each(response.articles, function(){
            var feed=   "<div class='media well'>"+
                            "<div class='media-body'>"+
                                "<h4 class='media-heading'>"+this.title+"</h4>"+
                                "<h6>"+this.publish_date+"</h6>"+
                                "<p>"+this.summary+"</p>"+
                                "<a href='"+this.url+"' target='_blank'>Continue Reading</a>"+
                            "</div><br><br>"+
                            "<button class='showinfo btn pull-right' id='info'>Show Info</button>"+
                        "</div>";

            $('#feeds').append(feed);
        });
    },

    // to execute when error
    error: function(jqXHR, textStatus, errorThrown){
        alert("Server error!");
    },

    // execute at last whether success or error
    complete: function(){

    }
});
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在添加一个类'showinfo'以及id'info'来动态添加按钮.

现在以下是我的事件处理程序:

    // SHOW INFO BUTTON CLICK HANDLER
    $('.showinfo').click(function(){ …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

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

jQuery中的wierd json错误

我跟随json:

{
    "responseHeader": {
        "status": 0,
        "QTime": 1
    },
    "spellcheck": {
        "suggestions": [
            "a",
            {
                "numFound": 4,
                "startOffset": 0,
                "endOffset": 1,
                "suggestion": [
                    "api",
                    "and",
                    "as",
                    "an"
                ]
            },
            "collation",
            "api"
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

我想访问'suggestion'数组,为此我在jQuery中使用它:

$.getJSON('http://localhost:8983/solr/suggest/?q=' + query + '&wt=json&json.wrf=?', {

})
    .done(function(response){
        // just for testing
        alert(response.spellcheck.suggestions.suggestion); // error: response.spellcheck is not defined
    });
Run Code Online (Sandbox Code Playgroud)

'response.spellcheck'的值显示为undefined,而'response.responseHeader'显示[object Object],我也可以访问responseHeader下的元素.但我无法弄清楚'拼写检查'的问题是什么.救命!

javascript jquery json

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