笔试面试题三
2012-08-29 19:56
148 查看
1、 用变量a给出下面的定义
a) 一个整型数(An integer)
b) 一个指向整型数的指针(A pointer to an integer)
c) 一个指向指针的的指针,它指向的指针是指向一个整型数(A pointer to a pointer to an integer)
d) 一个有10个整型数的数组(An array of 10 integers)
e) 一个有10个指针的数组,该指针是指向一个整型数的(An array of 10 pointers to integers)
f) 一个指向有10 个整型数数组的指针(A pointer to an array of 10 integers)
g) 一个指向函数的指针,该函数有一个整型参数并返回一个整型数(A pointer to a function that takes an integer as an argument and returns an integer)
h) 一个有10 个指针的数组,该指针指向一个函数,该函数有一个整型参数并返回一个整型数( An array of ten pointers to functions that take an integer argument and return an integer )
答案是:
a) int a; // An integer
b) int *a; // A pointer to an integer
c) int **a; // A pointer to a pointer to an integer
d) int a[10]; // An array of 10 integers
e) int *a[10]; // An array of 10 pointers to integers
f) int (*a)[10]; // A pointer to an array of 10 integers
g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer
h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer
2、使用指针的三个好处
直接访问内存,函数返回多个值,函数为指针传参时避免了创建临时对象
3、将0值传给malloc函数,会发生什么有趣的事情呢?
4、有一个16位的整数,每4位为一个数,写函数求他们的和。解释:整数1101010110110111和 1101+0101+1011+0111
5、找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
6、int A[nSize],其中隐藏着若干0,其余非0整数,写一个函数int Func(int* A, int nSize),使A把0移至后面,非0整数移至数组前面并保持有序,返回值为原数据中第一个元素为0的下标。
7、写一个程序, 要求功能:求出用1,2,5这三个数不同个数组合的和为100的组合个数。
因为x+2y+5z=100
所以x+2y=100-5z,且z<=20 x<=100 y<=50
所以(x+2y)<=100,且(x+5z)是偶数
对z作循环,求x的可能值如下:
z=0, x=100, 98, 96,... 0
z=1, x=95, 93, ...,1
z=2, x=90, 88, ...,0
z=3, x=85, 83, ...,1
z=4, x=80, 78, ...,0
......
z=19, x=5, 3, 1
z=20, x=0
a) 一个整型数(An integer)
b) 一个指向整型数的指针(A pointer to an integer)
c) 一个指向指针的的指针,它指向的指针是指向一个整型数(A pointer to a pointer to an integer)
d) 一个有10个整型数的数组(An array of 10 integers)
e) 一个有10个指针的数组,该指针是指向一个整型数的(An array of 10 pointers to integers)
f) 一个指向有10 个整型数数组的指针(A pointer to an array of 10 integers)
g) 一个指向函数的指针,该函数有一个整型参数并返回一个整型数(A pointer to a function that takes an integer as an argument and returns an integer)
h) 一个有10 个指针的数组,该指针指向一个函数,该函数有一个整型参数并返回一个整型数( An array of ten pointers to functions that take an integer argument and return an integer )
答案是:
a) int a; // An integer
b) int *a; // A pointer to an integer
c) int **a; // A pointer to a pointer to an integer
d) int a[10]; // An array of 10 integers
e) int *a[10]; // An array of 10 pointers to integers
f) int (*a)[10]; // A pointer to an array of 10 integers
g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer
h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer
2、使用指针的三个好处
直接访问内存,函数返回多个值,函数为指针传参时避免了创建临时对象
3、将0值传给malloc函数,会发生什么有趣的事情呢?
#include "stdafx.h" #include "stdlib.h" #include <string.h> #include <iostream.h> int main(int argc, char* argv[]) { char *ptr=(char*)malloc(0); int size=sizeof(ptr); int len=strlen(ptr); if(ptr==NULL) cout<<"Got a null pointer"<<endl; else cout<<"Got a valid pointer"<<endl; //ptr不为NULL if(size==4) cout<<"Got a null pointer"<<endl; //指针大小为4 else cout<<"Got a valid pointer"<<endl; if(len==0) cout<<"Got a null pointer"<<endl; else cout<<"Got a valid pointer"<<endl; //strlen(ptr)为24??,因为ptr指向的空间没有初始化 *ptr=6; cout<<*ptr<<endl; //ok cout<<strlen(ptr)<<endl; //长度为22?? return 0; }
4、有一个16位的整数,每4位为一个数,写函数求他们的和。解释:整数1101010110110111和 1101+0101+1011+0111
short SumOfQuaters(unsigned short n) { short c = 0; int i = 4; do { c += n & 15; n = n >> 4; } while (--i); return c; }
5、找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"
#include <stdio.h> #include <string.h> #include <iostream.h> void same_str( char *str1, char const *str2, char const *str3 ) { int i,j,k,len,max=0,n=0; const char *temp; if (strlen(str2) < strlen(str3)) /*为了保证外层循环的那个字符串总是最长的*/ { temp = str2; str2 = str3; str3 = temp; } for ( i=0; *(str2+i)!='\0'; i++ ) { len = 0; for ( j=0,k=i; *(str3+j)!='\0'&&*(str2+k)!='\0'; j++,k++ ) { if ( *(str2+k) == *(str3+j) ) { len++; } else /*当下一个字符不相同时,要避免相隔相同的字母len累加*/ { if ( max < len ) { max = len; //相同字符串长度 n = k-len; //相同字符串起始地方 } len = 0; } } if ( max < len ) /*当一个字符串的末尾字符串相等时*/ { max = len; n = k-len; len = 0; } } for ( j=0; j<max; j++,n++ ) *str1++ = *(str2+n); *str1 = '\0'; } void main() { char str1[10]; char *str2="aaaaabcd"; char *str3="aaaaabcdefg"; same_str( str1, str2, str3 ); cout<<str1<<endl; }
6、int A[nSize],其中隐藏着若干0,其余非0整数,写一个函数int Func(int* A, int nSize),使A把0移至后面,非0整数移至数组前面并保持有序,返回值为原数据中第一个元素为0的下标。
int func(int *A,int Nsize) { int i,t=1; for(i=0;i<Nsize-1;i++) { if(A[i]==0&&A[i+1]!=0) //交换永远是排在前面的0 { A[i+1-t]=A[i+1]; A[i+1]=0; } else if(A[i]==0&&A[i+1]==0) //记录0的个数 { t++; } } return t; }
7、写一个程序, 要求功能:求出用1,2,5这三个数不同个数组合的和为100的组合个数。
因为x+2y+5z=100
所以x+2y=100-5z,且z<=20 x<=100 y<=50
所以(x+2y)<=100,且(x+5z)是偶数
对z作循环,求x的可能值如下:
z=0, x=100, 98, 96,... 0
z=1, x=95, 93, ...,1
z=2, x=90, 88, ...,0
z=3, x=85, 83, ...,1
z=4, x=80, 78, ...,0
......
z=19, x=5, 3, 1
z=20, x=0
int CombCount(int m) { int number=0; for (int i=0;i<=m;i+=5) { number+=(i+2)/2; } return number; }
相关文章推荐
- 笔试/面试题_算法
- Hadoop笔试/面试题
- android面试笔试常见面试题
- 一些操作系统笔试、面试题
- 笔试——全排序算法(字典序法)——记Works Applications面试题
- Java常见面试题笔试题
- 《计算机操作系统笔试面试题及答案》
- JAVA面试题(笔试改错题,附答案)
- 各大IT公司校园招聘程序猿笔试、面试题集锦
- Linux笔试面试题总结
- 经典笔试面试题——宏macro
- Java笔试面试题二(常考问答)
- 【C语言】【面试题】【笔试题】对于宏的使用,if endif #define _DEBUG_ ifdef _DEBUG_
- C/C++ 笔试面试(3)——内存管理GetMemory 确实遇到的面试题
- 空间系统一步一个脚印之笔试面试——腾讯面试题
- C和C++笔试面试题汇总
- 前端笔试面试题
- [C#]C#面试题笔试题(三)
- 微软笔试面试题集锦 --留着自己慢慢研究啊
- 转:迅雷亲历面经:笔试+上机+面试(完整JAVA面试题求解大讨论)