地點:深圳南山區
方式:閉卷,寫程序
1、將一個字符串逆序
2、將一個鏈表逆序
3、計算一個字節裏(byte)裏面有多少bit被置1
4、搜索給定的字節(byte)
5、在一個字符串中找到可能的最長的子字符串
6、字符串轉換爲整數
7、整數轉換爲字符串
2、將一個鏈表逆序
3、計算一個字節裏(byte)裏面有多少bit被置1
4、搜索給定的字節(byte)
5、在一個字符串中找到可能的最長的子字符串
6、字符串轉換爲整數
7、整數轉換爲字符串
* 題目:將一個字符串逆序
* 完成時間:2006.9.30深圳極訊網吧
* 版權歸劉志強所有
* 描述:寫本程序的目的是希望練一下手,希望下午去面試能成功,不希望國慶節之後再去找工作拉!
*/
#include <iostream>
using namespace std;
//#define NULL ((void *)0)
char * mystrrev(char * const dest,const char * const src)
{
if (dest==NULL && src==NULL)
return NULL;
int val_len = strlen(src);
dest[val_len] = '\0';
int i;
for (i=0; i<val_len; i++)
{
*(dest+i) = *(src+val_len-i-1);
}
}
main()
{
char *str="asdfa";
char *str1=NULL;
str1 = (char *)malloc(20);
if (str1 == NULL)
cout<<"malloc failed";
free(str1);
str1=NULL;//杜絕野指針
}
p=head;
q=p->next;
while(q!=NULL)
{
temp=q->next;
q->next=p;
p=q;
q=temp;
}
這樣增加個輔助的指針就行樂。
ok 通過編譯的代碼:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int data;
struct List *next;
}List;
{
struct List *head,*tail,*p;
int e;
head=(List *)malloc(sizeof(List));
tail=head;
printf("\nList Create,input numbers(end of 0):");
scanf("%d",&e);
while(e){
p=(List *)malloc(sizeof(List));
p->data=e;
tail->next=p;
tail=p;
scanf("%d",&e);}
return head;
}
{
List *p,*q,*r;
p=head;
q=p->next;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head=p;
return head;
}
{
struct List *head,*p;
int d;
head=list_create();
printf("\n");
for(p=head->next;p;p=p->next)
printf("--%d--",p->data);
printf("\n");
for(p=head;p->next;p=p->next)
printf("--%d--",p->data);
}
解:此題按步驟解:先定位到某一個BYTE數據;再計算其中有多少個1。疊加得解。
#define N 10
//定義BYTE類型別名
#ifndef BYTE
typedef unsigned char BYTE;
#endif
{
int count=0;
int bi,bj;
BYTE cc=1,tt;
for(bi=0;bi<n;bi++)
{
//計算該BYTE的8個bit中有多少個1
tt=b[bi];
for(bj=0;bj<8;bj++)
{
//與1相與或模2結果是否是1?測試當前bit是否爲1
//if(tt%2==1)
if((tt&cc)==1)
{
count++;
}
//右移一位或除以2,效果相同
//tt=tt>>1;
tt=tt/2;
}
}
return count;
}
int main()
{
BYTE b[10]={3,3,3,11,1,1,1,1,1,1};
cout<<comb(b,N)<<endl;
return 0;
}
1。編寫一個 C 函數,該函數在一個字符串中找到可能的最長的子字符串,且該字符串是由同一字符組成的。
char * search(char *cpSource, char ch)
{
char *cpTemp=NULL, *cpDest=NULL;
int iTemp, iCount=0;
while(*cpSource)
{
if(*cpSource == ch)
{
iTemp = 0;
cpTemp = cpSource;
while(*cpSource == ch)
++iTemp, ++cpSource;
if(iTemp > iCount)
iCount = iTemp, cpDest = cpTemp;
if(!*cpSource)
break;
}
++cpSource;
}
return cpDest;
}
#include <stdio.h>
#include <string.h>
{
int i;
int weight = 1; // 權重
int rtn = 0; // 用作返回
{
rtn += (str[i] - '0')* weight; //
weight *= 10; // 增重
}
}
{
char str[32];
gets(str);
}
#include<string.h>
void reverse(char s[])
{ //字符串反轉
int c, i=0, j;
for(j=strlen(s)-1;i<j;j--)
{ c=s[i];
s[i]=s[j];
s[j]=c;
i++;
}
}
void IntegerToString(char s[],int n)
{ int i=0,sign;
if((sign=n)<0)//如果是負數,先轉成正數
n=-n;
do //從個位開始變成字符,直到最高位,最後應該反轉
{ s[i++]=n%10+'0';
}while((n=n/10)>0);
//如果是負數,補上負號
if(sign<0)
s[i++]='-';
s[i]='\0';//字符串結束
reverse(s);
}
void main()
{ int m;
char c[100];
printf("請輸入整數m: ");
scanf("%d",&m);
IntegerToString(c,m);
printf("integer = %d string = %s\n", m, c);
}