常见的C语言面试编程题(一)
来源:互联网 发布:带通滤波器算法 编辑:程序博客网 时间:2024/05/21 07:12
最近一直忙着找实习单位,空闲之余,复习了一下c语言和数据结构,写了几段小程序,也有同学面试过程中碰到的一些编程问题:
(1) 求n的阶乘,这是一个比较简单的题目,有很多方法,但用递归方法是最简单的了:
#include <stdlib.h>
#include <stdio.h>
int main()
{
long factorial(long n);
long n;
scanf("%ld",&n);
printf("%ld",factorial(n));
return 0;
}
long factorial(long d)//求阶乘
{
long m;
if(d<0)
{
printf("d的阶乘不存在!");
}
else if(d==0||d==1)
{
m=1;
}
else
{
m=d*factorial(d-1);
}
return m;
}
(2)从一个文件读取整数,对其进行排序,然后再将排序的结果输入到原来文件当中,这是一个经常考的题目,即考你的文件操作,又考了排序,我在这里用的是选择排序
#include <stdlib.h>
#include <stdio.h>
int readtoarray(int *a,FILE *fp)//从文件里将整数读到数组里
{
int i=0;
if(fp==NULL)
{
exit(0);
}
while(fgetc(fp)!=EOF)
{
fscanf(fp,"%d",&a[i]);
printf("%d/n",a[i]);
i++;
}
return i;
}
void writetofile(int a[],FILE *fp,int i)//将数组写到文件里去
{
int k = 0;
if(fp==NULL)
{
exit(0);
}
while(k<i)
{
fprintf(fp,"%c%d",' ',a[k++]);
}
}
void selectionSort(int *a,int i)//选择排序
{
int m,n;
int tmp,min;
for(m=0;m <i-1;m++)
{
min=m;
for(n=m+1;n <i;n++)
{
if(a[n]<a[min])
min=n;
}
tmp=a[m];
a[m]=a[min];
a[min]=tmp;
}
}
int main()
{
FILE* fp,* fpwrite;
int i;
int a[10];
fp=fopen("2.txt","r");
i=readtoarray(a,fp);
fclose(fp);
selectionSort(a,i);
fpwrite=fopen("2.txt","w");
writetofile(a, fpwrite,i);
fclose(fpwrite);
return 0;
}
以下是单向链表、双向链表,栈,队列的C语言的实现方法,只包含简单的插入删除操作 1,单向链表的插入,删除,逆序操作 #include <stdio.h> typedef struct Node node newNode(int k) void printlist(node n) node insertNode(node n,int p,int k) node deleteNode(node n,int k) node reverse(node n) node n1[10]; if(head==NULL) int main() //插入操作 //删除操作 //链表的倒置操作 2,双向链表的插入删除操作 #include <stdlib.h> typedef struct Node node newNode(int i) node newduplinklist() void print(node n) } node delNode(node n) } 3,栈操作,用数组实现的包含出栈,入栈的操作 #include <stdio.h> typedef struct Sta sta NullStack() sta pushstack(sta s,int i) sta popstack(sta s) void printstack(sta s) return 0; 4,队列操作,类似于栈, #include <stdio.h> typedef struct Que que NullQueue() que enq(que q,int i) que deq(que q) void printqueue(que q) return 0; 1,航天二院某所面试题,考查的是结构体和数组的内存布局情况。 #include <stdlib.h> typedef struct array1{ typedef struct array2{ int main() { } 2,某通信企业的面试题目,从字符串数组和指针字符串在内存中的分配情况考查指针的使用。 #include <stdlib.h> char *GetMemory(char *p) void Test(void) void Test1() void GetMemory2(char **p, int num) void Test2(void) void Test3(void) int main() 3,c语言中sizeof的用法 void fun(char s[10]) int main() { char str[]={"sasdasdes"}; printf("%d/n",sizeof(str));//字符串数组的大小10(包含了字符'/0') printf("%d/n",strlen(str)));//字符串的长度9 printf("%d/n",sizeof(p));//指针的大小4 printf("%d/n",strlen(p));//字符串的长度9 void *h=malloc(100); printf("%d/n",strlen(ss));//字符串的长度4 }
#include <stdlib.h>
{
int key;
struct Node* next;
}* node;
{
node n=(node)malloc(sizeof(node));
n->key=k;
n->next=NULL;
return n;
}
{
if(!n)
{
printf("n is NULL list/n");
}
while(n)
{
printf("%d",n->key);
printf(" ");
n=n->next;
}
printf("/n");
}
node newList()
{
int k;
node head=(node)malloc(sizeof(node));;
scanf("%d",&k);
if(k==0)
{
head=NULL;
return head;
}
else
{
node n=newNode(k);
head=n;
while(k)
{
scanf("%d",&k);
if(k!=0)
{
node n1=newNode(k);
n->next=n1;
n=n->next;
}
}
n->next=NULL;
return head;
}
}
{
node n1=newNode(k);
node head=(node)malloc(sizeof(node));
head=n;
if(head==NULL)
{
n1->next=head;
return n1;
}
else
{
if(p==1)
{
n1->next=head;
head=n1;
return head;
}
else
{
int i=2;
while(i!=p&&(n->next))
{
n=n->next;
i++;
}
if(n->next==NULL)
{
printf("the p can't be found/n");
return head;
}
else
{
n1->next=n->next;
n->next=n1;
return head;
}
}
}
}
{
node n1=(node)malloc(sizeof(node));
node head=(node)malloc(sizeof(node));
head=n;
if(head==NULL)
{
printf("list is NULL/n");
return head;
}
else
{
if(head->key==k)
{
head=head->next;
return head;
}
while(n->key!=k&&n->next)
{
n1=n;
n=n->next;
}
if(n==NULL)
{
printf("can't find the same value as k in this list/n");
return head;
}
else
{
n1->next=n->next;
n=NULL;
return head;
}
}
}
{
node head=(node)malloc(sizeof(node));
node n2=(node)malloc(sizeof(node));
head=n;
{
return head;
}
else
{
int i=0;
while(head!=NULL)
{
n2=head;
head=head->next;
n2->next=NULL;
n1[i]=n2;
i++;
}
head=n1[i-1];
for(int j=i-1;j>0;j--)
{
n1[j]->next=n1[j-1];
}
return head;
}
}
{
node n=newList();
printlist(n);
int k,p;
scanf("%d,%d",&p,&k);
node nn=insertNode(n,p,k);
printlist(nn);
int q;
scanf("%d",&q);
node nd=deleteNode(nn,q);
printlist(nd);
node m=reverse(n);
printlist(m);
return 0;
}
#include <stdio.h>
{
int key;
struct Node* pre;
struct Node* next;
}* node;
{
node n=(node)malloc(sizeof(node));
n->key=i;
n->pre=NULL;
n->next=NULL;
return n;
}
{
int i;
scanf("%d",&i);
node n;
if(i==0)
{
n=NULL;
return n;
}
n=newNode(i);
node head=n;
int k=1;
while(k!=0)
{
scanf("%d",&k);
if(k!=0)
{
node n1=newNode(k);
n->next=n1;
n1->pre=n;
n=n1;
}
}
n->next=head;
head->pre=n;
return head;
}
int sizeduplinklist(node n)
{ if(n==NULL) return 0;
node head=n;
int i=1;
while(head->next!=n)
{
head=head->next;
i++;
}
return i;
}
{
if(n==NULL)
printf("此时链表为空!");
else
{
printf("输出链表:/n");
for(int i=0;i<sizeduplinklist(n);i++)
{
printf("%-2d",n->key);
n=n->next;
}
printf("/n");
}
}
node insertNode(node n)
{
int p,k;
printf("插入位置p:/n");
scanf("%d,%d",&p,&k);
printf("/n");
if(p>sizeduplinklist(n))
{
printf("此位置超出链表的长度!/n");
return n;
}
else if(p<1)
{
printf("此位置不存在!/n");
return n;
else
{
node n1,head,m;
m=newNode(k);
head=n;
if(p==1)
{
m->pre=n->pre;
n->pre->next=m;
m->next=n;
n->pre=m;
return m;
}
else
{
int i=1;
while(i!=p)
{
n1=n;
n=n->next;
i++;
}
n1->next=m;
m->pre=n1;
m->next=n;
n->pre=m;
return head;
}
}
}
{
int p;
printf("删除位置p:/n");
scanf("%d",&p);
printf("/n");
if(p>sizeduplinklist(n))
{
printf("此位置超出链表的长度!/n");
return n;
}
else if(p<1)
{
printf("此位置不存在!/n");
return n;
else
{
node n1,head;
if(p==1)
{ head=n->next;
n->pre->next=n->next;
n->next->pre=n->pre;
return head;
}
else
{
head=n;
int i=1;
while(i!=p)
{
n1=n;
n=n->next;
i++;
}
n1->next=n->next;
n->next->pre=n1;
return head;
}
}
}
int main()
{
node n=newduplinklist();
printf("%d/n",sizeduplinklist(n));
print(n);
node m=insertNode(n);
print(m);
node m1=delNode(m);
print(m1);
return 0;
}
#include <stdlib.h>
{
int a[20];
int num;
}* sta;
{
sta s=(sta)malloc(sizeof(sta));
s->num=0;
return s;
}
{
s->a[s->num]=i;
s->num=s->num+1;
return s;
}
{
if(s->num==0)
{
printf("stack is NULL/n");
return s;
}
s->num=s->num-1;
return s;
}
{
if(s->num==0)
{
printf("stack is NULL/n");
}
else
{
for(int i=s->num-1; i>=0;i--)
{
printf("%d/n",s->a[i]);
}
}
}
int main()
{
sta s=NullStack();
s=pushstack(s,1);
s=pushstack(s,2);
printstack(s);
s=popstack(s);
s=popstack(s);
printstack(s);
}
#include <stdlib.h>
{
int a[20];
int num;
}* que;
{
que q=(que)malloc(sizeof(que));
q->num=0;
return q;
}
{
q->a[q->num]=i;
q->num=q->num+1;
return q;
}
{
if(q->num==0)
{
printf("queue is NULL/n");
return q;
}
for(int i=1;i<q->num;i++)
{
q->a[i-1]=q->a[i];
}
q->num=q->num-1;
return q;
}
{
if(q->num==0)
{
printf("queue is NULL/n");
}
else
{
for(int i=q->num-1; i>=0;i--)
{
printf("%3d",q->a[i]);
}
}
}
int main()
{
que q=NullQueue();
q=enq(q,1);
q=enq(q,2);
q=enq(q,3);
q=enq(q,4);
printqueue(q);
printf("/n");
q=deq(q);
//printf("%d",s->a[0]);
//q=deq(q);
printqueue(q);
}
#include <stdio.h>
int ID;
struct array1* next;
}A;
int ID;
int a;
int b;
int c;
}* B;
A s1[15];
A* s2;
B s3;
for(int i=0;i<10;i++)
{
s1[i].ID=i+64;
}
s2=s1+3;
s3=(B)s2;
printf("%d/n",s3->b);
return 0;
#include <stdio.h>
#include <string.h>
{
p = (char *)malloc(100);
return p;
}//当调用此函数时,会在栈里分配一个空间存储p, p指向堆当中的一块内存区,当函数调用结束后,若函数没有返回值,
//系统自动释放栈中的P
{
char *str = NULL;
str=GetMemory(str);
strcpy(str, "test");
printf("%s/n",str);
}
char *GetMemory1(void)
{
char *p = "Test1";
return p;
}//若换成char p[]="hello world"; 就会在函数调用结束后,释放掉为"Test1"的拷贝分配的空间,返回的P只是一个野指针
{
char *str = "";
str=GetMemory1();
printf("%s/n",str);
//str=GetMemory();
}
{
*p = (char *)malloc(num);
}//当调用此函数时,会在栈里分配一个空间存储p, p指向栈中的一变量str,在此函数中为str在堆当中分配了一段内存空间
//函数调用结束后,会释放p, 但str所在的函数Test2还没运行完,所以str此时还在栈里.
{
char *str = NULL;
GetMemory2(&str, 100);
strcpy(str, "hello");
printf("%s/n",str);
}
{
char *str=(char *)malloc(100);
strcpy(str, "hello");//此时的str指向的是拷贝到栈里的"hello",所以当释放掉str指向的堆空间时,str指向的栈里的值还是不变
free(str);
if(str != NULL)
{
strcpy(str, "world");
printf("%s/n",str);
}
}
{
Test();
Test1();
Test2();
Test3();
}
{
printf("%s/n",s);
printf("%d/n",sizeof(s));//引用的大小
}
char *p=str;
fun(str);
char ss[100]="abcd";
printf("%d/n",sizeof(ss));//字符串数组的大小100
printf("%d/n",sizeof(h));//指针的大小4
- 常见的C语言面试编程题(一)
- 常见的C语言面试编程题(一)
- 常见的C语言面试编程题(二)
- 常见的C语言面试编程题(三)
- 【c语言】c语言常见编程题总结(一)
- 面试常见编程题专题一:链表
- C语言笔试面试常见编程题目(更新中)
- 自己整理的C语言常见编程题
- c语言编程的常见错误
- C语言编程中常见的错误
- C语言 函数编程的常见错误
- C语言常见编程题【索引】
- 一个C语言面试编程题
- 常见面试编程题
- 【c语言】c语言常见编程题总结(二)
- 【c语言】c语言常见编程题总结(三)
- 【c语言】c语言常见编程题总结(四)
- 【c语言】c语言常见编程题总结(五)
- 韩寒经典爱情语录
- SQL Server查詢可用磁盘空间
- 两道C语言经典编程
- 为什么?!——《走出软件作坊》读后感想
- javascript 获取URL参数
- 常见的C语言面试编程题(一)
- 如何獲得GridView內LinkButton的RowIndex? (.NET) (ASP.NET)
- 设下心防
- GridView 介绍
- div JS模拟滚动条
- How to debug the exception
- 这些人有机会成为Linux高手
- 怎样用word制作标准格式公文操作实例
- 浅谈C++中内存泄漏的检测