http://topic.csdn.net/t/20040916/13/3378595.html1将一个字符串逆序
2将一个链表(linked list)逆序
3写计算一个字节(byte)里有多少bit被置1的函数
4在一个字符串中找到可能的最长的子字符串并返回之,该字符串是由同一字符组成的
5字符串转换成整数 不用库函数
6整数转换成字符串 不用库函数
要求用纯C,时间复杂度和空间复杂度要好。
我先写一个1的:
char* revch(char *str)
{
char* ps = NULL;
char* p = str;
for(ps=str; *ps!='\0'; ++ps);
for(--ps; ps>p; --ps,++p){
*p^=*ps;
*ps^=*p;
*p^=*ps;
}
return str;
}
3
int GetNum( int Num )
{
int i = 0,LastNum = 0;
while(i<=7)
Num&(2^i) != 0 ? LastNum++ : LastNum;
}
瞎写写
3.
int GetNumofOne(unsigned char data)
{
int _res = 0;
while(data)
{
_res += (data & 1);
data >> = 1;
};
return _res;
}
5.
int StrToInt(char* s)
{
int i = 0, fac = 1;
if(*s = '-' || *s = '+')
{
fac = *s == '-' ? -1 : 1;
s++;
}//if
//不管溢出
while(*s >= '1' && *s <= '0') i = i * 10 + (*s++ - '0');
return fac * i;
}
3
int GetNum( unsigned int Num )
{
int nOne = 0;
while(Num)
{
nOne += (Num & 1);
Num >>= 1;
}
return nOne;
}
3
int GetNum(unsigned char Num )
{
return ((Num & 1)
+ (Num & 2)
+ (Num & 4)
+ (Num & 8)
+ (Num & 0x10)
+ (Num & 0x20)
+ (Num & 0x40)
+ (Num & 0x80));
}
5
int AtoI(const char* pData)
{
int flag = 1; // 正数
int result = 0;
if(pData == 0)
{
return flag * result;
}
// 正负符号
if(*pData == '-')
{
flag = -1;
pData++;
}
// 数值计算
while(*pData)
{
if((*pData < '0') || (*pData > '9'))
{
// 非法字符
break;
}
result *= 10;
result += (*pData - '0');
pData++;
}
// 返回结果
return flag * result;
}
No.2好象还没有人写啊
struct node{
int code;
LinkList *next;
}Node *LinkList/*是对链表中节点的定义*/
LinkList revers(LinkList head)
{
/*链表的初始也省略了,只写关键部分*/
head=(LinkList *)malloc(sizeof(LinkList));
/*用一个头指针指向这个链表*/
LinkList *p,*q,*t;
q=head->next;
while(q){
t=q->next;
q->next=p;
p=q;
q=t;
}/*while*/
head->next==p;
return head;
}
也没调试,大家一起来讨论吧
字符串转换成整数 标准C源代码:
int atoi(const char *str)
{
return ( (int)( strtol (str, (char **) NULL, 10) ) );
}/* int atoi(const char *str) */
long strtol( const char *str, char **endptr, int base )
{
unsigned long result;
unsigned long stop;
const char *cursor = str;
int i_char, sign = 0, track, threshold;
do
{
i_char = *cursor++;
} while ( isspace( i_char & 0xff ) );
if ( i_char == '-' )
{
sign = 1;
i_char = *cursor++;
} else if ( i_char == '+' )
{
i_char = *cursor++;
}/* end of if else */
if (( base == 0 || base == 16 ) && ( i_char == '0')
&& ( *cursor == 'x' || *cursor == 'X'))
{
i_char = cursor[1];
cursor += 2;
base = 16;
}/* end if */
if ( base == 0 )
base = ( ( i_char == '0' ) ? 8 : 10 );
stop = ( sign ? ( - ( unsigned long )LONG_MIN) : LONG_MAX );
threshold = ( stop % ( unsigned long )base );
stop /= ( unsigned long )base;
result = 0; track = 0;
for ( ; ; ( i_char = *cursor++ ), ( i_char &= 0xff ) )
{
if ( isdigit( i_char ) )
{
i_char -= '0';
} else if ( isalpha( i_char ) )
{
i_char -= ( isupper(i_char) ? ( 'A' - 10 ) : ( 'a' - 10 ) );
} else
{
break;
}/* end of if elseif else */
if ( i_char >= base )
break;
if ( ( track < 0 ) || ( result > stop ) ||
( ( result == stop ) && ( i_char > threshold ) ))
{
track = -1;
} else
{
track = 1;
result *= base;
result += i_char;
}/* end of if else */
}/* for ( ; ; ( i_char = *cursor++ ), ( i_char &= 0xff ) ) */
if ( track < 0 )
{
result = ( sign ? LONG_MIN : LONG_MAX );
errno = ERANGE;
} else if ( sign )
{
result = -result;
}
if ( endptr != 0 )
*endptr = ( track ? ( fixit( cursor, char * ) - 1 ) :
fixit( str, char * ) );
return result;
}/* long strtol( const char *str, char **endptr, int base ) */
char *itoa ( int value, char *string, int radix )
{
char tmp[ 33 ];
char *tp = tmp, *sp, r[ 33 ];
int i, sign;
unsigned v;
if( radix > 36 || radix <= 1 )
return 0;
sign = ( radix == 10 && value < 0 );
if( sign )
v = -value;
else
v = (unsigned) value;
while( v || tp == tmp ) {
i = v % radix;
v = v / radix;
if( i < 10 )
*tp++ = i + '0';
else
*tp++ = i + 'a' - 10;
}
if( string == 0 )
string = (char *)r;
sp = string;
if( sign )
*sp++ = 'a';
while( tp > tmp )
*sp++ = *--tp;
*sp = 0;
return string;
}
找子串的,
但是有缺陷,
如果有多个最大长度的,只能找出其中最后的一个
#include <iostream.h>
#include <string.h>
int Test(char* src,char &c )
{
int max = 1;
c = src[0];
for(int i=1,begin=0,end =0;i<(int)strlen(src); i++)
{
if(src[i]==src[i-1])
end = i;
else
begin = end = i;
int len = end - begin + 1;
if(max <= len)
{
c = src[i];
max = len;
}
}
return max;
}
void main()
{
char c;
cout<<Test("baaccccac",c);
cout<<c;
}
posted on 2006-02-22 11:58
萌芽的叶子 阅读(263)
评论(0) 编辑 收藏 引用 所属分类:
生活点点嘀嘀