由买买提看人间百态

topics

全部话题 - 话题: unsigned
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
t*******r
发帖数: 22634
1
虽然俺灌水比较无厘头,不过俺认真一下回答你这个问题。
如果一个 formal system 是这么建立的:
任何一个符号或者(1)”是一个明确的有限集”,或者(2)“是由明确的有限集
递归生成的”。那么这个 formal system 里面不需要 “从无限集 减去 无限集
得到 有限集” 这样的算子。
所以这样的一个 formal system 不需要你这个“从无穷集筛选”的操作。
对于这个实际例子 “小于 a 的自然数”,在这样的 formal system 里面,并
没有发生把 “大于 a 的所有自然扔掉” 这样一个操作。取而代之的,是下面这样
的操作:
(a)“自然数递归生成至 a 结束”,产生一个 “前N个连续自然数” 的 token。
(这个 token 实际上是个有限集)。
(b)从(a)产生的 token 里面去掉最大的数,产生另一个 token。
(c)test 自然数 x 是不是在(b)产生的 token 里。(operator:in_set())。
更确切的说,"(a)" 其实发生得比上面看起来的更早。"(a)" 需要发生
在你 refer 任何自然数之前。... 阅读全帖
t******n
发帖数: 2939
2
☆─────────────────────────────────────☆
l63 (l63) 于 (Thu May 23 00:34:22 2013, 美东) 提到:
假设素数只有有限个, 记为 p_1,p_2,...,p_k
考察 N = p_1*p_2*...*p_k + 1
可知: 对于任意i = 1,2,3,...,k, p_i 不能整除 N
由素数的定义:
a是素数 <=> a是大于1的自然数, 且a不被任何小于a的素数整除
可知: N是素数
这与素数只有p_1,p_2,...,p_k矛盾.
故假设不成立.
所以素数有无穷多个.
☆─────────────────────────────────────☆
l63 (l63) 于 (Thu May 23 00:37:03 2013, 美东) 提到:
在承认素数的这个等价定义 (即 a是素数 <=> a是大于1的自然数, 且a不被任何小于a
的素数整除) 的前提下, 居然有人会认为这个证明是错的, 或者是不完备的.
我实在不能理解.
求问一下大家, 是不是有的人的脑子天生有缺陷, 根本怎么教都不会明白... 阅读全帖
f*****r
发帖数: 229
3
来自主题: CS版 - 谁有什么solution吗?

treat
O(1)
it
C/C++.
Let us get the Linux version of bitcount:
/*
41 * hweightN: returns the hamming weight (i.e. the number
42 * of bits set) of a N-bit word
43 */
44
45 static inline unsigned int generic_hweight32(unsigned int w)
46 {
47 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
48 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
49 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
50 res = (res & 0x00FF00FF) + ((res >> 8) & 0x0
b******p
发帖数: 23
4

()
thread
If the seed is a global variable, then how can calling srand multiple times be
of any help?
Following is the typical rand and srand implementation in a standard C lib:
unsigned long next=1;
int rand(void) /* NOT RECOMMENDED (see text) */
{
next = next*1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed)
{
next=seed;
}
l***s
发帖数: 1
5
in c/c++ you can use
struct node_t {
unsigned int bit31: 1;
unsigned int bit30: 1;
....
unsign int bit0: 1;
};
bit0~bit31 are one bit data member. if you define only one 1-bit data
memeber
it will occupy at least 32bits for alignment.arthben (tu) 的大作中提到: 】
y******n
发帖数: 6
6
【 以下文字转载自 Programming 讨论区 】
发信人: youngsun (Kinn), 信区: Programming
标 题: 讨厌的WARNING: 在 MANAGED C++ 中写东东
发信站: BBS 未名空间站 (Sat Aug 27 18:19:58 2005)
HI,
想要在MANAGED EXTENTION C++ 在 。NET 里做 (有大量历史C++ FILES 了) ,
(JIU SHI managed C++ using .NET framework class)
头大:
我有一个 的数组 myring, 想写到 BINARY 文件里。
...
unsigned char *myring = new unsigned char (1000);
....
FileStream *file = ...;
BinaryWriter *bw = ...;
bw->Write(myring); --->出错误了 warning C4800: unsigned char *: forcing value
to bool 'true' or 'false'
i**p
发帖数: 902
7
【 以下文字转载自 Programming 讨论区 】
发信人: isup (No), 信区: Programming
标 题: 哪位用过tty_flip_buffer_push()?
发信站: BBS 未名空间站 (Sun Jun 15 17:43:46 2014, 美东)
I am trying to run tiny_tty in LDD3. When I use "cat /dev/ttty0" to read
from it, there is no output and the command is blocked.
Checking the trace, I notice both tty_insert_flip_char() and tty_flip_buffer
_push() are called. However, the data is not sent to the user by tty core.
Instead, it is sent back to the tiny_tty driver's tiny_write() callback
functio... 阅读全帖
c****s
发帖数: 37
8
来自主题: Programming版 - [转载] Re: 问个土问题吧
【 以下文字转载自 shopping 讨论区 】
【 原文由 campos 所发表 】
用位运算吧
unsigned char x;
for(i=0;i<8;i++)
{
if(inputstring[i]=='1')
x |= (unsigned char)(2^(7-i));
else
x &= (unsigned char)(255-2^(7-i));
}
inputstring 是那个输入的8位二进制字符串
q***z
发帖数: 934
9
来自主题: Programming版 - One network C question
Hello I am a newbie on network programming.
I am trying to receive a packet
if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct
sockaddr*)®ister_addr, &addr_len))==-1){
fprintf(stderr, "error in recvfrom.\n");
exit(1);
}
The packet I am receiving has the following possible structure.
typedef struct struct_CN
{
unsigned char magicA;
unsigned char magicB;
unsigned short msgLen;
} CN;
typedef struct struct_Cc
{
CN msgHeader;
uns
h****e
发帖数: 2125
10
来自主题: Programming版 - template metaprogramming 的问题
大家都知道STL里面有个valarray,definition是
template
class valarray
{
...
}
有人说有个更efficient的definition就是
template
class valarray
{
static const unsigned int count = N;
...
}
说因为array size在compile time就知道了,所以可以使用例如
valarray a = { 1, 2, 3 };
我比较confused的是,如果程序中都是valarry这类的东东,是比
valarray a(3);要efficient。但是code如果都是象
void func(const unsigned int size)
{
valarray a;
...
}
这样的,有何efficiency可言呢?这只是一个小例子来反映我
对template metaprogramming的很
c*****t
发帖数: 1879
11
来自主题: Programming版 - 来来来,我也问个题 (转载)
Here is the general version to get integer value of base 2 log(v).
In your case, you don't need the last minus part since that case
never occurs.
int getPos (unsigned int v)
{
float f;
if (v == 0) /* special case for 0 */
return -1;
f = v;
return (*((unsigned int *)&f) >> 23) - 127 - (((unsigned int)f & v) == 0);
}
z***e
发帖数: 5393
12
来自主题: Programming版 - 急问:这个为什么不行?
void *t=malloc(100);
unsigned int* p=(unsigned int*)((unsigned int)t+9);
//int k=300;
//memcpy(p,&k,4);
*p=1;
直接对 *p赋值就出错,用memcpy就可以,why??
linux下run的。
p****o
发帖数: 1340
13
来自主题: Programming版 - ambiguous operators in c++
in my code, T* is an abstract opaque pointer due to the system design.
while [] is an operator for accessing the real data. so they both make
sense somehow.
now the problem is solved. i declare the operator [] as
double& operator [] (int);
originally i used "unsigned int" for the most strict type checking. but in
practice, the compiler treated all constants like 5 as int but not unsigned.
so there is a conversion from int to unsigned. that's why the compiler does
not know which decoding path to
i***h
发帖数: 12655
14
来自主题: Programming版 - question about shift
下面的程序我期待 c=129
为什么高位会是ff?
#include
using namespace std;
int
main()
{
cout << (unsigned short)(2>>1 | 1<<7) << endl;
char c = 2>>1 | 1<<7;
cout << "c = " << (unsigned short)c << endl;
cout << "c = " << hex << (unsigned short)c << endl;
}
>>> output:
129
c = 65409
c = ff81
t****t
发帖数: 6806
15
来自主题: Programming版 - question about shift
by default, char is signed or unsigned is implementation-defined...i've seen
unsigned "char" as well.
that said, char, signed char, unsigned char are *3* different types. don't
make any assumptions.
b***y
发帖数: 2799
16
☆─────────────────────────────────────☆
gladioli (gladioli) 于 (Wed Sep 14 10:56:23 2005) 提到:
这是个奇怪的问题,我用的其实是标准C,在linux下运行正常,可是拿到windows上的
visual c++上运行问题就出来了。首先我是用unsigned char和fread来读二进制图形文件
的的,一个8位二进制算做一个unsigned char, 读了几行后就开始乱码,应该是内容读错
了。查了一下,sizeof(unsigned char)=1,不明白为什么会出错。
还有用fwrite写图形文件,有的时候也会出错。好像每一行都会错几格。可是如果将写入
值限制在25以上,好像错误就会少一些。
非常的困惑。有没有有经验的指点一下?多谢!
☆─────────────────────────────────────☆
rebatezhq (test) 于 (Wed Sep 14 19:48:06 2005) 提到:
fopen()的时候用binary模式,如"rb", "wb"
m******s
发帖数: 204
17
来自主题: Programming版 - 请教一个系统设计问题
设计一个字符队列管理系统:总的字符队列数目不定,每个队列长度不定。预先不知道
最大有几个队列
界面要求:
Q * create_queue(); //Creates a FIFO byte queue, returning a handle to it.
void destroy_queue(Q * q); //Destroy an earlier created byte queue.
void enqueue_byte(Q * q, unsigned char b); //Adds a new byte to a queue.
unsigned char dequeue_byte(Q * q); //Pops the next byte off the FIFO queue.
1。 不准使用任何动态内存分配函数。所有的操作只能在给定的数组中进行:
unsigned char data[2048];
2。 注重内存的有效使用,大约15个队列,每个可能含有80个字符。有可能队列的数目
更多,每个队列里的字符较少, 或者队列更少, 而每个队列里的字符更多些
3。 对速度的要求: 当增加或
f********f
发帖数: 475
18
来自主题: Programming版 - Need help on C++ code
另外, main() function是VC++/CLI编的, 需要调用encrypt(...)和decrypt(...), 但
传入的变量类型是array^, 怎么将它变成传统的UINT16*呢? UINT16被
typedef为unsigned short. unsigned char是8位, UINT16是16位的, 好象不能用(
UINT16*)直接cast.
X****r
发帖数: 3557
19
来自主题: Programming版 - Question about data member offset
You did it wrong when trying to print out these values of
pointer-to-member type. Try this way:
(assuming sizeof(float Point3d::*) == sizeof(unsigned long) here;
if not, change 'unsigned long' to appropriate integral type)
float Point3d::*p = &Point3d::x;
std::cout << "&Point3d::x = " << *(unsigned long*)&p << std::endl;
The reason why you got '1's if you try to print out
pointer-to-member values directly is because they can be
automatically convert to boolean (true for not null, false for
null)
d****e
发帖数: 251
20
来自主题: Programming版 - 这种function的写法是C的标准的么?
unsigned bi_reverse(code, len)
unsigned code; /* the value to invert */
int len; /* its bit length */
{
...
}
一,单独的unsigned是什么意思啊
二,参数分行
我平常没看到过这样的写法,这是从gnuzip里面看到的。
t****t
发帖数: 6806
21
func() will repeatly make x*=2 and x+=global_var.
if global_var is 1, then the final result should be (unsigned)-1.
if global_var changes to 2 in the middle, then the final result should be (
unsigned)-2. straightforward enough.
func1() will change the global_var to 2.
func() will start first. then after 3 sec, func1() will change global_var to
2. so the final result should be (unsigned)-2.
the length of loop is arbitrary, which makes my computer execute the loop
for more than 3 seconds.
now her
g*********s
发帖数: 1782
22
来自主题: Programming版 - binary number question
i guess cpu has two instructions. a common practice would be:
unsigned int x = -1; x >> 31; // compiler map to logic_shift
int x = -1; x >> 31; // compiler map to arithmetic_shift
for x86:
SAL Shift Arithmetically left (signed shift left)
SAR Shift Arithmetically right (signed shift right)
SHL Shift left (unsigned shift left)
SHR Shift right (unsigned shift right)
but i don't know why SAL is needed. possibly put sign on the right-most
bit?
c standard is undefined, i think.
g*********s
发帖数: 1782
23
来自主题: Programming版 - how to statically initialze a mutex in class?
the following code gives me warnings. what does it mean and does it matter?
inclass_mutex.cpp: In constructor ‘X::X(unsigned int)’:
inclass_mutex.cpp:8: warning: extended initializer lists only available with
-std=c++0x or -std=gnu++0x
inclass_mutex.cpp:8: warning: extended initializer lists only available with
-std=c++0x or -std=gnu++0x
#include
class X {
public:
X(unsigned int in_sz = 0): sz (in_sz)
{
buffer = new int[sz];
lock = PTHREAD_MUTEX_INITIALIZER;
... 阅读全帖
s****n
发帖数: 700
24
来自主题: Programming版 - a question about bitwise operation
那看看这个code
#include
using namespace std;
int main() {
int i;
unsigned int ui;
long l;
unsigned long ul;
long long ll;
unsigned long long ull;
i = 1 << (sizeof(i) * 8 - 1);
cout << "sizeof(i) is " << sizeof(i) << endl;
cout << "i is " << i < ui = 1 << (sizeof(ui) * 8 - 1);
cout << "sizeof(ui) is " << sizeof(ui) << endl;
cout << "ui is " << ui < l = 1 << (sizeof(l) * 8 - 1);
cout << "sizeof(l) is " << sizeof(l) << endl;
cout << "l is " << l < ul = 1 << (sizeof(ul) * 8 -... 阅读全帖
i**********e
发帖数: 1145
25
Try this:
It will use the high resolution performance clock if available (for both
windows/*nix platform), otherwise it will roll back to the non-high
resolution timer.
/**
* Timer class definition file.
*
* Provides basic timer functionality which calculates running time for
certain
* part of your program. Very useful for analyzing algorithm running time.
* After a Timer object is created, calling the Start() member function
starts
* the timer running. The Stop() member function stops the timer... 阅读全帖
k****t
发帖数: 2288
26
来自主题: Programming版 - 请教怎么用#define实现如下的功能
呵呵,你这个很强。
不过我很好奇的就是这个define会占instruction code的size吗?
我的想法很简单:就是简单,不容易出错,代码的size不增加。。。
比如
#include 。。。。
#define transfer(x) 。。。。
const unsigned char StrA1[] = {0xb,0xc,transfer("ABC")};
const unsigned char StrA2[] = {0xd,0xe,transfer("abcdedfg");
const unsigned char StrA3[] = {0xf,0x10,transfer("test signal");
....
m**o
发帖数: 5261
27
来自主题: Programming版 - c++ print endl
I am not very familiar with c++. I want to print out an integer matrix line
by line. Why the cout< void Matrix::printMatrix(){
for(unsigned int i=0; i {
Matrix::printRow(i);
}
}
void Matrix::printRow(unsigned int rownumber)
{
matrix_row_t row=grid.at(rownumber);
for(unsigned int i=0; i < row.size(); i++)
{
cout< }
cout< }
c*******y
发帖数: 1630
28
来自主题: Programming版 - 问个multiple core的问题
void do_sim(vector &rnumber, size_t run_bg, run_ed){
for(size_t i=run_bg;i!=run_ed;++i){
rnumber.at(i) = rand();
}
trailnum = 100;
vector rnumber(100, 0.);
numOfThreads = boost::thread::hardware_concurrency();
unsigned int perTrail = trailnum/numOfThreads;
unsigned int run_bg, run_ed = 0;
for(unsigned int i = 0; i!=numOfThreads-1; ++i){
run_bg = i*perTrail;
run_ed = (i+1)*perTrail;
threads[i] = boost::thread(do_sim, boost::ref(rnumber), run_bg, run_ed);
}
ru... 阅读全帖
O*******d
发帖数: 20343
29
来自主题: Programming版 - recurvion真的很难懂~~
我用的就是在现在的C++里用recursion. 极度简化后就是下边的思路
template
class Fool
{
public:
Fool() { data = N; }
unsigned int data;

Fool nextLevel; // recursively create next level.
void printData() { cout << data; nextLevel.printData(); }
};
// Specialize to terminate recursion
template<>
class Fool<0U>
{
public:
Fool() { data = 0U; }
unsigned int data;
void printData() { cout << data; }
};

Fool<9U> fool;
fool.printData(); // print 987654321... 阅读全帖
S*A
发帖数: 7142
30
我的机器没有Wei 的好,
但是相对结果还是可以看出来的。
原来的测试程序是严格顺序向后找, 虽然看上去
跳了 40 byte, 但是仍然小于 64 byte 的cache line
size. 但是从访问内存的次序来看是严格增加的。
这个是 cache 最爽的状态。
我改变了输入的次序,变成随机定票段,从输入数组
里面进来,其他的不变。构造输入数组的时间是刨去的。
程序里面 USE_RAND = 1 vs 0
仅仅变的是订票的次序,结果呢?
速度差了整整 4 倍。从 28M 暴减少到7M.当然实际订票
更接近与随机分布。谁排好了一个接一个定。
这就是为什么我会担心 IO 是瓶颈的问题。外部进来的数据,
全部都是 cache miss。这些纯计算的模拟还不能很好的代表真实
的负载情况。
程序如下:
比较快的机器建议增加 ITER 大小,使得总共运行时间超过几十秒。
#include
#include
#include
#define SEGMENTS 10000000
#define INPUTS 1000... 阅读全帖
i**p
发帖数: 902
31
来自主题: Programming版 - 哪位用过tty_flip_buffer_push()?
I am trying to run tiny_tty in LDD3. When I use "cat /dev/ttty0" to read
from it, there is no output and the command is blocked.
Checking the trace, I notice both tty_insert_flip_char() and tty_flip_buffer
_push() are called. However, the data is not sent to the user by tty core.
Instead, it is sent back to the tiny_tty driver's tiny_write() callback
function. What is wrong there?
The kernel version is 2.6.32-61-generic.
static void tiny_timer(unsigned long timer_data)
{
struct tiny_serial *... 阅读全帖
x******a
发帖数: 6336
32
来自主题: Programming版 - 请教(C++)
I defined a random number generator class norm, I would like to define a std
::vector of norm. However it did not work.
I got "Debug Assertation Failed" after the first of for loop.
Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse).
Any input are appreciated. Thanks!
code:
unsigned long seeds[]={123, 345, 356};
std::vector myseeds(std::begin(seeds), std::end(seeds));
std::vector mynorm;
for(auto it=myseeds.begin(); it!=myseeds.end(); ++it)
{
mynorm.push_back(norm(*it));
... 阅读全帖
w******w
发帖数: 126
33
来自主题: Programming版 - 请教(C++)
...
unsigned long seeds[]={123, 345, 356};
std::vector myseeds(std::begin(seeds), std::end(seeds));
std::vector mynorm;
for(auto it=myseeds.begin(); it!=myseeds.end(); ++it)
{
mynorm.push_back(norm(*it));
}
你把一个 unsigned long 的vector 往 norm的vector 里面去塞,C++ 会不高兴的 我
想 ^_^ 难道让编译器去隐士转换? :-)
r*****8
发帖数: 2560
34
C语言,结构体转字符串。简单的难题
结构体转字符串怎么做?我有一个野外站点,通过铱星发回来数据,10个字符1分
钱。为了降低费用,要压缩字符数量。结构体的位域(bit fields)很理想,以下例子
3个数据只要2个字符就够了。
结构体做好以后,要用字符传送(short burst message),怎么把结构体变成字
符串?我用的是个笨办法,把结构体写入一个文件。然后把文件的字符串读出来。
各位大侠有更好的方法吗?
如果有时间细看以下是程序。
// #############################################
#include // Standard input output.
#include // Standard library.
#include // String handling
#include /* POSIX terminal co... 阅读全帖
d****i
发帖数: 4809
35
Using a file to convert back and forth is a waste. You can simply use a
pointer to your struct data to do the transfer. But bear in mind that the
way you wrote may cause issue mainly due to two reasons:
1. Compiler can do some padding to struct
2. Endianess issue (big/little endian)
So in order to make it correct during network transmission, you first want
to make sure that compiler padding is removed, so you can use #pramga
directive:
#pragma pack(1)
struct packed_struct {
unsigned int f1:8... 阅读全帖
A*V
发帖数: 3528
36
来自主题: _OurFantasia版 - The philippine islands 1493-1898
这破系统不能自动排版, 只好凑合看了.
Volume XVIII, 1617–1620
Contents of Volume XVIII
Preface 9
Documents of 1617–1618
Letter to Felipe III. Andrés de Alcaraz; Manila, August 10, 1617. 31
Trade between Nueva España and the Far East. [Unsigned and undated; ca.
1617]. 57
Events in the Filipinas Islands, 1617–18. [Unsigned; Manila], June, 1618.
65
Description of the Philippinas Islands. [Unsigned]; Manila, 1618. 93
Dutch factories and posts in the Orient. [Pedro de Heredia]; [1618?]. 107
Memorial regarding Man
j**l
发帖数: 2911
37
说明一下,那个在O(logN)时间计算矩阵的N次方的方法是从如下的计算整数a的N次方的
方法推广而来
unsigned long long power(int a, int n)
{
unsigned long long result =1;
int p = a;
while (n)
{
if (n & 1)
result *= p;
n >>= 1;
p *= p;
}
return result;
}
a****s
发帖数: 559
38
来自主题: JobHunting版 - 问一个关于xor的题
1.先把这n个unsigned integers,每个都取位反,得到n个新的unsigned integers.
2.用原来的n个旧数生成小尾羊说的二叉树。
3.把新的n个数也放入2中二叉树。如果加入某个新数时其位置已经被某旧数占据,则该
新数取反前的旧数和占据该位置的旧数之XOR为0xFFFFFFFF,最大。
4.如果没有任何新数和旧数走到同一位置,查看旧数和新数的上一层的父节点。如果出
现某新数和某旧数有同一父节点,则该新数取反前的旧数和同父的旧数之XOR为
0xFFFFFFFE.
5.如果还没有,再往上找同父节点,以此类推。
真正在编程时,可以在插入新数时就一直保持一个有同父节点的最深的新旧数对(或多
个,有可能多解)。这样新数插入完,就有了结果,不需再用4.5.的办法向上回朔。
b****r
发帖数: 1272
39
来自主题: JobHunting版 - how to reverse the bits of an integer?
unsigned int s = sizeof(v) * CHAR_BIT; // bit size; must be power of 2
unsigned int mask = ~0;
while ((s >>= 1) > 0)
{
mask ^= (mask << s);
v = ((v >> s) & mask) | ((v << s) & ~mask);
}
from Bit Twiddling Hacks
m******s
发帖数: 204
40
来自主题: JobHunting版 - 请教一个系统设计问题 (转载)
【 以下文字转载自 Programming 讨论区 】
发信人: metricss (metr), 信区: Programming
标 题: 请教一个系统设计问题
发信站: BBS 未名空间站 (Fri Feb 26 04:15:12 2010, 美东)
设计一个字符队列管理系统:总的字符队列数目不定,每个队列长度不定。预先不知道
最大有几个队列
界面要求:
Q * create_queue(); //Creates a FIFO byte queue, returning a handle to it.
void destroy_queue(Q * q); //Destroy an earlier created byte queue.
void enqueue_byte(Q * q, unsigned char b); //Adds a new byte to a queue.
unsigned char dequeue_byte(Q * q); //Pops the next byte off the FIFO queue.
1。 不准使用任何动态内存分配函数。所有的操作只能在
b******v
发帖数: 1493
41
unsigned int t = 0;
for(unsigned int i=0; i<32; i++) {
t += (n>>i)&1;
}
cout << "The number " << n << " has " << t << " one's" << endl;
b******v
发帖数: 1493
42
来自主题: JobHunting版 - 请做一道简单题(附感想)
unsigned int countWords(char* a)
{
unsigned int t=0, i;
bool flag=false; /* whether the previous char is space */
for(i=0;a[i]!='\0';i++) {
if(a[i]==' ') {
if(flag==false)
t++;
flag = true;
}
if(a[i]!=' ') {
flag = false;
}
}
if(a[0] == ' ')
return t;
else
return t+1;
}
b******v
发帖数: 1493
43
来自主题: JobHunting版 - 请做一道简单题(附感想)
修改后的程序
unsigned int countWords(char* a)
{
unsigned int t=0, i;
char pre = a[0];
for(i=0;a[i]!='\0';i++) {
if(a[i]==' ') {
if(pre!=' ')
t++;
}
pre = a[i];
}
if(pre == ' ') /* the last char is a space */
return t;
else
return t+1;
}
b******v
发帖数: 1493
44
来自主题: JobHunting版 - 请做一道简单题(附感想)
其实原来用flag的程序修改一下也能用
主要改动是把flag初始为true,而不是false
这样就能处理初始字符为空格的情况
并且最后判断最后一个字符是否为空格,
这样就能处理最后字符是空格的情况
由于每次只用写1bit,估计比用pre字符的会稍微快一点
unsigned int countWords(char* a)
{
unsigned int t=0, i;
bool flag=true; /* whether the previous char is space */
for(i=0;a[i]!='\0';i++) {
if(a[i]==' ') {
if(flag==false)
t++;
flag = true;
}
if(a[i]!=' ') {
flag = false;
}
}
if(flag == true) /* the last char is space */
return t;
else
return t+1;
}
t***e
发帖数: 446
45
来自主题: JobHunting版 - 请做一道简单题(附感想)
what about this?
#include
#include
using namespace std;
unsigned int CountWord(const char* ch)
{
unsigned int cnt=0;
bool flag=true; /*check for consecutive spaces*/
if (ch==NULL)
return cnt;
for (size_t i=0; ch[i]!='\0'; ++i)
{
if (ch[i]==' ' && flag==false)
{++cnt;flag=true;}
if (ch[i]!=' ')
flag=false;
}
if (flag==false)
++cnt;
return cnt;
}

int main()
{
cout<
d****n
发帖数: 233
46
来自主题: JobHunting版 - Non-recursive permutation
// test1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
void PrintArray(int *A, unsigned int N)
{
for (unsigned int i = 0; i < N; i++)
{
printf("%d ", A[i]);
}
printf("\r\n");
}
// This method print out an array iteratively.
// The idea behind this is:
// 1. Suppose we have a way to print out permutation for A[n-1].
// 2. For every permutation, insert the nth element between any of
// the n-1 elements will construct a new permuation
// F
d****n
发帖数: 233
47
来自主题: JobHunting版 - Non-recursive permutation
void IterativePrintPermutation2(int *A, unsigned int N)
{
int *controller = new int[N];
for (unsigned int i = 0; i < N; i++)
controller[i] = i;

while (1)
{
PrintArray(A, N);

int i = -1;
while (++i < N)
{
if (controller[i] == 0)
{
controller[i] = i;
Swap(A, i, 0);
}
else
{
Swap(A, i, controller[i]);
Swap(A,
l******t
发帖数: 12659
48
来自主题: JobHunting版 - size_type是干嘛用的?
因为ivect.size()是unsigned的;
size_type:
Type defined by the string and vector classes that is capable of containing
the size of any string or vector, respectively. Library classes that define
size_type define it as an unsigned type.
B*****t
发帖数: 335
49
来自主题: JobHunting版 - 离奇的Amzaon第一轮电面
建议您把unsigned和signed的在计算机内的存储以及各种在unsigned和signed上的运
算好好看看
j**l
发帖数: 2911
50
来自主题: JobHunting版 - 离奇的Amzaon第一轮电面
我的理解,假定int是4位二进制,
则signed int 的取值范围是从-8到+7,最高位是符号位并采用补码表示法,而
unsigned int的取值范围是0到+15
如果i = 1000是signed int变量,则它表示-8
i--, 计算机就是机械的给它加上1111, 从而得到0111, 也就是+7
-8 - 1的算术运算发生溢出,你得不到-9这个你要的结果(它根本不在-8到+7的表示范
围内), 而只能得到+7
如果i = 1000是unsigned int变量,则它表示+8
i--, 计算机同样机械的给它加上1111, 从而得到0111, 也就是+7
+8 - 1的算术运算没有发生溢出,你得到的+7(它在0到+15的表示范围内)正是你要的结果
首页 上页 1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)