我正在尝试解决这样的问题:
我给了n个数字(1 <= n <= 10 ^ 5).我必须在左边写下所有数字的总和,这些数字小于当前数字并重复所有n个数字的过程.然后我必须找到所有先前获得的和的总和.(每个数字N,0 <= N <= 10 ^ 6).
例如,
1 5 3 6 4
less1 less5 less3 less6 less4
(0) + (1) + (1)+(1+5+3)+(1+3)
0 + 1 + 1 + 9 + 4
= 15
Run Code Online (Sandbox Code Playgroud)
这个问题的一个简单的解决方案是运行两个循环,并且对于给定数字中的每一个,找到小于该数字的所有数字的总和,最后给出这些和的总和作为输出.时间复杂度为O(n ^ 2) .
我认为使用二进制索引树(Fenwick树)可以更好地解决这个问题的O(nlogn)解决方案.对于每个数字,我将在全局数组a中添加每个数字并执行BIT的两个明显操作.我认为该算法的时间复杂度为O(nlogn),如果为真,则明显优于之前的O(n ^ n ^ 2).
我用C++实现了代码.
#include<iostream>
#include<cstdio>
using namespace std;
#define max 1000001
long int a[max];
void add(long int v,int idx){
while(idx<max){
a[idx] += v;
idx += (idx & -idx);
}
}
long int …Run Code Online (Sandbox Code Playgroud) 我正在解决一个问题,给定一个数字 n,我必须找到下一个具有相同数量的设置位的较大元素。在互联网上搜索时,我发现了一段有趣的代码,它只需几行代码(BIT MAGIC)即可完成此操作:
unsigned nexthi_same_count_ones(unsigned a) {
/* works for any word length */
unsigned c = (a & -a);
unsigned r = a+c;
return (((r ^ a) >> 2) / c) | r);
}
Run Code Online (Sandbox Code Playgroud)
但我想了解该算法始终有效的基本逻辑。所有边界情况都将得到妥善处理。
有人可以用简单的步骤解释一下逻辑吗?
谢谢
我正在开发一个项目,需要用Java设计一个Web爬虫,可以让用户查询特定的新闻主题,然后访问不同的新闻网站,然后从这些页面中提取新闻内容并将其存储在一些文件/数据库中.我需要这个来总结整个存储的内容.我是这个领域的新手,所以希望得到一些有经验的人的帮助.
现在我有了从单个页面中提取新闻内容的代码,该页面手动获取页面,但我不知道如何将其集成到Web爬虫中以从不同页面中提取内容.
任何人都可以提供一些很好的链接到Java的教程或实现,我可以根据我的需要使用或修改?
我已经读过线性丢番图方程,例如ax+by=c称为丢番图方程,只有在给出整数解的情况下gcd(a,b) divides c.
这些方程在编程竞赛中非常重要.当我遇到这个问题时,我只是在网上搜索.我认为它是丢番图方程的一种变体.
问题:
我有两个人,人X和Y都站在一根绳子中间.人X可以一次向左或向右跳A或B单位.人Y可以一次向左或向右跳C或D单位.现在,我给了一个K号,我必须找到号码.在[-K,K]范围内的绳索上的可能位置,使得两个人可以使用它们各自的电影任意次数到达该位置.(A,B,C,D和K有问题).
我的解决方案
我认为问题可以使用丢番图方程在数学上解决.
我可以为Person X形成一个等式A x_1 + B y_1 = C_1 where C_1 belongs to [-K,K],类似于Person Y 的等式C x_2 + D y_2 = C_2 where C_2 belongs to [-K,K].
现在我的搜索空间减少到只找到C_1和C_2相同的可能值的数量.这将是我对这个问题的回答.
为了找到这些价值,我只是发现gcd(A,B)和gcd(C,D)再取LCM这两个GCD的获得LCM(gcd(A,B),gcd(C,D)),然后简单地计算范围在[1,K]这点的数量是这个倍数LCM.
我的最终答案是2*no_of_multiples in [1,K] + 1.
我尝试在我的C++代码中使用相同的技术,但它不起作用(错误的答案).
这是我的代码:http: //pastebin.com/XURQzymA
我的问题是:如果我正确使用丢番图方程,有人可以告诉我吗?
如果是的话,任何人都可以告诉我可能出现逻辑失败的情况.
这些是在问题陈述的网站上给出的一些测试用例.
ABCDK以相同的顺序给出,相应的输出在下一行给出:
2 4 3 6 7
3
1 2 …
当我发现数组大小必须在声明时给出或者在运行时使用malloc从堆分配时,我正在读一本书.我在C中编写了这个程序:
#include<stdio.h>
int main() {
int n, i;
scanf("%d", &n);
int a[n];
for (i=0; i<n; i++) {
scanf("%d", &a[i]);
}
for (i=0; i<n; i++) {
printf("%d ", a[i]);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常.
我的问题是这段代码是如何正常工作的.不是违反C的基本概念,数组大小必须在运行时之前声明或者在运行时使用malloc()分配它.我不做这两件事中的任何一件,那它为什么它正常工作?
我的问题的解决方案是C99支持的可变长度数组,但是如果我玩我的代码并将语句放入int [n]; 在scanf("%d,&n)之上;然后它停止工作为什么它如此.如果C中支持可变长度数组?
可能重复:
使用%f打印整数变量
#include<stdio.h>
int main()
{
printf("%f",9/5);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:0.000000
任何人都可以解释上述程序的输出吗?
不应该是程序的输出是1.000000?
我认为这个问题很容易理解.为了更清楚,我举例说明:
在2位数的回文列表中,第7回文是77(第1是11,第2是22,依此类推).
显然存在蛮力解决方案,但效率不高.
谁能建议我一些更好的解决方案来解决问题?
我正在尝试在我的项目中使用角度ui-select但不知何故css无法正常工作.
index.html的:
<link rel="stylesheet" href="bower_components/bootstrap-social/bootstrap-social.css" />
<link rel="stylesheet" href="bower_components/angularjs-slider/dist/rzslider.css" />
<link rel="stylesheet" href="bower_components/angular-dropdowns/dist/angular-dropdowns.css" />
<link rel="stylesheet" href="bower_components/angular-loading-bar/build/loading-bar.css" />
<link rel="stylesheet" href="bower_components/textAngular/dist/textAngular.css" />
<link rel="stylesheet" href="bower_components/perfect-scrollbar/src/perfect-scrollbar.css" />
<link rel="stylesheet" href="bower_components/fullcalendar/dist/fullcalendar.css" />
<link rel="stylesheet" href="bower_components/angularjs-toaster/toaster.css" />
<link rel="stylesheet" href="bower_components/ngprogress/ngProgress.css" />
<link rel="stylesheet" href="bower_components/angular-ui-select/dist/select.css" />
<script src="bower_components/ng-file-upload-shim/ng-file-upload-shim.js"></script>
<script src="bower_components/perfect-scrollbar/src/perfect-scrollbar.js"></script>
<script src="bower_components/angular-perfect-scrollbar/src/angular-perfect-scrollbar.js"></script>
<script src="bower_components/moment/moment.js"></script>
<script src="bower_components/fullcalendar/dist/fullcalendar.js"></script>
<script src="bower_components/angular-ui-calendar/src/calendar.js"></script>
<script src="bower_components/angularjs-geolocation/src/geolocation.js"></script>
<script src="bower_components/angularjs-toaster/toaster.js"></script>
<script src="bower_components/ngprogress/build/ngProgress.js"></script>
<script src="bower_components/angular-ui-select/dist/select.js"></script>
Run Code Online (Sandbox Code Playgroud)
包装版本:
Angularjs: 1.4.5
ui-bootstrap: 0.11.2
Angular ui-select: 0.12.1
Run Code Online (Sandbox Code Playgroud)
html文件:
<ui-select ng-model="stream.selected" theme="select2" ng-disabled="disabled" style="min-width: 300px;">
<ui-select-match …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 api 将照片添加到 Foursquare 页面:https ://api.foursquare.com/v2/photos/add 和以下 node.js 代码:
var accessToken = "myAccessToken";
var platformProfileId = "4squarePageId";
var b64content = "somebase64stringrepresentationofimage";
var url = "https://api.foursquare.com/v2/photos/add";
var formObj = {'oauth_token': accessToken, v: '20151009', 'pageId': platformProfileId, 'photo': b64content};
request({
url: url, //URL to hit
form: formObj, //form data
method: 'POST',
headers: { 'Content-Type': 'image/jpeg' }
}, function(error, response, body){
if(error) {
console.log(error);
return cb(error);
} else {
if(typeof body != 'object') {
body = JSON.parse(body);
}
console.log(body);
if(('meta' in body) && …Run Code Online (Sandbox Code Playgroud) 在使用 AES 加密和解密时,我正面临着本线程中提到的完全相同的问题。
crypto.js:202
var ret = this._handle.final(); ^ 错误:错误:0606506D:数字信封例程:EVP_DecryptFinal_ex: Decipher.Cipher.final (crypto.js:202:26)
处错误(本机)的最终块长度错误
这些是我的加密和解密功能:
var config = {
cryptkey: crypto.createHash('sha256').update('Nixnogen').digest(),
iv: "a2xhcgAAAAAAAAAA"
};
function encryptText(text){
console.log(config.cryptkey);
var cipher = crypto.createCipheriv('aes-256-cbc', config.cryptkey, config.iv);
var crypted = cipher.update(text,'utf8','binary');
crypted += cipher.final('binary');
crypted = new Buffer(crypted, 'binary').toString('base64');
return crypted;
}
function decryptText(text){
console.log(config.cryptkey);
if (text === null || typeof text === 'undefined' || text === '') {return text;};
text = new Buffer(text, 'base64').toString('binary');
var decipher = crypto.createDecipheriv('aes-256-cbc', config.cryptkey, config.iv);
var dec …Run Code Online (Sandbox Code Playgroud) algorithm ×4
c ×4
c++ ×3
javascript ×2
node.js ×2
aes ×1
algebra ×1
angularjs ×1
arrays ×1
cryptography ×1
css ×1
declaration ×1
encryption ×1
equation ×1
foursquare ×1
java ×1
mongoose ×1
optimization ×1
palindrome ×1
request ×1
ui-select ×1
web-crawler ×1
web-scraping ×1