1.有一个16位的整数,每4位为一个数,写函数求他们的和。
char sum(unsigned short n){
char c = 0;
int i = 4;
do
{
c += n & 15;
n = n>>4;
} while (i--);
return c;
}
2, 判断一个字符串是否是回文,如果是,返回字符串"yes" 字符串"no" .
char * huiwen(char * str){
int n = strlen(str);
int mid = n/2, i=0;
while(mid--){
if(*(str+i) != *(str+n-1-i))
return "no";
i++;
}
return "yes";
}
3, 写一个函数比较两个字符串 str1 和 str2 的大小,若相等返回 0,若 str1 大于 str2 返回 1,若 str1 小于 str2 返回-1,不调用 C /C 的字符串的字符库函数, 请编写函数 strcmp,函数定义为:int strcmp(const char*src,const char*dst)
int strcmp(const char* src, const char* dst){
int result;
assert(src && dst);
while((result = *src - *dst) == 0){
src++;
dst++;
if(*(src-1) == '\0')
return 0;
}
if(result > 0) return 1;
else return -1;
}
4, 函数 sun(int a [],int n)的返回值是数组 a[]的前几个元素之和. 请使用递归的非递归两种方式实现.
int sun(const int a[], int n){
if(n <= 0) return 0;
else return (sun(a, n-1) + a[n-1]);
}
int sun(const int a[], int n){
int result = 0;
while(n--){
result += a[n];
}
return result;
}
5.给定一个长度为n的整数数组,只允许用乘法,不允许用除法,计算(n-1)个数的组合乘积中最大的一组,计算时空复杂度。
int foo(int arr[], int n){
int max = numeric_limits<int>::min();
for(int i=0; i<n; i++){
int product = 1;
for(int j=0; j<n; j++){
if(j != i)
product *= arr[j];
}
max = product > max ? product : max;
}
return max;
}
//时间复杂度:n*((n+(n-1)+1)) = 2n^2, 即O(n^2);
//空间复杂度:4 * sizeof(int), 即O(1);
6.寻找一个单链表的中项,如果有2个则返回前面一个。参考:struct Node{Node *next;}; Node* someFunc(Node* head);
struct Node{
Node* next;
};
Node * someFunc(Node * head){
if(head == NULL)
return NULL;
Node* quickNode = head;
Node* slowNode = head;
while(quickNode != NULL && quickNode->next != NULL){
quickNode = quickNode->next->next;
slowNode = slowNode->next;
}
return slowNode;
}
7.给定字符串A和B,输出A和B中的最大公共子串。比如A=”aocdfe” B=”pmcdfa” 则输出”cdf”。
//给定字符串A和B,输出A和B中的最大公共子串。比如A=”aocdfe” B=”pmcdfa” 则输出”cdf”
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char* commanstring(char shortstring[], char longstring[])
{
int i, j;
char* substring = (char*)malloc(256);
if(strstr(longstring, shortstring) != NULL)
return shortstring;
for(i=strlen(shortstring)-1; i>0; i--)
{
for(j=0; j<=strlen(shortstring)-i; j++)
{
memcpy(substring, &shortstring[j], i);
substring[i] = '\0';
printf("%s\n", substring);
if(strstr(longstring, substring) != NULL)
return substring;
}
}
return NULL;
}
int main()
{
char* str1 = "aocdfe";
char* str2 = "pmcdfa";
char *comman = NULL;
if(strlen(str1) > strlen(str2))
comman = commanstring(str2, str1);
else
comman = commanstring(str1, str2);
printf("%s\n", comman);
system("PAUSE");
return 0;
}
8.输入字符串,长度小于200,只包含数字和字母。输出:删除输入的字符串中出现次数最多的字符,如果存在多,则都删除。例如:输入aa1111bbccccd,输出aabbd。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* FilterString(const char* src)
{
const char* pstart = src;
char* output = (char*)malloc(sizeof(char)*200);
memset(output, 0, 200);
int max_len = 1;
while(*pstart)
{
int step = 1;
while(*(pstart+step) == *pstart)
{
step++;
}
if(step > max_len)
max_len = step;
pstart++;
}
char* pfill = output;
while(*src)
{
const char* pmove = src + 1;
int step = 1;
while(*pmove == *src)
{
pmove++;
step ++;
}
if(step == max_len)
src += max_len;
else
{
*pfill = *src;
src++;
pfill++;
}
}
return output;
}
int main()
{
char input[200] = "";
while(1)
{
scanf("%s", input);
char* output = FilterString(input);
printf("%s\n", output);
free(output);
}
return 0;
}
9.下列输出什么?
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
class A
{
public:
A()
{
printf("default ctr\n");
}
A(const A& other)
{
printf("copy ctr\n");
}
A& operator=(const A& other)
{
printf("operator\n"); return *this;
}
void swap(A& other)
{
printf("inner swap");
}
};
void swap(A& left, A& right)
{
left.swap(right);
}
void myswap(std::vector<A> &left, std::vector<A> &right)
{
std::vector<A> temp(left);
left = right;
right = temp;
}
int main(int argc, char **argv)
{
std::vector<A> v1(1);
std::vector<A> v2(1);
myswap(v1, v2);
std::swap(v1, v2);
return 0;
}
default ctr
copy ctr
default ctr
copy ctr
copy ctroperator
operator
7.写出下列程序的输出结果。
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
class A
{
public:
int value;
A() {value = 1;}
A(int t) {value = t;}
virtual void Read(){printf("A value = %d\n", value);}
virtual void doRead() {Read();}
};
class B : public A
{
public:
int value;
B() {value = 2;}
B(int t) {value = t;}
virtual void Read() {printf("B value = %d\n", value);}
virtual void doRead() {Read();}
};
class C : public B
{
public:
int value;
C() {value = 3;}
C(int t) {value = t;}
virtual void Read() {printf("C value = %d\n", value);}
virtual void doRead() {Read();}
};
int main(int argc, char **argv)
{
C c(10);
c.Read();
c.A::Read();
c.B::Read();
c.C::Read();
c.doRead();
c.A::doRead();
c.B::doRead();
c.C::doRead();
return 0;
}