c****s 发帖数: 241 | 1 思想大概是这样:不过这个程序我没有编译它,也没有处理溢出
unsigned int add(unsigned int a, unsigned int b){
if( b == 0 ) return a;
unsigned int carry = a & b;
carry <<= 1;
return add(a^b, carry);
}
|
|
i********s 发帖数: 133 | 2 the code for the 1st problem. assume A[0] <= B[0].
int findK(int *A, int *B,
unsigned int sizeA, unsigned int sizeB,
unsigned k)
{
if (sizeA == 0)
{
if (k < sizeB)
return B[k-1];
else
throw std::exception();
}
if (sizeB == 0)
{
if (k
return A[k-1];
else
throw std::exception();
}
unsigned int index1, index2;
index1 = std::min(k-1, sizeA-1);
index2 = 0;
|
|
l*******o 发帖数: 791 | 3 这样做行么?
unsigned short int rand_7()
{
bool high,middle,low;
high=rand5()>3?1:0;
middle=rand5()>3?1:0;
low=rand5()>3?1:0;
unsigned short int result=high*4+middle*2+low;
return result;
}
unsigned short int rand7()
{
unsigned short int res=rand_7();
while(!res)
{
res=rand_7();
}
return res;
} |
|
f****g 发帖数: 313 | 4 The following is my code :S
#include
#include
#include
#define MAXLEN 5
char* countCharInStr(const char* s,
unsigned int len)
{
char *pFast, *pSlow;
unsigned char num[MAXLEN] = {0};
unsigned int count = 0;
unsigned int resC = 0;
if( 0 == len || NULL == s)
{
return NULL;
}
char *res = (char*)malloc(sizeof(char)*len);
if( NULL == res)
{
return NULL;
}
pSlow = s;
pFa... 阅读全帖 |
|
o*******p 发帖数: 722 | 5 in C++:
#include
#include |
|
b**********2 发帖数: 1923 | 6 3. Write a C++ program that would find and print the first longest
ascending or descending subsequence for a vector of integers. For example,
given a vector with
4, 2, 1, 2, 3, 4, 3, 5, 1, 2, 4, 6, 5
the program would find the underlined subsequence and print it.
void printLongestConsecAsending (vector& seq) {
unsigned int i=0;
unsigned int best_start = 0, best_length=1;
unsigned int current_start = 0, current_length=1;
while (i < seq.size()) {
if (seq[i++] > s... 阅读全帖 |
|
w****o 发帖数: 2260 | 7 就这个话题,想问点儿东西
到底 unsigned int在CPU里面是如何表示的?我觉得通常数据都是存在寄存器(
register)进行运算的。可是寄存器是不区分 signed 还是unsigned的吧?!
我觉得unsigned int这个只是编程语言和编译器弄出来的,不是CPU native support的
,到底编译器是如何区分singed和unsigned的?
是不是要写个代码,到gdb里看汇编来弄明白?!
谢谢! |
|
c*i 发帖数: 348 | 8 To be completed in any language
Write a function called enlargeImage which takes as parameters an array of
unsigned 8-bit integers, a width, a height and a scaling ratio. The function
should return an array of unsigned 8-bit integers. In C, the function
signature would look as follows:
unsigned char *enlargeImage(const unsigned char *pixels, const int width,
const int height, const float scalingRatio);
The input and output arrays contain the pixel values for the Y plane of a
YUV image.
The funct... 阅读全帖 |
|
w****x 发帖数: 2483 | 9 自己想的解法, 一般不能通过现在的number > 之前的number来判断.
, 如果用int统一处理的话可能不能处理INT_MIN的情况, 因为负数范围比正数大一个.
用unsigned int来处理也不能通过判断现在的比值前的数大来决定是否溢出, 并且
unsigned int自己可能溢出.
溢出判断分两阶段, 一个是在增加新digit前作溢出判断, 这个是判断unsigned int自
己不溢出.
if (uRes > UINT_MAX/10 || (uRes == UINT_MAX/10 && nDigit > (UINT_MAX - (UINT
_MAX/10)*10)))
throw CException("Over flow detected");
在unsigned int自己不溢出的情况下判断是否转换成int后会溢出
//update uRes
uRes = uRes*10 + nDigit;
//uLimit = bNeg ? 0x7FFFFFFF + 1 : 0x7FFFFFFF;
//Overflow situation if uRes... 阅读全帖 |
|
f**********t 发帖数: 1001 | 10 unsigned MinJump(vector jumps) {
unsigned res = UINT_MAX;
vector minJumps(jumps.size(), UINT_MAX);
if (jumps.empty()) {
return res;
}
minJumps[0] = 0;
for (size_t i = 0; i < jumps.size(); ++i) {
if (jumps[i] == 0 || minJumps[i] >= res) {
continue;
}
if (i + jumps[i] >= jumps.size()) {
res = min(res, minJumps[i] + 1);
continue;
}
for (size_t k = jumps[i]; k != 0; --k) {
minJumps[i + k] = min(minJumps[i + k], minJumps[... 阅读全帖 |
|
s**x 发帖数: 7506 | 11 I believe leetcode provide a simple solution which may overflow, but
actually it is correct.
You can simply use unsigned int. I would think even an int would work as
well.
bool isPalindrome(int num) {
if(num < 0) return false;
unsigned int oldNum = num;
unsigned int rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
return rev == oldNum);
}
The following is from gnu comments.
13.2.1 Basics of Integer Overflow
In languages like C, unsigned integer overflow ... 阅读全帖 |
|
l*x 发帖数: 14021 | 12 Feb 10: you are here
----------------------------------
Feb 17: Teams may designate one Franchise/Transition Player
Feb 19-25: NFL Scouting Combine, Lucas Oil Stadium, Indianapolis, IN
Mar 3: Deadline for designating one Franchise/Transition player (3:00 PM CST)
Mar 8-11: Clubs are permitted to enter into contract negotiations with
certified agents of players who will be Unrestricted Free Agents at the end
of the current League Year.
--------------------------------
Mar 11: 2014 League Year star... 阅读全帖 |
|
q***z 发帖数: 934 | 13 Here is my program
the head is
typedef struct struct_CN
{
unsigned char magicA;
unsigned char magicB;
unsigned short msgLen;
} CN;
typedef struct struct_CcDev
{
CN Header;
unsigned short action;
} CcDev;
CcDev CcDev_packet;
Evaluate_CN(CN *CN_p)
{
strcpy(&CN_p->magicA,"A");
printf("magicA is %s\n", &CN_p->magicA);
strcpy(&CN_p->magicB,"H");
printf("magicB is %s\n", &CN_p->magicB);
CN_p->msgLen = 1234;
printf("msgLen is %d\n", &CN_p->msgLen);
}
void |
|
w****h 发帖数: 212 | 14 运行时出线如3452816845 3452816845 3452816845这种奇怪的大数字
以下我本来用的是int,也有类似的错误。不知道哪里来的。
#define C 10000 //define a fixed maximum capacity for the list that can
be taken away of knapsack
#define N 200 //define number of items for the list
struct body { //define a struct with three parts, the item name, size and
value
unsigned int name;
unsigned int size;
unsigned int value;
};
typedef struct body Plist;
void disp(Plist*, unsigned int);
int _tmain(int argc, _TCHAR* argv[])
{
srand((un |
|
l*****d 发帖数: 359 | 15 Conversion rules are more complicated when unsigned operands are involved.
The problem is that comparisons between signed and unsigned values are
machine-dependent, because they depend on the sizes of the various integer
types. For example, suppose that int is 16 bits and long is 32 bits. Then -
1L < 1U, because 1U, which is an unsigned int, is promoted to a signed long.
But -1L > 1UL because -1L is promoted to unsigned long and thus appears to
be a large positive number. |
|
b***y 发帖数: 2799 | 16 ☆─────────────────────────────────────☆
youngsun (Kinn) 于 (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'
这咋回事呀? 那写的参数不就是 unsigned * in C++/Byte[] in C#?
能人给说 |
|
J*******i 发帖数: 2162 | 17 代码如下
#include
#include
struct Port {
char name[8];
};
struct Node {
char name[8];
uint32_t num_of_ports;
struct Port ports[0];
};
int main() {
struct Node x;
printf("size=%u, ptr(x)=%u, ports=%u, ptr(ports)=%u, "
"ptr(ports[0])=%u\n", sizeof(struct Node), (unsigned int)&x,
(unsigned int)x.ports, (unsigned int)&(x.ports),
(unsigned int)&(x.ports[0]));
return 0;
}
在这个Node里面有一个struct Port ports[0]的结构,用来动态的表示可变长度的一个
数组
我能够理解他指向的实际地址应该是&x+ |
|
X****r 发帖数: 3557 | 18 One possibility is that j is signed but u is unsigned.
When a binary operator has both signed and unsigned operands,
the signed integer is converted to unsigned according to
"usual arithmetic conversions". Thus the result of expression
-1 < 1u is false, while -1 < 1 is true. VC should have given
you a warning about mixing signed and unsigned, which you probably
have ignored. |
|
t****t 发帖数: 6806 | 19 sizeof() has unsigned type. as a result of [usual arithmetic conversions] (
standard 5, clause 9), in "i-DELTA", int is converted to unsigned; then "i-
DELTA>=0", 0 is converted to unsigned. thus the condition is always true,
since by definition, any unsigned number is >=0. |
|
c***r 发帖数: 4631 | 20 俺系业余爱好的,上来玩玩。
1,
int IsSecretPassword(char *ptrstring)
{
if( null == ptrstring ) return 0;
int n = strlen(ptrstring);
if( n > 0)
{
char * temp = malloc(n+1);
int r;
strcpy(temp, ptrstring);
for (int i =0; i
{
temp[i] = toupper(temp[i]);
}
if (strcmp(temp, "TEST")==0)
{
r=1;
}
else
{
r = 0;
}
free(temp);
return r;
}
return 0;
}
2,
int* MakeArray(u... 阅读全帖 |
|
l*****r 发帖数: 15615 | 21 in a communication task, the python script received the data buff as a list
of bytes.
since python tool was used to do the communication with embedded software,
which were written in C/C++;
so the message were definded in c as type of structure, for example
struct
{
unsigned long variable1;
unsigned char variable2;
unsigned char variable3[4];
...
}MessageType
in stead of writing a function in python to decode the message buffer byte
by byte: forming four bytes into unsigned long/int; and etc.
i... 阅读全帖 |
|
d****n 发帖数: 1241 | 22 undefined behavior可能是C里边最"邪恶"的一个东西了。。。
听到过一个很有意思的undefined behavior是关于整数溢出的,
signed integer overflow是一个常见的undefined behavior.
但是unsigned integer在某些情况下也会导致undefined behavior,
比如两个unsigned short相加,如果ABI规定sizeof(unsigned short)是16,
sizeof(int)是32, 那么C标准里的integer promotion规则会先把
unsigned short变成signed int, 然后就存在integer overflow的可能性了。呵呵 |
|
T********i 发帖数: 2416 | 23 我说过,我还保留单线程处理订票请求的选项。
今天写了一个小程序进行测试。
假设:
路段有1000万个。也就是全国1000万个车站
当然,全国不可能有1000万个车站。但是可以按照路段/车次划分。
也就是全国几个月的车票可以放进去
每趟车不超过56635个座位,用ushort表达。
内存规划:
一共大约20M数据,全部fit进L3 cache。
假定:
还是一次请求20个路段。
初始化:
1000万路段填满,一共超过320亿张票。不知道够不够今后10年的总运量?
访问模式:
尽量避免L1和L2 cache hit。每个循环每路段逐一访问,最大可能造成L1,L2 miss。
结果:
如下程序:
循环:5000 × 10000000 / 20 = 2500000000
用时:49.86s
2500000000 / 49.86 = 50.14M TPS。
程序如下:
unsigned short data[10000000];
bool reserveTicket(unsigned short *line, int start, int len) {
unsigned short *p = l... 阅读全帖 |
|
T**E 发帖数: 1892 | 24 各种族之间的jj差异并不大。亚洲人jj小是西方系统重复的谎言。
西方这么宣传其实就是为了防止白女外f.
Does average penis size really vary between the races? Or is that just a
myth? If it's true, it should be mentioned in the article. Voortle (talk) 18
:55, 27 December 2009 (UTC)
A quick search reveals this page http://answers.google.com/answers/threadview?id=366192 which quotes from a previous version of the main article. What happened to the quoted section? —Preceding unsigned comment added by 68.59.49.25 (talk) 19:21, 4 January 2010 (UTC)
Rel... 阅读全帖 |
|
w****t 发帖数: 33 | 25 Debug the following code - explain the problems you find. Optionally, provid
e an
improved version.
#include
#include
char *gText = 0;
unsigned int gSize = 0; // the length of gText, in bytes
int append(const char* s )
{
if( s && s[0] )
{
if( (! gText) || (gSize = 0) )
{
gText = (char *)s;
gSize = strlen(s);
}
else
{
unsigned int len = gSize;
unsigned int s_len = strlen( s );
gSize += s_len;
char* temp = new char[ gSize ];
memcpy(temp, gText, len);
memcpy(temp + len, s, s_le |
|
b****g 发帖数: 625 | 26 Suppose there is a platform where
sizeof(unsigned int) == 4
and the function app():
unsigned int app(unsigned int)
yields the following output:
app(0x80000000) == 1;
app(0x40000000) == 2;
app(3) == 0xc0000000;
app(0x70) == 0xe000000;
app(0x99999999) == 0x99999999
app(0xAAAAAAAA) == 0x55555555
app(app(x)) == x for all x.
Write a version of app() |
|
c*******d 发帖数: 255 | 27 36 = 2^2*3^2
两个2分给三个人,有C(4,2)种分法
C(4,2)*C(4,2) = 36种情况
所以程序可以这么写:
#include
#include
using namespace std;
int main()
{
unsigned int k1, k2, k3, s1, s2, s3, n1, n2, n3;
unsigned int count = 0;
unsigned int i, j, p, q;
for(i=1;i<=4;i++) {
for(j=i+1;j<=4; j++) {
k1 = i-1;
k2 = j-(i+1);
k3 = 4-j;
for(p=1; p<=4; p++) {
for(q=p+1; q<=4; q++) {
s1 = p-1;
s2 = q-(p+1);
s3 = 4-q;
n1 = pow(2,k1)*pow(3,s1 |
|
s********a 发帖数: 1447 | 28 请教一下 如何写一个struct
这个struct里面只有 4个项 每个占 1bit
我写了一个
struct {
unsigned char priority :1;
unsigned char nonpriority :1;
unsigned char empty :1;
unsigen char full :1;
}flag;
但是这个占了bit 因为是char
如何写这个struct只占4bit
谢谢 |
|
t******e 发帖数: 1293 | 29 测试了前面8个都没有问题, O(n)时间和空间,可以改为O(1)空间
#include
#include
#include |
|
w******1 发帖数: 520 | 30 什么是BITMAP, 怎么个结构啊?
看到很多人在讨论BITMAP, 可是GOOGLE BITMAP, 出来的结果都不是算法啊, 都是图
像处理的。
用128MB的内存做一个BitMap~
然后把原来的文件遍历一次,每读一个数都在上述BitMap中标记~按位取或预算~
索引就是这个数字的大小~结束后,扫描bitMap,找到没有标记的即可~
for(unsigned int i=0;i<0xffffffff;i++){ unsigned temp=next integer from
inputfile; bitmap[temp]=1;}for(unsigned int i=0;i<0xffffffff;i++){
if(bitmap[i]==0) output(i);} 4个月前 by rex.nani
关于代码格式化:把代码的那一部分部分全选,然后点击101010按钮 -
又:4Billion的内容,128M是不够的 -
@半瓶墨水: 128MByte*8=1Gbits=2^32 按位存的话是可以的吧~ -
@rex |
|
j**l 发帖数: 2911 | 31 用unsigned long是最好的
用signed long只是碰巧运行结果对而已
我们用4位的signed int例子说明,范围是-8到+7
这个+7怎么来呢?
如果不用unsigned, 1 << 3实际上是1000, 表示-8而不是+8
减1的结果,虽然正确得到+7 (1000 - 1 = 0111)
但背后的逻辑会让人困惑 -8 - 1 = +7?
用unsigned, 就是+8 - 1 = +7了 |
|
j**l 发帖数: 2911 | 32 我的看法是,对四位二进制整数,计算机才不care它是signed还是unsigned, 对减去1
的操作,总是机械的加上1111
以1010为例子,加上1111并丢弃任何最高位的进位得到1001
1. 假如1010是unsigned, 则它表示10,而1001表示9,等式为10 - 1 = 9, 正确
2. 假如1010是signed,则补码表示-6,而1001的补码表示-7,等式为-6 - 1 = -7, 同
样正确
这两种情形都没有溢出。9在区间[0, 15]内,而-7也在区间[-8, 7]内
以1000为例子,加上1111并丢弃任何最高位的进位得到0111
3. 假如1000是unsigned, 则它表示8,而0111表示7,等式为8 - 1 = 7, 正确
4. 假如1000是signed,则该补码表示-8,而0111表示7,等式为-8 - 1 = 7, 不正确
情形3没有溢出,而4溢出了。7在区间[0, 15]内,而-9已不在区间[-8, 7]内了 |
|
H****r 发帖数: 2801 | 33 有点笨,欢迎拍砖
1. 一个rotated的排序整数数组,比如A=[6,8,1,2,4,5],写code找一个给定元素
// Ranged search
// returns index or size if not found
bool FindInRotatedArray(const std::vector &rotatedArray,
int iVal,
unsigned int &index,
unsigned int leftIndex,
unsigned int rightIndex)
{
while (leftIndex < rightIndex)
{
if (rotatedArray.at(leftIndex)==iVal)
{
// found!
index = leftIn |
|
R*******e 发帖数: 36 | 34 这样看看:
unsigned int integerSquarRoot(unsigned int number)
{
unsigned int i= number/2;
while (i * i > number)
i /= 2;
while (i * i < number)
i += 1;
if (i * i == number)
return i;
else
return i-1;
} |
|
f****g 发帖数: 313 | 35 电话面试
1st:
1. 讨论我的博士研究项目
2. 如果SNMP agent不能获取数据,或者获取的数据不符合预期,如何诊断该问题?
3。我做过的最有挑战的项目是什么?
4。用邮件写代码,然后讨论我写的代码:
unsigned char * get(int sizeOfArray, int sizeOfRecord);
void release(unsigned char* ptr);
该函数可以实现:
unsigned char ** array = get(5, 10);
snprintf( array[0], 10, “hello world\n”);
snprintf( array[1], 10, “hello again\n”);
5。Java的基本概念
2nd
1。Apache的log file如何找访问量最大的网页 (用linux shell写个小script)
2。如果某网站访问量突然增加,可能是什么情况发生,如何确定各种情况(1。暂时的
Popularity激增 2. DDOS Attack 3. 网站添加新的内容)
3。Java基本概念+设计扑克牌的类
4。读re... 阅读全帖 |
|
c***2 发帖数: 838 | 36 /*
sizeOfArray: how many lines
sizeOfRecord: size of each line
*/
unsigned char **get(int sizeOfArray, int sizeOfRecord)
{
unsigned char **p;
int i;
p=(char**)malloc(sizeof(char*)*sizeOfArray);
for(i=0;i
p[i]=(char*)malloc((sizeOfRecord+1)*sizeof(char));
}
return p;
}
void release(unsigned char **ptr, int sizeOfArray)
{
int i;
for(i=0;i
free(ptr[i]);
}
free(ptr);
} |
|
c***2 发帖数: 838 | 37 There are at least two other ways:
1) int a[2];
unsigned addr0=&a[0];
unsigned addr1=&a[1];
size_t sizeofint=addr1-addr0;
2) typedef struct ab {
int a;
int b;
} AB;
sizeofint=offset(b in AB)
=(unsigned)(&((AB*)0->b))
|
|
J*********r 发帖数: 5921 | 38 I think what he actually meant is like below since he mentioned recursion.
unsigned add(unsigned a, unsigned b){
if(b==0)
return a;
return add(a^b, (a&b)<<1);
}
(a&b)<<1 stores the carries and will be ultimately left-shifted out of all
bits, leaving a 0, which is the base case here. |
|
w**z 发帖数: 8232 | 39 http://stackoverflow.com/questions/825221/where-can-i-find-the-
It's a math problem more than CS. Well, don't know how far you want to go..
That is the implementation from math lib
public static double sqrt(double a) {
return StrictMath.sqrt(a); // default impl. delegates to StrictMath
// Note that hardware sqrt instructions
// frequently can be directly used by JITs
// and should be much faster than doing
// Math.... 阅读全帖 |
|
w****x 发帖数: 2483 | 40 int myatoi(const char* p)
{
assert(p);
bool bNeg = false;
if ('-' == *p || '+' == *p)
{
bNeg = '-' == *p;
p++;
}
if (*p == 0) throw CException();
//use unsigned to deal with INT_MIN
unsigned int nAbsValue = 0;
while (*p != 0)
{
if (*p < '0' || *p > '9')
throw CException();
unsigned int nOrgValue = nAbsValue;
int nDigit = *p - '0';
nAbsValue = 10*nAbsValue + nDigit;
//deal with normal o... 阅读全帖 |
|
t****t 发帖数: 6806 | 41 quite the contrary, most CPU support unsigned and signed numbers. most
likely they share the binary representation, but instructions distinguish
them. for example, in x86 you have mul and imul for unsigned and signed
integer multiplication. for addition and subtraction, the operations are the
same anyway. but for comparison, CPU do check different flag.
so, the answer is no, most CPU natively support unsigned and signed integers. |
|
p*****o 发帖数: 1285 | 42 My solution:
long zero-count(unsigned array[], int length){
long ct = 0;
int digits=numeric_limits::digits;
for (int i=length-1; i >=0; i++) {
unsigned s = array[i];
for (int j=0; j
if (s%2 == 1) ct = 0;
else ++ct;
s>>1;
}
}
return ct;
} |
|
r*****e 发帖数: 792 | 43 it is correct because the equation is derived from digital logic which is
Gray code's origin.
Use K-Map on the truth table when the variables are not too many, otherwise
not easy to use
K-map even though it still works.
FYI, grayToBinary(unsigned int num)
unsigned int numBits = 8*sizeof(num); //here 8 means each byte has 8 bits
unsigned int shift;
for (shift=1; shift
num ^= num>>shift;
return num; |
|
w****x 发帖数: 2483 | 44 unsigned int GetOriginalNum(unsigned int num)
{
int nBack = 0;
int b = 0;
int a = 0;
unsigned int x = num;
while (x != 0)
{
int d = x%10;
if (d > 7)
nBack += pow((double)10, a) + (d-1)*b;
else nBack += d*b;
b = pow((double)10, a) + 9*b;
a++;
x /= 10;
}
return num - nBack;
} |
|
s***b 发帖数: 139 | 45 下面的程序如果用了注释部分,对于相等的key的处理就有错,如果不用注释部分,就
没错,不知道哪位大牛能解释下为什么啊?谢谢
Ex: 比如array = [2,1,9,4,4,56,90,3], target = 8,正确输出应该是4,5
如果定义hash table是用的是:table[numbers[i]] = i 那就能正确输出
如果定义用的是:table.insert(std::make_pair(numbers[i], i)); 那就
会输出4,4
对此十分不理解啊,不是同样的插入么?unordered_map是怎么处理key相等的状况的,
一般来说key不是unique的么?万分感谢啊!!!
class Solution {
public:
std::vector twoSum(std::vector &numbers, int target) {
std::vector result;
std::unordered_map table;
for(unsigned int i =... 阅读全帖 |
|
p*****d 发帖数: 126 | 46 错了,unsigned和singed的比较会被转换为unsigned.
我故意使用unsigned来作为绝对值差这样我的表示范围可以从0-2的32次方-1,而不是
负的2的31次方到正的2的31次方-1 |
|
f**********t 发帖数: 1001 | 47 unsigned MinJump(vector jumps) {
vector minJumps(jumps.size(), UINT_MAX);
if (jumps.empty()) {
return UINT_MAX;
}
minJumps[0] = 0;
if (jumps[0] >= jumps.size()) {
return 1;
}
for (size_t i = 0; i < jumps.size(); ++i) {
if (jumps[i] == 0) {
continue;
}
for (size_t k = jumps[i]; k != 0; --k) {
minJumps[i + k] = min(minJumps[i + k], minJumps[i] + 1);
if (jumps[i + k] + i + k >= jumps.size()) {
return minJumps[i + k] + 1... 阅读全帖 |
|
r**d 发帖数: 116 | 48 下面是apple的source code. 我不明白为什么需要把char 转换成unsigned char?
strcmp(const char *s1, const char *s2)
{
for ( ; *s1 == *s2; s1++, s2++)
if (*s1 == '\0')
return 0;
return ((*(unsigned char *)s1 < *(unsigned char *)s2) ? -1 : +1);
} |
|
L******k 发帖数: 395 | 49 Problem Description
-------------------
Your task is to write a command-line program to evaluate a set of
equations, each specified on separate lines. An equation is defined
by:
=
is the left-hand side of the equation and is always a variable
name. A variable name can only be composed of letters from the
alphabet (e.g. for which isalpha(c) is 1). is the right hand
side of the equation and can be composed of variables, unsigned
integers, and the + operator.
Here is one exa... 阅读全帖 |
|
C********s 发帖数: 7 | 50 准确的说,并不是编译器智能高了,而是不同的编译器在不同的平台上有自由的选择
char的实现的权利,所以不能对char做这个假设。
比如在我的机器上用GCC,你的这段代码就不work。
stackoverflow上最精确的一段描述:
The C standard does not specify if plain char is signed or unsigned.
In fact, the standard defines three distinct types: char, signed char, and
unsigned char. If you #include and then look at CHAR_MIN, you can
find out if plain char is signed or unsigned (if CHAR_MIN is less than 0 or
equal to 0), but even then, the three types are distinct as far as the
standard is conc... 阅读全帖 |
|