我现在脑震荡,所以我想问一个非常简单的问题.
Currenly,我试图打印出这样的开头
当输入为7时,输出为
*
**
*
**
*
**
*
Run Code Online (Sandbox Code Playgroud)
在这里,我的代码是,它打印14次而不是7次,或者当我把N/2它打印出来时不打印奇数.
#include <iostream>
using namespace std;
int main () {
int N;
cout << " Please enter N " ;
cin >> N;
for (int i = 0; i < N ; i++) {
cout << "*" << endl;
for (int j = 0; j < 2; j++) {
cout << "*" ;
}
cout << endl;
}
}
Run Code Online (Sandbox Code Playgroud) 基本上,我将长度作为“移动”窗口,因为它通过 j 循环到向量大小的末尾。这个向量充满了股票价格。
如果数字 1 2 3 4 的 2 天移动平均线的长度等于 2。我应该能够输出 1.5、2.5 和 3.5。但是,我收到超出范围的错误。
逻辑如代码所示。如果专家可以帮助我使用我正在尝试创建的这个简单的移动平均函数,那就太好了!谢谢。
void Analysis::SMA()
{
double length;
cout << "Enter number days for your Simple Moving Average:" << endl;
cin >> length;
double sum = 0;
double a;
while (length >= 2){
vector<double>::iterator it;
for (int j = 0; j < close.size(); j++){
sum = vector1[length + j - 1] + vector1[length + j - 2];
a = sum / length;
vector2.push_back(a);
vector<double>::iterator …Run Code Online (Sandbox Code Playgroud) 我试图了解DFS 的复杂性如何/为什么是 O(V+E)。这是我分析伪代码迭代 DFS 复杂性的尝试。
DFS(G, t)
{
1 stack S = new empty Stack of size G.|V| ... O(1)
2 S.push(t) ... O(1)
3 while(S is not Empty) ... O(|V|), this will always be =<|V|
{
4 u = S.pop() ... O(1)
5 if(u.visited = false) ... O(1)
{
6 u.visited = true ... O(1)
7 for-each edge (u,v) in G.E ... O(|E|), this will always be =<|E|
8 if(v.visited = false) ... O(1)
9 S.push(v) …Run Code Online (Sandbox Code Playgroud) algorithm big-o time-complexity nested-loops asymptotic-complexity
除了这本书,我没有编码经验
编程Swift!尼克史密斯的Swift 2 Kindle版(作者)
我现在在分会
5.3嵌套FOR LOOPS
// NESTED FOR LOOP#2
这段代码 -
for var a = 0; a < 11; a++ {
print("")
for var b = 0; b < a; b++ {
print("*", terminator: " ")
}
}
Run Code Online (Sandbox Code Playgroud)
现在[经过几个/ 4小时'奇怪']我简单地无法解决如何改变上面'简单'[如果你知道如何]代码生成这个模式?
我(想想我)可以看到外部和内部循环我无法解决剩下的问题!我试过了我能想到的每一个变化!(并且我知道只做"排列"并不意味着我对我想要做的事情有了真正的了解!...)
尝试使用 - 操作员和更改[大多数/所有]值[但'排列'是一种有限的方法]
我觉得自己像个傻瓜,但如果这是我第一次看到这些东西,也许它不是那么糟糕,这些东西需要学习!
帮助(答案LOL)将非常感激
我写了这个嵌套的"for"程序,但第二个循环只工作一次并打印一行"x"
#include <stdio.h>
void draw_box(int , int);
int main(int argc, char **argv)
{
draw_box(8 , 35);
return 0;
}
void draw_box(int row , int column)
{
for(;row>0;row--)
{
for(;column>0;column--)
printf("X");
printf("\n");
}
}
Run Code Online (Sandbox Code Playgroud) 我需要写一下这句话:
for ( int i1 = i1_min; i1 < width; ++i1 )
for ( int i2 = i2_min; i2 < i2_max; ++i2 )
for ( int i3 = i3_min; i3 < i3_max; ++i3 )
...
for ( int iN = iN_min; iN < iN_max; ++iN )
Run Code Online (Sandbox Code Playgroud)
怎么做?
这是Irvine基于英特尔计算机的汇编语言练习:
在这个例子中,EAX的最终价值是多少?
mov eax,0
mov ecx,10 ; outer loop counter
L1:
mov eax,3
mov ecx,5 ; inner loop counter
L2:
add eax,5
loop L2 ; repeat inner loop
loop L1 ; repeat outer loop
Run Code Online (Sandbox Code Playgroud)
我的解决方案是eax = 28,因为最初的ecx设置为5,所以L2执行5次,因此eax = 3 + 5*5 = 28.然后ecx变为0并且流程移出L2,但由于ecx = 0,L1不会重复,它将移动到下一行而不重复L1.所以eax的最终值= 28.我知道这是错的,但有人能说出我的想法中的错误在哪里?
我正在使用 Wordpress 的高级自定义字段并尝试在组内循环转发器。我得到的只是“注意:数组到字符串的转换......”
出了什么问题,我该如何解决?
<?php if( have_rows('start_horlurar') ): while ( have_rows('start_horlurar') ) : the_row(); ?>
<?php $horlur = get_sub_field('horlur'); ?>
<?php if( have_rows( $horlur['arsmodeller_lankar']) ): while ( have_rows($horlur['arsmodeller_lankar']) ) : the_row(); ?>
<?php echo get_sub_field('lank'); ?>
<?php endwhile; endif; ?>
<?php endwhile; endif; ?>
Run Code Online (Sandbox Code Playgroud) php wordpress while-loop nested-loops advanced-custom-fields
I’m in need of some help and guidance on the design of my code. I want to run tests with multiple variables set to multiple values, without creating insane amounts of nested loops. I got a struct which holds various variables like this (only three integers as an example, but the real deal will hold a lot more, including booleans, doubles etc):
struct VarHolder
{
int a;
int b;
int c;
// etc..
// etc..
};
Run Code Online (Sandbox Code Playgroud)
The struct get passed …
我正在查看以下代码
public class Solution {
public boolean judgeSquareSum(int c) {
for (long a = 0; a * a <= c; a++) {
for (long b = 0; b * b <= c; b++) {
if (a * a + b * b == c)
return true;
}
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
作者说这段代码的时间复杂度是?c。我不明白的是如何。
所以假设我们给出了一个 c=20 的例子。那么代码将运行 15 次但是?20=4.47