c语言中combination函数,C语言面试题-huaer2008ibm-ChinaUnix博客

#define NUM 1000

int find5(int num){

int ret=0;

while(num%5==0){

num/=5;

ret++;

}

return ret;

}

int main(){

int result=0;

int i;

for(i=5;i<=NUM;i+=5)

{

result+=find5(i);

}

printf(" the total zero number is %dn",result);

return 0;

}

1. 有双向循环链表结点定义为:

struct node

{ int data;

struct node *front,*next;

};

有两个双向循环链表A,B,知道其头指针为:pHeadA,pHeadB,请写一函数将两链表中data值相同的结点删除

BOOL DeteleNode(Node *pHeader, DataType Value)

{

if (pHeader == NULL) return;

BOOL bRet = FALSE;

Node *pNode = pHead;

while (pNode != NULL)

{

if (pNode->data == Value)

{

if (pNode->front == NULL)

{

pHeader = pNode->next;

pHeader->front = NULL;

}

else

{

if (pNode->next != NULL)

{

pNode->next->front = pNode->front;

}

pNode->front->next = pNode->next;

}

Node *pNextNode = pNode->next;

delete pNode;

pNode = pNextNode;

bRet = TRUE;

//不要break或return, 删除所有

}

else

{

pNode = pNode->next;

}

}

return bRet;

}

void DE(Node *pHeadA, Node *pHeadB)

{

if (pHeadA == NULL || pHeadB == NULL)

{

return;

}

Node *pNode = pHeadA;

while (pNode != NULL)

{

if (DeteleNode(pHeadB, pNode->data))

{

if (pNode->front == NULL)

{

pHeadA = pNode->next;

pHeadA->front = NULL;

}

else

{

pNode->front->next = pNode->next;

if (pNode->next != NULL)

{

pNode->next->front = pNode->front;

}

}

Node *pNextNode = pNode->next;

delete pNode;

pNode = pNextNode;

}

else

{

pNode = pNode->next;

}

}

}

2. 编程实现:找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"

int GetCommon(char *s1, char *s2, char **r1, char **r2)

{

int len1 = strlen(s1);

int len2 = strlen(s2);

int maxlen = 0;

for(int i = 0; i < len1; i++)

{

for(int j = 0; j < len2; j++)

{

if(s1[i] == s2[j])

{

int as = i, bs = j, count = 1;

while(as + 1 < len1 && bs + 1 < len2 && s1[++as] == s2[++bs])

count++;

if(count > maxlen)

{

maxlen = count;

*r1 = s1 + i;

*r2 = s2 + j;

}

}

}

}

3. 编程实现:把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列库函数

char* test3(long num) {

char* buffer = (char*)malloc(11);

buffer[0] = '0';

buffer[1] = 'x';

buffer[10] = '';

char* temp = buffer + 2;

for (int i=0; i < 8; i++) {

temp[i] = (char)(num<<4*i>>28);

temp[i] = temp[i] >= 0 ? temp[i] : temp[i] + 16;

temp[i] = temp[i] < 10 ? temp[i] + 48 : temp[i] + 55;

}

return buffer;

}

输入N, 打印 N*N 矩阵

比如 N = 3,打印:

1  2  3

8  9  4

7  6  5

N = 4,打印:

1   2   3   4

12  13  14  5

11  16  15  6

10  9   8   7

解答:

1 #define N 15

int s[N][N];

void main()

{

int k = 0, i = 0, j = 0;

int a = 1;

for( ; k < (N+1)/2; k++ )

{

while( j < N-k ) s[i][j++] = a++; i++; j--;

while( i < N-k ) s[i++][j] = a++; i--; j--;

while( j > k-1 ) s[i][j--] = a++; i--; j++;

while( i > k )   s[i--][j] = a++; i++; j++;

}

for( i = 0; i < N; i++ )

{

for( j = 0; j < N; j++ )

cout << s[i][j] << 't';

cout << endl;

}

}

2 define MAX_N  100

int matrix[MAX_N][MAX_N];

/*

*(x,y):第一个元素的坐标

* start:第一个元素的值

* n:矩阵的大小

*/

void SetMatrix(int x, int y, int start, int n) {

int i, j;

if (n <= 0)    //递归结束条件

return;

if (n == 1) {  //矩阵大小为1时

matrix[x][y] = start;

return;

}

for (i = x; i < x + n-1; i++)   //矩阵上部

matrix[y][i] = start++;

for (j = y; j < y + n-1; j++)   //右部

matrix[j][x+n-1] = start++;

for (i = x+n-1; i > x; i--)     //底部

matrix[y+n-1][i] = start++;

for (j = y+n-1; j > y; j--)     //左部

matrix[j][x] = start++;

SetMatrix(x+1, y+1, start, n-2);   //递归

}

void main() {

int i, j;

int n;

scanf("%d", &n);

SetMatrix(0, 0, 1, n);

//打印螺旋矩阵

for(i = 0; i < n; i++) {

for (j = 0; j < n; j++)

printf("%4d", matrix[i][j]);

printf("n");

}

}

斐波拉契数列递归实现的方法如下:

int  Funct( int n )

{

if(n==0) return 1;

if(n==1) return 1;

retrurn  Funct(n-1) + Funct(n-2);

}

请问,如何不使用递归,来实现上述函数?

请教各位高手!

解答:int  Funct( int n )  //  n 为非负整数

{

int a=0;

int b=1;

int c;

if(n==0) c=1;

else if(n==1) c=1;

else for(int i=2;i<=n;i++)  //应该n从2开始算起

{

c=a+b;

a=b;

b=c;

}

return c;

}

解答:

现在大多数系统都是将低字位放在前面,而结构体中位域的申明一般是先声明高位。

100  的二进制是 001 100 100

低位在前   高位在后

001----s3

100----s2

100----s1

所以结果应该是 1

如果先申明的在低位则:

001----s1

100----s2

100----s3

结果是 4

1、原题跟little-endian,big-endian没有关系

2、原题跟位域的存储空间分配有关,到底是从低字节分配还是从高字节分配,从Dev C++和VC7.1上看,都是从低字节开始分配,并且连续分配,中间不空,不像谭的书那样会留空位

3、原题跟编译器有关,编译器在未用堆栈空间的默认值分配上有所不同,Dev C++未用空间分配为

01110111b,VC7.1下为11001100b,所以在Dev C++下的结果为5,在VC7.1下为1。

注:PC一般采用little-endian,即高高低低,但在网络传输上,一般采用big-endian,即高低低高,华为是做网络的,所以可能考虑big-endian模式,这样输出结果可能为4

判断一个字符串是不是回文

int IsReverseStr(char *aStr)

{

int i,j;

int found=1;

if(aStr==NULL)

return -1;

j=strlen(aStr);

for(i=0;iif(*(aStr+i)!=*(aStr+j-i-1))

{

found=0;

break;

}

return found;

}

Josephu 问题为:设编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m 的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。

数组实现:

#include #include int Josephu(int n, int m)

{

int flag, i, j = 0;

int *arr = (int *)malloc(n * sizeof(int));

for (i = 0; i < n; ++i)

arr[i] = 1;

for (i = 1; i < n; ++i)

{

flag = 0;

while (flag < m)

{

if (j == n)

j = 0;

if (arr[j])

++flag;

++j;

}

arr[j - 1] = 0;

printf("第%4d个出局的人是:%4d号n", i, j);

}

free(arr);

return j;

}

int main()

{

int n, m;

scanf("%d%d", &n, &m);

printf("最后胜利的是%d号!n", Josephu(n, m));

system("pause");

return 0;

}

链表实现:

#include #include typedef struct Node

{

int index;

struct Node *next;

}JosephuNode;

int Josephu(int n, int m)

{

int i, j;

JosephuNode *head, *tail;

head = tail = (JosephuNode *)malloc(sizeof(JosephuNode));

for (i = 1; i < n; ++i)

{

tail->index = i;

tail->next = (JosephuNode *)malloc(sizeof(JosephuNode));

tail = tail->next;

}

tail->index = i;

tail->next = head;

for (i = 1; tail != head; ++i)

{

for (j = 1; j < m; ++j)

{

tail = head;

head = head->next;

}

tail->next = head->next;

printf("第%4d个出局的人是:%4d号n", i, head->index);

free(head);

head = tail->next;

}

i = head->index;

free(head);

return i;

}

int main()

{

int n, m;

scanf("%d%d", &n, &m);

printf("最后胜利的是%d号!n", Josephu(n, m));

system("pause");

return 0;

}

已知strcpy函数的原型是:

char * strcpy(char * strDest,const char * strSrc);

1.不调用库函数,实现strcpy函数。

2.解释为什么要返回char *。

解说:

1.strcpy的实现代码

char * strcpy(char * strDest,const char * strSrc)

{

if ((strDest==NULL)||(strSrc==NULL)) file://[/1]

throw "Invalid argument(s)"; //[2]

char * strDestCopy=strDest;  file://[/3]

while ((*strDest++=*strSrc++)!=''); file://[/4]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值