define array

int arr1[10 + 1];
const int n = 2;
int arr2[n];

posted @ 2011-10-02 14:46 メmarsメ 阅读(288) | 评论 (0)编辑 收藏

1000瓶药水,其中至多有1瓶剧毒,现在给你10只小狗在24小时内通过小狗试药的方式找出哪瓶药有毒或者全部无毒(小狗服完药20小时后才能判断是否中毒)

把狗从0-9编号
把药水按1-1000编号
把药水编号按二进制,如果第i位(因为最大1000,所以bit位为0-9)bit位为1,则分给编号为i的狗狗喝
最后得一二进制数,如果编号为i的狗狗死了,该数的第i bit位为1,该数就是有毒的药水编号

每2.4分钟给一只狗狗喝一瓶药,并记录,若有狗狗中毒,查看时间记录就知道哪瓶药有毒了

posted @ 2011-10-02 14:04 メmarsメ 阅读(1195) | 评论 (2)编辑 收藏

静态常成员变量运用

// test.h

class C{
public:
 static const int M;
};
const int C::M = 12;

// test.cpp
#include "stdafx.h"
void fun(){
 cout << C::M << endl;
}

// main.cpp
#include "stdafx.h"
#include "test.h"


void fun();

int main(){
 fun();
}

// 会出错, 静态常成员变量必须在静态联编的时候解析,而调用fun本就是【另外一个文件中】的函数,因此在link的时候解析,如

果把fun调到main中,则可以正常运行.

posted @ 2011-10-02 11:03 メmarsメ 阅读(355) | 评论 (0)编辑 收藏

GetAsyncKeyState()& 0x8000f

GetAsyncKeyState函数功能:读取的是物理键状态,也是就是不管你怎么鼠标键盘映射,它只读取实际的按键状态。MSDN上给出了例子很恰当For example, the call GetAsyncKeyState(VK_LBUTTON) always returns the state of the left physical mouse button, regardless of whether it is mapped to the left or right logical mouse button.也就是说如果你重新设置了映射,GetAsyncKeyState还是只读取物理状态

GetAsyncKeyState的返回值:表示两个内容,一个是最高位bit的值,代表这个键是否被按下,按下为1,抬起为0;一个是最低位

bit的值,在windowsCE下要忽略(参考自MSDNIf the most significant bit is set, the key is down. The least significant

bit is not valid in Windows CE, and should be ignored.)

 
note:
GetAsyncKeyState('M')& 0x8000f // 要大写M, f的意思是float的意思

posted @ 2011-10-01 18:30 メmarsメ 阅读(1454) | 评论 (3)编辑 收藏

巧妙的算法

有5个人比赛,采用淘汰制。那么第一轮有1人轮空,淘汰2人。剩下3人继续,又一人轮空,最后2人决赛。这样最后有2个人轮空。

那么37个人比赛有几个人轮空呢?

可以这么算:用比37大的最小的2的幂次的数也就是64,去减37,64-37=27,而27=16+8+2+1,所以27的二进制是11011,

数一数有四个1,所以37人比赛有4人轮空。
按此在新窗口浏览图片

posted @ 2011-09-30 16:37 メmarsメ 阅读(233) | 评论 (0)编辑 收藏

判断另一字符串的所有字母是否在母串中都有

From:A Google Interviewing Story
Q:
Say you have one string of alphabetic characters, and say you have another, guaranteed smaller string of alphabetic characters. Algorithmically speaking, what's the fastest way to find out if all the characters in the smaller string are in the larger string?

For example, if the two strings were:


String 1: ABCDEFGHLMNOPQRS
String 2: DCGSRQPOM


You'd get true as every character in string2 is in string1. If the two strings were:


String 1: ABCDEFGHLMNOPQRS
String 2: DCGSRQPOZ

A:
Finally, I told him the best method would simply be O(n+m). That is, iterate through the first string and put each character in a hashtable (cost of O(n) or 16). Then iterate the 2nd string and query the hashtable for each character you find. If its not found, you don't have a match. That would cost 8 operations - so both operations together is a total of 24 operations. Not bad and way better than the other solutions.


He stepped up to the whiteboard, "What if - given that we have a limited range of possible characters - I assigned each character of the alphabet to a prime number starting with 2 and going up from there. So A would be 2, and B would be 3, and C would be 5, etc. And then I went through the first string and 'multiplied' each character's prime number together. You'd end up with some big number right? And then - what if I iterated through the 2nd string and 'divided' by every character in there. If any division gave a remainder - you knew you didn't have a match. If there was no remainders through the whole process, you knew you had a subset. Would that work?"

Every once in awhile - someone thinks so fantastically far out of your box you really need a minute to catch up. And now that he was standing, his leather pants weren't helping with this.

Now mind you - Guy's solution (and of course, needless to say I doubt Guy was the first to ever think of this) was algorithmically speaking no better than mine. Even practically, you'd still probably use mine as it was more general and didn't make you deal with messy big integers. But on the "clever scale", Guy's was way, way, (way) more fun.


you'd get false as Z isn't in the first string.

posted @ 2011-09-30 13:24 メmarsメ 阅读(418) | 评论 (0)编辑 收藏

VS2010 下DirectX 配置问题

首先DirectX要下载最新版,然后设置好[包含目录]和库文件就OK了
d3d9.lib
d3dx9.lib
winmm.lib
Gdi32.lib
user32.lib


否则可能出现如下问题:
Mantis.obj : error LNK2001: unresolved external symbol __imp__DispatchMessageA@4
Mantis.obj : error LNK2001: unresolved external symbol __imp__TranslateMessage@4
Mantis.obj : error LNK2001: unresolved external symbol __imp__GetMessageA@16
Mantis.obj : error LNK2001: unresolved external symbol __imp__LoadStringA@16



error LNK2019: 无法解析的外部符号 _Direct3DCreate9@4


posted @ 2011-09-26 22:15 メmarsメ 阅读(1300) | 评论 (0)编辑 收藏

栈和静态数据的大小限制

栈的大小限制:1M,随编译器不同而不同
静态数据的上限,不能超过进程的最大使用的内存空间

posted @ 2011-09-26 20:15 メmarsメ 阅读(183) | 评论 (0)编辑 收藏

数列L中有n个整数,其中K个数字出现了两次,1个数字出现了一次,所以n=2k+1;请在使用O(1)空间的前提下,尽快找出只出现一次的那个数字,并说明算法的复杂度。

数列L中有n个整数,其中K个数字出现了两次,1个数字出现了一次,所以n=2k+1;请在使用O(1)空间的前提下,尽快找出只出现一次的那个数字,并说明算法的复杂度。

既然是空间复杂度的限制
所以 
int res = 0;
forint i = 0; i < n; i++ ){
    res 
^= arr[i];
}

return res;
最终有
1.两个相同的数异或结果为0
2.0与任何数异或结果还是这个数

posted @ 2011-09-19 19:28 メmarsメ 阅读(1137) | 评论 (0)编辑 收藏

仅列出标题
共5页: 1 2 3 4 5 
<2011年12月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜