2015年趋势科技笔试A卷
35题:C
#include<iostream> #include<vector> #include<string> #include<algorithm> std::string foo(const std::string& s) { std::string ret; ret.reserve(s.size()); for(size_ti=0;i<s.size();++i) { ret.push_back(s[i]|0x20); } return ret; } bool compare_string(const std::string& lhs,const std::string &rhs) { returnfoo(lhs)<foo(rhs); } int main(int argc,constchar* argv[]) { constchar* data[]={"Have","fun","in","Trendmicro"}; std::vector<std::string>vec(data,data+sizeof(data)/sizeof(data[0])); std::sort(vec.begin(),vec.end(),compare_string); std::copy(vec.begin(),vec.end(),std::ostream_iterator<std::string>(std::cout," ")); std::cout<<std::endl; return 0; }34:BDE(下面是改动后的代码)
#include<stdio.h> #include<exception> #include<stdlib.h> template<unsigned int size> class CTestObj { private: int *m_pvalue; public: CTestObj(int arr[size]) { m_pvalue=new int; *m_pvalue=0; for(unsigned int i=0;i<size;i++) { *m_pvalue+=arr[i]; } } ~CTestObj() { delete m_pvalue; } voidTest() { if(*m_pvalue>100) { throw std::exception(); } } }; int main(int argc,const char* argv[]) { try { int arr[]={1,2,3,4,500}; CTestObj<_countof(arr)> *pobj =new CTestObj<_countof(arr)>(arr); pobj->Test(); delete pobj; } catch(std::exception) { printf("exception\n"); // delete pobj; } printf("end\n"); return 0; }9:A(供參考)
#include<stdio.h> #include<iostream> usingnamespace std; #define BUFFER_SIZE 12 /*void Copydata(const char *imgdata,int width,int height) { charbuf[BUFFER_SIZE]; if(width*height>BUFFER_SIZE) return; const char*p=imgdata; for(inti=0;i<width;++i){ for(intj=0;j<height;++j){ buf[i*width+j]=*p++; } } cout<<buf<<endl; } void Copydata(const char *data,int len) { if(len>BUFFER_SIZE) return; charbuf[BUFFER_SIZE]; memcpy(buf,data,len); cout<<buf<<endl; }*/ void Copydata(const char *imgdata,int width,int height) { char *buf=(char *)malloc(width*height); if(NULL==buf)return; memcpy(buf,imgdata,width*height); cout<<buf<<endl; free(buf); } int main(int argc,const char* argv[]) { char *p="abcdefghijkl"; Copydata(p,3,4); return 0; }8:BC
#include<stdio.h> #include<iostream> usingnamespace std; int main(int argc,constchar* argv[]) { intarr[3][3]={{1,2,3},{4,5,6},{7,8,9}}; inti=1,j=2; cout<<**(arr+3*i+j)<<endl; cout<<*(*(arr+i)+j)<<endl; cout<<*(arr[i]+j)<<endl; cout<<*(arr+3*i+j)<<endl; return 0; }
7:A
#include<stdio.h> #include<iostream> using namespace std; void check() { unsigned int i=1; // char *cptr =(char *)i; char *cptr= (char *)&i; // char*cptr=&(char *)i; // char cptr =(char)&i; if(*cptr!=0) printf("1"); else printf("0"); } int main(int argc,const char* argv[]) { check(); return 0; }
6:BC
#include <stdio.h> #include <iostream> using namespace std; void f(char *s,char *t) { do{ *s++=*t++; }while(*t!='\0'); *s='\0'; // cout<<s<<endl; } void f(char *s,char *t) { while(*t){ *s++=*t++; } *s='\0'; cout<<s<<endl; } void f(char *s,char *t) { do { *s=*t; s++; }while(*t++); // cout<<s<<endl; } void f(char *s,char *t) { unsigned i; for(i=0;i<strlen(t);i++) s[i]=t[i]; s[i]='\0'; } int main(int argc,const char* argv[]) { char *t="abcde"; char *s=(char *)malloc(10); f(s,t); cout<<s<<endl; return 0; }
4:D
//假设基类对象指向子类对象的话 ,而且此函数是虚函数的时候。基类对象调用子类中定义的此函数。 #include<stdio.h> #include<iostream> usingnamespace std; class Base{ int x; public: Base(){x=1;} virtualvoid display(){ cout<<x; } void show(){ cout<<x; } }; class Derived:public Base{ int y; public: Derived(){y=2;} voiddisplay() { cout<<y; } void show(){ cout<<y; } }; int main() { Base b; Derived d; b.display(); d.display(); Base *p=&d; p->display(); b.show(); d.show(); p->show(); return 0; }