CC++面试题目汇总(9)

2019-08-02 00:18

void inverse(char *p) {

if( *p = = '\\0' ) return;

inverse( p+1 ); printf( \*p ); }

int main(int argc, char *argv[]) {

inverse(\

return 0; }

借签了楼上的“递规反向输出” #include

void test(FILE *fread, FILE *fwrite) {

char buf[1024] = {0};

if (!fgets(buf, sizeof(buf), fread)) return;

test( fread, fwrite ); fputs(buf, fwrite); }

int main(int argc, char *argv[]) {

FILE *fr = NULL; FILE *fw = NULL; fr = fopen(\\ fw = fopen(\\ test(fr, fw); fclose(fr); fclose(fw); return 0; }

在对齐为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

由于是4字节对齐,

sizeof(struct BBB) = sizeof(*p)

= 4 + 4 + 2 + 1 + 1/*补齐*/ + 2*5 + 2/*补齐*/ = 24 bytes (经Dev-C++验证)

p=0x1000000; p+0x200=____;

= 0x1000000 + 0x200*24

(Ulong)p+0x200=____; = 0x1000000 + 0x200

(char*)p+0x200=____; = 0x1000000 + 0x200*4

你可以参考一下指针运算的细节

写一段程序,找出数组中第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

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 << \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 << \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 << \NO.\<< k << \is:\<< narry[sum - kk] << endl;

cout << \it's place is:\; for (i = 0;i < sum;i++) {

if (addr[i] == narry[sum - kk]) cout << i << '\\t'; }

return0; }

1、找错 Void test1() {

char string[10];

char* str1=\23456789\

strcpy(string, str1);// 溢出,应该包括一个存放'\\0'的字符string[11] }

Void test2() {

char string[10], str1[10]; for(I=0; I<10;I++) {

str1[i] ='a'; }

strcpy(string, str1);// I,i没有声明。 }

Void test3(char* str1) {

char string[10];

if(strlen(str1)<=10)// 改成<10,字符溢出,将strlen改为sizeof也可以 {

strcpy(string, str1); } }

2. void g(int**); int main() {

int line[10],i;

int *p=line; //p是地址的地址 for (i=0;i<10;i++) {

*p=i;

g(&p);//数组对应的值加1 }

for(i=0;i<10;i++) printf(\return 0; }


CC++面试题目汇总(9).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:[参考文档]旅游管理毕业论文参考文献word版本(3页)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: