C是高階語言:它把高階語言的基本結構和語句與低階語言的實用性結合起來。C 語言可以像組合語言一樣對位、位元組和地址進行操作,而這三者是計算機最基本的工作單元。下面就由本站小編為大家介紹一下c經典筆試題的文章,歡迎閱讀。
c經典筆試題篇1
有兩個磁碟檔案A和B,各存放一行字母,要求把這兩個檔案中的資訊合併(按字母順序排列),輸出到一個新檔案C中.
#include
#include
int main(int argc,char* argv)
{
FILE* fp;
int i,j,k,num,NUM;
char c[50],t,ch;
if((fp=fopen("A","r"))==NULL)
/*can be replaced by open
* int fd=open("A",O_RDONLY|O_CREAT);*/
{
printf("fileA cannot be openedn");
exit(0);
}
printf("nA contents are:n");
for(i=0;(ch=fgetc(fp))!=EOF;i++)/*一個字元一個字元讀*/
{
c[i]=ch;
putchar(c[i]);
}
num=i+1;
fclose(fp);
if((fp=fopen("B","r"))==NULL)
{
printf("fileB cannot be openedn");
exit(0);
}
printf("nB contents are :n");
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{
c[num+i]=ch;
putchar(c[num+i]);
}
fclose(fp);
NUM=num+i+1;
for(k=0;k
{
for(j=0;j
{
if(c[j]>c[j+1])
{
t=c[j];
c[j]=c[j+1];
c[j+1]=t;
}
}
}
printf("nC fileis:n");
fp=fopen("C","w");
for(i=0;i
{
putc(c[i],fp);/*將字元一個個寫入檔案中*/
putchar(c[i]);/*一個個輸出字元*/
}
fclose(fp);
return 1;
}
c經典筆試題篇2
有一浮點型陣列A,用C語言寫一函式實現對浮點陣列A進行降序排序,並輸出結果,要求要以陣列A作為函式的入口.(建議用氣泡排序法)
#include
#include
void BubbleSort(int arr, int n)
{
int i,j;
int exchange = 1;//交換標誌,提高演算法效率;
int temp;
for(i=0;i
{
exchange=0;//本趟排序開始前,交換標誌應為假
for(j=0;j
{
if(arr[j+1] > arr[j])
{
temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
exchange=1; //發生了交換,故將交換標誌置為真
}
}
if(!exchange) //本趟排序未發生交換,提前終止演算法
return;
}
}
int main(int argc,char* argv)
{
int arr[5]={1,4,2,6,5};
int i;
BubbleSort(arr, 5);
printf("after sort,arr is :n");
for(i=0;i<5;i++)
{
printf("%3d",arr[i]);
}
return 1;
}
c經典筆試題篇3
寫出二分查詢的程式碼:
Int binary_search(int* arr,int key,int size)
{
Intmid;
Intlow=0;
Int high=size-1;
While(low<=high)
{
Mid=(low+high)/2;
If(arr[mid]>key)
High=mid-1;
ElseIf(arr[mid]
Low=mid+1;
Else
Return mid;
}
Return -1;
}
請編寫一個C 函式,該函式在一個字串中找到可能的最長的子字串,該字串是由同一字元組成的。
#include
#include
#include
int ChildString(char*p)
{
char* q=p;
int stringlen=0, i=0,j=1,len=0,maxlen=1;
//stringlen=strlen(p);
while(*q!='