?? c,c++筆試.txt
字號:
node *first = (node*)malloc(sizeof(node));
first->data = 'a';first->next=NULL;head->next = first;
p = first;
int longth = 'z' - 'b';
int i=0;
while ( i<=longth )
{
node *temp = (node*)malloc(sizeof(node));
temp->data = 'b'+i;temp->next=NULL;q=temp;
head->next = temp; temp->next=p;p=q;
i++;
}
print(head);
6.可怕的題目終于來了
象搜索的輸入信息是一個字符串,統計300萬輸入信息中的最熱門的前十條,我們每次輸入的一個字符串為不超過255byte,內存使用只有1G,
請描述思想,寫出算發(c語言),空間和時間復雜度,
7.國內的一些帖吧,如baidu,有幾十萬個主題,假設每一個主題都有上億的跟帖子,怎么樣設計這個系統速度最好,請描述思想,寫出算發(c語言),空間和時間復雜度,
#include string.h
main(void)
{ char *src="hello,world";
char *dest=NULL;
dest=(char *)malloc(strlen(src));
int len=strlen(str);
char *d=dest;
char *s=src[len];
while(len--!=0)
d++=s--;
printf("%s",dest);
}
找出錯誤!!
#include "string.h"
#include "stdio.h"
#include "malloc.h"
main(void)
{
char *src="hello,world";
char *dest=NULL;
dest=(char *)malloc(sizeof(char)*(strlen(src)+1));
int len=strlen(src);
char *d=dest;
char *s=src+len-1;
while(len--!=0)
*d++=*s--;
*d='\0';
printf("%s",dest);
}
intel c語言
1。struct s1
{
int i: 8;
int j: 4;
int a: 3;
double b;
};
struct s2
{
int i: 8;
int j: 4;
double b;
int a:3;
};
printf("sizeof(s1)= %d\n", sizeof(s1));
printf("sizeof(s2)= %d\n", sizeof(s2));
答案:16, 24
第一個struct s1
{
int i: 8;
int j: 4;
int a: 3;
double b;
};
理論上是這樣的,首先是i在相對0的位置,占8位一個字節,然后,j就在相對一個字節的位置,由于一個位置的字節數是4位的倍數,因此不用對齊,就放在那里了,然后是a,要在3位的倍數關系的位置上,因此要移一位,在15位的位置上放下,目前總共是18位,折算過來是2字節2位的樣子,由于double是8字節的,因此要在相對0要是8個字節的位置上放下,因此從18位開始到8個字節之間的位置被忽略,直接放在8字節的位置了,因此,總共是16字節。
第二個最后會對照是不是結構體內最大數據的倍數,不是的話,會補成是最大數據的倍數
編程題
2。讀文件file1.txt的內容(例如):
12
34
56
輸出到file2.txt:
56
34
12
(逆序)
答案:注意可增長數組的應用.
#include <stdio.h>
#include <stdlib.h>
3。輸出和為一個給定整數的所有組合
例如n=5
5=1+4;5=2+3(相加的數不能重復)
則輸出
1,4;2,3。
答案:
#include <stdio.h>4。在對齊為4的情況下
struct BBB
{
long num;
char *name;
short int data;
char ha;
short ba[5];
}*p;
p=0x1000000;
p+0x200=____;
(Ulong)p+0x200=____;
(char*)p+0x200=____;
答案:假設在32位CPU上,
sizeof(long) = 4 bytes
sizeof(char *) = 4 bytes
sizeof(short int) = sizeof(short) = 2 bytes
sizeof(char) = 1 bytes
int main(void)
{
unsigned long int i,j,k;
printf("please input the number\n");
scanf("%d",&i);
if( i % 2 == 0)
j = i / 2;
else
j = i / 2 + 1;
printf("The result is \n");
for(k = 0; k < j; k++)
printf("%d = %d + %d\n",i,k,i - k);
return 0;
}
#include <stdio.h>
void main()
{
unsigned long int a,i=1;
scanf("%d",&a);
if(a%2==0)
{
for(i=1;i<a/2;i++)
printf("%d",a,a-i);
}
else
for(i=1;i<=a/2;i++)
printf(" %d, %d",i,a-i);
}
一個遞規反向輸出字符串的例子,可謂是反序的經典例程.
void inverse(char *p)
{
if( *p = = '\0' )
return;
inverse( p+1 );
printf( "%c", *p );
}
int main(int argc, char *argv[])
{
inverse("abc\0");
return 0;
}
由于是4字節對齊,
sizeof(struct BBB) = sizeof(*p)
= 4 + 4 + 4((2 + 1 )+ 1補齊為4)+ 12(2*5 + 2補齊為12) = 24 bytes
p=0x1000000;
p+0x200=____;
= 0x1000000 + 0x200*24
(Ulong)p+0x200=____;
= 0x1000000 + 0x200
(char*)p+0x200=____;
= 0x1000000 + 0x200*4
5。寫一段程序,找出數組中第k大小的數,輸出數所在的位置。例如{2,4,3,4,7}中,第一大的數是7,位置在4。第二大、第三大的數都是4,位置在1、3隨便輸出哪一個均可。函數接口為:int find_orderk(const int* narry,const int n,const int k)
要求算法復雜度不能是O(n^2)
答案:可以先用快速排序進行排序,其中用另外一個進行地址查找
代碼如下,在VC++6.0運行通過。給分吧^-^
//快速排序
#include<iostream>
usingnamespacestd;
intPartition (int*L,intlow,int high)
{
inttemp = L[low];
intpt = L[low];
while (low < high)
{
while (low < high && L[high] >= pt)
--high;
L[low] = L[high];
while (low < high && L[low] <= pt)
++low;
L[low] = temp;
}
L[low] = temp;
returnlow;
}
voidQSort (int*L,intlow,int high)
{
if (low < high)
{
intpl = Partition (L,low,high);
QSort (L,low,pl - 1);
QSort (L,pl + 1,high);
}
}
intmain ()
{
intnarry[100],addr[100];
intsum = 1,t;
cout << "Input number:" << endl;
cin >> t;
while (t != -1)
{
narry[sum] = t;
addr[sum - 1] = t;
sum++;
cin >> t;
}
sum -= 1;
QSort (narry,1,sum);
for (int i = 1; i <= sum;i++)
cout << narry[i] << '\t';
cout << endl;
intk;
cout << "Please input place you want:" << endl;
cin >> k;
intaa = 1;
intkk = 0;
for (;;)
{
if (aa == k)
break;
if (narry[kk] != narry[kk + 1])
{
aa += 1;
kk++;
}
}
cout << "The NO." << k << "number is:" << narry[sum - kk] << endl;
cout << "And it's place is:" ;
for (i = 0;i < sum;i++)
{
if (addr[i] == narry[sum - kk])
cout << i << '\t';
}
return0;
}
int main(void)
{
int MAX = 10;
int *a = (int *)malloc(MAX * sizeof(int));
int *b;
FILE *fp1;
FILE *fp2;
fp1 = fopen("a.txt","r");
if(fp1 == NULL)
{printf("error1");
exit(-1);
}
fp2 = fopen("b.txt","w");
if(fp2 == NULL)
{printf("error2");
exit(-1);
}
int i = 0;
int j = 0;
while(fscanf(fp1,"%d",&a[i]) != EOF)
{
i++;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -