数组测试题
轉(zhuǎn)自:http://bbs.yingjiesheng.com/thread-37806-1-1.html
對數(shù)組,指針,數(shù)據(jù)結(jié)構(gòu),算法,字符串,文件操作等問題都有覆蓋.主要以c語言的實現(xiàn)為主,也有c++的題.大家可以先做做這10道題,測試一下自己的水平.
1. 下面這段代碼的輸出是多少(在32位機(jī)上).
??? char *p;
??? char *q[20];
??? char *m[20][20];
??? int (*n)[10];
??? struct MyStruct
{
char dda;
double dda1;
int type ;
};
MyStruct k;
?printf("%d %d %d %d",sizeof(p),sizeof(q),sizeof(m),sizeof(n),sizeof(k));
答案:4,80,1600,4,24
?????????? n是指向一維數(shù)組的指針變量;k中不要忘了考慮對齊問題,這里dda為4個字節(jié)。
2.
(1)
char a[2][2][3]={{{1,6,3},{5,4,15}},{{3,5,33},{23,12,7}} };
for(int i=0;i<12;i++)
printf("%d ",_______);
在空格處填上合適的語句,順序打印出a中的數(shù)字
答 案:a[i/6][(i/3)%2][i%3];這道題目是多維數(shù)組的輸出問題,這里要考慮的是每維數(shù)字的取值順序問題:第一維,前六次循環(huán)都取0,后六 次取1,于是i/6可以滿足要求;第二維,前3次為0,再3次為1,再3次為0,再3次為1,用量化的思想,i/3把12個數(shù)字分為4組每組3個,量化為 0、1、2、3,為要得到0、1、0、1我們這里就需要對(0、1、2、3)%2=(0、1、0、1),于是(i/3)%2;最后一維我們需要的是(0、 1、2;0、1、2;0、1、2;0、1、2;)我們就i%3。
?(2)
char **p, a[16][8];?
問:p=a是否會導(dǎo)致程序在以后出現(xiàn)問題?為什么?
答案:這個不會導(dǎo)致出現(xiàn)問題,但是要注意p的使用,如a[1][2] 等價的為 *(*(p+1)+2)而不是*(p+11),
3.用遞歸方式,非遞歸方式寫函數(shù)將一個字符串反轉(zhuǎn).
?? 函數(shù)原型如下:char *reverse(char *str);
答案:
非遞歸方式:
char *reverse(char *str)
{
?int len = strlen(str);
?char temp;
?for(int i=0; i<len/2; i++)
?{
? temp = *(str+i);
? *(str+i) = *(str+len-1-i);
? *(str+len-1-i) = temp;
?}
?return str;
}
遞歸方式:???
4.strcpy函數(shù)和memcpy函數(shù)有什么區(qū)別?它們各自使用時應(yīng)該注意什么問題?
答案:strcpy是字符串拷貝,遇 '\0' 則停。??
?????????? memcpy是內(nèi)存拷貝,要指定拷貝的長度。??
?????????? 當(dāng)要拷貝二進(jìn)制數(shù)據(jù)(比如說一個結(jié)構(gòu)),只能用memcpy
5.寫一個函數(shù)將一個鏈表逆序.
一個單鏈表,不知道長度,寫一個函數(shù)快速找到中間節(jié)點的位置.
寫一個函數(shù)找出一個單向鏈表的倒數(shù)第n個節(jié)點的指針.(把能想到的最好算法寫出).
答案:
把一個鏈表中的接點順序倒排
typedef struct linknode
{
int data;
struct linknode *next;
}node;
//將一個鏈表逆置
node *reverse(node *head)
{
node *p,*q,*r;
p=head;
q=p->next;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=NULL;
head=p;
return head;
}
6.用遞歸算法判斷數(shù)組a[N]是否為一個遞增數(shù)組。
7.
有一個文件(名為a.txt)如下,每行有4項,第一項是他們的名次,寫一個c程序,將五個人的名字打印出來.并按名次排序后將5行數(shù)據(jù)仍然保存到a.txt中.使文件按名次排列每行.
2,07010188,0711,李鎮(zhèn)豪,
1,07010154,0421,陳亦良,
3,07010194,0312,凌瑞松,
4,07010209,0351,羅安祥,
5,07010237,0961,黃世傳,
8.寫一個函數(shù),判斷一個unsigned char 字符有幾位是1.
? 寫一個函數(shù)判斷計算機(jī)的字節(jié)存儲順序是升序(little-endian)還是降序(big-endian).
?9.微軟的筆試題.
Implement a string class in C++ with basic functionality like comparison, concatenation, input and output. Please also provide some test cases and using scenarios (sample code of using this class).
Please do not use MFC, STL and other libraries in your implementation.
10.有個數(shù)組a[100]存放了100個數(shù),這100個數(shù)取自1-99,且只有兩個相同的數(shù),剩下的98個數(shù)不同,寫一個搜索算法找出相同的那個數(shù)的值.(注意空間效率時間效率盡可能要低).
這十道題還是能夠看出自己的水平如何的.如果你能不假思索地做出這10道題,估計去國外大公司是沒有問題了,呵呵.
答案我在整理中,以后陸續(xù)發(fā)布.................
下面有些題也不錯,可以參考.
1.下面的代碼輸出是什么,為什么?
?????? void foo(void)
?????? {
??????????? unsigned int a = 6;
??????????? int b = -20;
??????????? (a+b>6)?puts(">6"):puts("<=6");//puts為打印函數(shù)
?????? }
輸出 >6.
就是考察隱式轉(zhuǎn)換.int型變量轉(zhuǎn)化成unsigned int, b成了正數(shù).
2. b)運(yùn)行下面的函數(shù)會有什么結(jié)果?為什么?
?????? void foo(void)
????????? {
?????????????? char string[10],str1[10];
?????????????? int i;
?????????????? for(i=0;i<10;i++)
?????????????? {
??????????????????? str1 = 'a';
?????????????? }
?????????????? strcpy(string, str1);
?????????? printf("%s",string);
????????? }
首先搞清strcpy函數(shù)的實現(xiàn)方法,
char * strcpy(char * strDest,const char * strSrc)
{
? if ((strDest == NULL) || (strSrc == NULL))
? throw "Invalid argument(s)";
? char * strDestCopy = strDest;
? while ((*strDest++ = *strSrc++) != '\0');
return strDestCopy;
}
由于str1末尾沒有‘\0’結(jié)束標(biāo)志,所以strcpy不知道拷貝到何時結(jié)束.
printf函數(shù),對于輸出char* 類型,順序打印字符串中的字符直到遇到空字符('\0')或已打印了由精度指定的字符數(shù)為止.
下面是微軟的兩道筆試題....
3. Implement a string class in C++ with basic functionality like comparison, concatenation, input and output. Please also provide some test cases and using scenarios (sample code of using this class).
Please do not use MFC, STL and other libraries in your implementation.
我的實現(xiàn)方案如下,這道題真地對c++的主要特性都進(jìn)行了較好地考察.
String.h:
#ifndef STRING_H
#define STRING_H
#include <iostream>
using namespace std;
class String{
?? public:
??? String();
?????? String(int n,char c);
??? String(const char* source);
??? String(const String& s);
??? //String& operator=(char* s);
??? String& operator=(const String& s);
??? ~String();
??? char& operator[](int i){return a;}
??? const char& operator[](int i) const {return a;}//對常量的索引.
??? String& operator+=(const String& s);
??? int length();
?? friend istream& operator>>(istream& is, String& s);//搞清為什么將>>設(shè)置為友元函數(shù)的原因.
?? //friend bool operator< (const String& left,const String& right);
?? friend bool operator> (const String& left, const String& right);//下面三個運(yùn)算符都沒必要設(shè)成友元函數(shù),這里是為了簡單.
?? friend bool operator== (const String& left, const String& right);
?? friend bool operator!= (const String& left, const String& right);
?? private:
??? char* a;
??? int size;
};
#endif
String.cpp:
#include "String.h"
#include <cstring>
#include <cstdlib>
String::String(){
??? a = new char[1];
??? a[0] = '\0';
??? size = 0;
}
String::String(int n,char c){
?a = new char[n + 1];
?memset(a,c,n);
?a[n] = '\0';
?size = n;
}
String::String(const char* source){
?if(source == NULL){
? a = new char[1];
? a[0] = '\0';
? size = 0;
?}
?else
?{?? size = strlen(source);
? a = new char[size + 1];
? strcpy(a,source);
?}
}
String::String(const String& s){
?size = strlen(s.a);//可以訪問私有變量.
?a = new char[size + 1];
?//if(a == NULL)
?strcpy(a,s.a);
}
?
String& String::operator=(const String& s){
?if(this == &s)
? return *this;
?else
?{
? delete[] a;
??????? size = strlen(s.a);
? a = new char[size + 1];
? strcpy(a,s.a);
? return *this;
?}
}
String::~String(){
?delete[] a;//????
}
String& String::operator+=(const String& s){
? int j = strlen(a);
? int size = j + strlen(s.a);
? char* tmp = new char[size+1];
? strcpy(tmp,a);
? strcpy(tmp+j,s.a);
?delete[] a;
?a = tmp;
?return *this;
?}
int String::length(){
?return strlen(a);
}
main.cpp:
#include <iostream>
#include "String.h"
using namespace std;
bool operator==(const String& left, const String& right)
{
?int a = strcmp(left.a,right.a);
??? if(a == 0)
? return true;
?else
? return false;
}
bool operator!=(const String& left, const String& right)
{
?return? !(left == right);
}
ostream& operator<<(ostream& os,String& s){
?int length = s.length();
?for(int i = 0;i < length;i++)
? //os << s.a;這么不行,私有變量.
? os << s;
?return os;
}
String operator+(const String& a,const String& b){
?String temp;
?temp = a;
?temp += b;
?return temp;
}
bool operator<(const String& left,const String& right){
?
?int j = 0;
?while((left[j] != '\0') && (right[j] != '\0')){
? if(left[j] < right[j])
?? return true;
? else
? {
?? if(left[j] == right[j]){
??? j++;
??? continue;
?? }
?? else
??? return false;
? }
?}
?if((left[j] == '\0') && (right[j] != '\0'))
? return true;
?else
? return false;
}
bool operator>(const String& left, const String& right)
{?? int a = strcmp(left.a,right.a);
??? if(a > 0)
? return true;
?else
? return false;
?
}
istream& operator>>(istream& is, String& s){
?delete[] s.a;
?s.a = new char[20];
?int m = 20;
??? char c;
?int i = 0;
?while (is.get(c) && isspace(c));
??? if (is) {
? do {s.a = c;
?????? i++;
??? /*if(i >= 20){
????? cout << "Input too much characters!" << endl;
????? exit(-1);
??? }*/
??? if(i == m - 1 ){
???? s.a = '\0';
???? char* b = new char[m];
???? strcpy(b,s.a);
???????????????? m = m * 2;
??????? s.a = new char[m];
???? strcpy(s.a,b);
???? delete[] b;
??? }
? }
? while (is.get(c) && !isspace(c));
??????? //如果讀到空白,將其放回.
? if (is)
?? is.unget();
?}
?s.size = i;
?s.a = '\0';
?return is;
}
int main(){
?String a = "abcd";
?String b = "www";
?//String c(6,b);這么寫不對.
??? String c(6,'l');
?String d;
?String e = a;//abcd
?String f;
?cin >> f;//需要輸入...
?String g;
?g = a + b;//abcdwww
?if(a < b)
? cout << "a < b" << endl;
?else
? cout << "a >= b" << endl;
?if(e == a)
? cout << "e == a" << endl;
?else
? cout << "e != a" << endl;
?
?b += a;
?
?cout << a << endl;
?cout << b << endl;
??? cout << c << endl;
?cout << d << endl;
?cout << e << endl;
?cout << f << endl;
?cout << g << endl;
?cout << g[0] << endl;
?return 0;
}
?
?
4. Implement a single-direction linked list sorting algorithm. Please first define the data structure of linked list and then implement the sorting algorithm.
?
5.編寫一個函數(shù),返回兩個字符串的最大公串!例如,“adbccadebbca”和“edabccadece”,返回“ccade”
轉(zhuǎn)載于:https://www.cnblogs.com/heyonggang/p/3182309.html
總結(jié)
- 上一篇: 监控调优工具详细参数整理
- 下一篇: hdu 4419 Colourful R