我需要计算实际索引左侧和右侧数组中的最大数字.我的意思是如果我的阵列是
1, 3, 2, 4, 3;
Run Code Online (Sandbox Code Playgroud)
我需要输出:
//Left Right
1 4 //no bigger on left, so 1; the biggest on right is 4
3 4 //and so on with rest points
3 4
4 4
4 3
Run Code Online (Sandbox Code Playgroud)
我需要非常快速地做到这一点,所以只是在左边然后在右边兜售是一个坏主意.我决定在右边找到最大的,然后当我到达那里时计算下一个,依此类推.左边最大,只要实际大于它,改变它.但它不起作用......程序有时会输出错误的结果.不幸的是,我不知道它做了什么输入......
这是我的代码,如果你能看到任何一般或算法错误我很高兴如果你可以发布它...
#include <cstdlib>
#include <iostream>
using namespace std;
inline int findmax(int tab[], int beg, int end)
{
int max=0;
for (int j=beg; j<end; j++)
if (tab[j]>max) max=j;
return max;
}
int main(int argc, char *argv[])
{
int len;
cin>>len;
int h[len];
for …Run Code Online (Sandbox Code Playgroud) 我有一些圈子,我知道他们的X,Y和r.我想检查他们中的任何一个是否与其他任何人一致......检查的方法很简单:
r 1 + r 2 <sqrt((x 1 -x 2)2 +(y 1 -y 2)2)
但是我必须全部检查所有吗?它给了我O(n 2)的复杂性,我想避免这个:/
我的rts中有一张二维地图.在地图上有一些单位.我想检查是否有任何单位在另一个范围内.单位范围以字段给出.看图像:

在图片中,没有单位(红色,蓝色,绿色)可以互相攻击.我想,例如检查,例如是否有任何蓝色范围内的单位.答案是不.我知道蓝色的范围和位置,我也知道其余的位置.我也知道地图xy是否被占用.我怎么检查这个?
当我在VS2012中调试我的应用程序并且它崩溃时,输入(鼠标和键盘)开始非常滞后,fps降至约0.3或更低,我甚至无法移动鼠标而不等待3秒......唯一的解决方案是做Shift-F5,这将结束调试,然后每一个都很好.
更有趣的是,唯一滞后的是输入,整个背景工作得很好,文本插入符号以正常速率闪烁,当鼠标越过按钮时工具提示很好地动画.
我用allegro 4.2编译项目(我必须使用它,解释原因需要很长时间).
我没有扩展,一个非常快的pc应该能够处理调试...
我对任何解决方案感兴趣,它可能是脏/ hackish ...我当然可以提供更多信息,如果需要的话.
谢谢你的帮助.
编辑:通过论坛阅读我发现了一些关于"自动"窗口的信息或类似的东西(不记得确切,再也找不到它),这是做一些"后台任务",导致滞后......你认为在单独的核心上运行它会解决这个问题吗?
lag visual-studio visual-studio-debugging visual-studio-2012
我有4个文件:
shared.h:
#ifndef SHARED_H
#define SHARED_H
int* sth;
#endif
Run Code Online (Sandbox Code Playgroud)
something.h:
#ifndef SOMETHING_H
#define SOMETHING_H
class foo
{
public:
void printVar();
};
#endif
Run Code Online (Sandbox Code Playgroud)
something.cpp:
#include <iostream>
#include "something.h"
#include "shared.h"
using namespace std;
void foo::printVar()
{
cout<<"Foo: "<<*sth<<endl;
};
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <cstdlib>
#include <iostream>
#include "shared.h"
#include "something.h"
using namespace std;
int main(int argc, char *argv[])
{
sth=new int(32);
foo x;
cout<<"Main: "<<*sth<<endl;
x.printVar();
system("PAUSE");
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
编译器返回*sth的multipe定义;
我在*sth中添加了静态修饰符并编译,但崩溃了.我更改了打印以打印指针的地址,我返回了程序:
Main: 0x3e0f20
Foo: 0
Run Code Online (Sandbox Code Playgroud)
为什么没有分配foo的指针?我想在main中只分配一次,然后在其他文件中共享...我该怎么做?它是extern修饰符的东西吗? …
我想在2d平台游戏,teeworlds找到路径.那里的玩家可以向左/向右移动,跳跃和使用钩子,这可以让你向上移动墙壁或在天花板下移动.那么,它很难因为像*或bfs这样的正常路径存在,因为你不能向上移动.我需要找到路径btw 2玩家,所以1可以去第二个.有3种类型的瓷砖,碰撞,nohook(你不能挂钩)和什么(空气).我有格式的地图,int map[w][h]其中0 =空气,1 =碰撞,2 = nohook.地图未针对整个游戏时间进行修改.
我完全不知道该怎么做.如果你能帮助我,我会很高兴.
PS.关于平台游戏的问题很普遍,teeworlds只是其中之一......
我已经说过像abca.我想知道我需要添加多少个字母以使其成为回文.在这种情况下它是1,因为如果我添加b,我得到abcba.
我正在制作一个简单的套接字应用程序,通过TCP连接我的服务器.我有时需要读取2个字节的值,所以它们都像:
public byte[] read(int bytes)
{
byte b[] = new byte[bytes];
try {
in.read(b); //in is InputStream from properly connected Socket.getInputStream()
return b;
} catch (IOException e) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
此函数应接收给定的字节数并将其返回到数组中.问题是,有时它会在休息可用之前读取一个字节并返回奇怪的数据.
byte a[]=read(2); //Program is blocked here untill some bytes arrive...
System.out.prntln(a[0]); //always correct
System.out.prntln(a[1]); //unprintable character (probably 0 or -1)
Run Code Online (Sandbox Code Playgroud)
我的快速修复是添加while循环检查是否有足够的数据来读取:
public byte[] read(int bytes)
{
byte b[] = new byte[bytes];
try {
while (in.available()<bytes); //It does the thing
in.read(b);
return b;
} catch (IOException e) { …Run Code Online (Sandbox Code Playgroud) 该程序必须计算标准输入的不同数字.例如,输入时
6 <- how many numbers
1
3
1
4
3
786345
Run Code Online (Sandbox Code Playgroud)
它应该给出
4
Run Code Online (Sandbox Code Playgroud)
这是我的溶剂,虽然它太慢了.有更快的方式吗?
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
vector<int> occured; //vector of numbers that occured already
/*
int findType(int find)
finds where given number should be, I want to keep this array sorted.
in case of number existed already return -1; otherwise return the index for inserting.
*/
int findType(int find)
{
for (int i=0; i<occured.size(); i++) //for each number in occured array
if …Run Code Online (Sandbox Code Playgroud) 例如,我有一个数组
$arr=array(
"foo" => "fooval",
"boo" => "booval",
"roo" => "rooval",
);
Run Code Online (Sandbox Code Playgroud)
然后我想打印模式"key is value"中的所有元素.这段代码应该完成这项工作:
foreach($arr as $key => $val)
echo $key." is ".$val;
Run Code Online (Sandbox Code Playgroud)
我能得到这个吗?
foo is fooval
boo is booval
roo is rooval
Run Code Online (Sandbox Code Playgroud)
我是指订单.是否保证它将以与给定数组相同的顺序执行,或者数组以某种方式排序?
谢谢你的回答.