C++笔试题汇总(5)

//上海贝尔的面试题43 分即可进入复试
一、请填写BOOL , float, 指针变量与“零值”比较的if 语句。(10 分)
提示:这里“零值”可以是0, 0.0 , FALSE 或者“空指针”。例如int 变量n 与“零值”比较的if 语句
为:
if ( n == 0 )
if ( n != 0 )
以此类推。
请写出BOOL flag 与“零值”比较的if 语句:
请写出float x 与“零值”比较的if 语句:
请写出char *p 与“零值”比较的if 语句:
二、以下为Windows NT 下的32 位C++程序,请计算sizeof 的值(10 分)
char str[] = “Hello” ;
char *p = str ;
int n = 10;
请计算
sizeof (str ) =
sizeof ( p ) =
sizeof ( n ) =void Func ( char str[100])
{
请计算
sizeof( str ) =
}
void *p = malloc( 100 );
请计算
sizeof ( p ) =
、简答题(25 分)
1、头文件中的ifndef/define/endif 干什么用?
2、#i nclude 和#i nclude “filename.h” 有什么区别?
3、const 有什么用途?(请至少说明两种)
4、在C++ 程序中调用被C 编译器编译后的函数,为什么要加extern “C”声明?
5、请简述以下两个for 循环的优缺点
// 第一个
for (i=0; i{
if (condition)
DoSomething();
else
DoOtherthing();
}// 第二个
if (condition)
{
for (i=0; i DoSomething();
}
else
{
for (i=0; i DoOtherthing();
优点:N 次中,每次都要对condition 进行判断
缺点: 优点:一次判断condition 后,对something 或Otherthing 执行N 次
缺点:
四、有关内存的思考题(20 分)
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
请问运行Test 函数会有什么样的结果?
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
请问运行Test 函数会有什么样的结果?
Void GetMemory2(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
请问运行Test 函数会有什么样的结果?
void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str, “hello”);
free(str);
if(str != NULL)
{
strcpy(str, “world”);
printf(str);
}
}
请问运行Test 函数会有什么样的结果?
五、编写strcpy 函数(10 分)
已知strcpy 函数的原型是
char *strcpy(char *strDest, const char *strSrc);
其中strDest 是目的字符串,strSrc 是源字符串。
(1)不调用C++/C 的字符串库函数,请编写函数strcpy
char *strcpy(char *strDest,const char *strSrc){
int n=0;
while(strSrc[n]!=NULL){
n++:
}
*srtDest=new char[n];
for(int i=0;i strDest=strSrc[i];
i++;
}
return *strDest;
}
(2)strcpy 能把strSrc 的内容复制到strDest,为什么还要char * 类型的返回值?
因为该函数的还可以把复制的字符串首地址指针给其他的指针,而且这种需要也是有用的。
六、编写类String 的构造函数、析构函数和赋值函数(25 分)
已知类String 的原型为:
class String
{
public:
String(const char *str = NULL);// 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & operate =(const String &other);// 赋值函数
private:
char *m_data;// 用于保存字符串
};
请编写String 的上述4 个函数。
//答案一并给出
一、请填写BOOL , float, 指针变量与“零值”比较的if 语句。(10 分)
请写出BOOL flag 与“零值”比较的if 语句。(3 分)
标准答案:
if ( flag )
if ( !flag )如下写法均属不良风格,不得分。
if (flag == TRUE)
if (flag == 1 )
if (flag == FALSE)
if (flag == 0)
请写出float x 与“零值”比较的if 语句。(4 分)
标准答案示例:
const float EPSINON = 0.00001;
if ((x >= - EPSINON) && (x <= EPSINON)
不可将浮点变量用“==”或“!=”与数字比较,应该设法转化成“>=”或“<=”此类形式。
如下是错误的写法,不得分。
if (x == 0.0)
if (x != 0.0)
请写出char *p 与“零值”比较的if 语句。(3 分)
标准答案:
if (p == NULL)
if (p != NULL)如下写法均属不良风格,不得分。
if (p == 0)
if (p != 0)
if (p)
if (!)
二、以下为Windows NT 下的32 位C++程序,请计算sizeof 的值(10 分)
char str[] = “Hello” ;
char *p = str ;
int n = 10;
请计算
sizeof (str ) = 6 (2 分)
sizeof ( p ) = 4 (2 分)
sizeof ( n ) = 4 (2 分)void Func ( char str[100])
{
请计算
sizeof( str ) = 4 (2 分)
}
void *p = malloc( 100 );
请计算
sizeof ( p ) = 4 (2 分)
三、简答题(25 分)
1、头文件中的ifndef/define/endif 干什么用?(5 分)
答:防止该头文件被重复引用。
2、#i nclude 和#i nclude “filename.h” 有什么区别?(5 分)
答:对于#i nclude ,编译器从标准库路径开始搜索filename.h
对于#i nclude “filename.h” ,编译器从用户的工作路径开始搜索filename.h
3、const 有什么用途?(请至少说明两种)(5 分)
答:(1)可以定义const 常量
(2)const 可以修饰函数的参数、返回值,甚至函数的定义体。被const 修饰的东西都受到强制保
护,可以预防意外的变动,能提高程序的健壮性。
4、在C++ 程序中调用被C 编译器编译后的函数,为什么要加extern “C”? (5 分)
答:C++语言支持函数重载,C 语言不支持函数重载。函数被C++编译后在库中的名字与C 语言的
不同。假设某个函数的原型为: void foo(int x, int y);
该函数被C 编译器编译后在库中的名字为_foo,而C++编译器则会产生像_foo_int_int 之类的名
字。
C++提供了C 连接交换指定符号extern“C”来解决名字匹配问题。
5、请简述以下两个for 循环的优缺点(5 分)
for (i=0; i{
if (condition)
DoSomething();
else
DoOtherthing();
}if (condition)
{
for (i=0; i DoSomething();
}
else
{
for (i=0; i DoOtherthing();
}
优点:程序简洁
缺点:多执行了N-1 次逻辑判断,并且打断了循环“流水线”作业,使得编译器不能对循环进行优化
处理,降低了效率。优点:循环的效率高
缺点:程序不简洁
四、有关内存的思考题(每小题5 分,共20 分)
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
请问运行Test 函数会有什么样的结果?
答:程序崩溃。
因为GetMemory 并不能传递动态内存,
Test 函数中的str 一直都是NULL。
strcpy(str, "hello world");将使程序崩溃。
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
请问运行Test 函数会有什么样的结果?
答:可能是乱码。
因为GetMemory 返回的是指向“栈内存”的指针,该指针的地址不是NULL,但其原现的内容已经
被清除,新内容不可知。
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
请问运行Test 函数会有什么样的结果?
答:
(1)能够输出hello
(2)内存泄漏
void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str, “hello”);
free(str);
if(str != NULL)
{
strcpy(str, “world”);
printf(str);
}
}
请问运行Test 函数会有什么样的结果?
答:篡改动态内存区的内容,后果难以预料,非常危险。
因为free(str);之后,str 成为野指针,
if(str != NULL)语句不起作用。

27 费波那其数列,1,1,2,3,5……编写程序求第十项。可以用递归,也可以用其他方法,但要
说明你选择的理由。
-------------------------------------------------------------------------------------
#i nclude
#i nclude
int Pheponatch(int);
int Pheponatch2(int);
int main()
{
printf("The 10th is %d",Pheponatch2(20));
system("pause");
return 0;
}
//递归算法
int Pheponatch(int N)
{
if( N == 1 || N == 2)
{
return 1;
}
else
return Pheponatch( N -1 ) + Pheponatch( N -2 );
}
//非递归算法
int Pheponatch2(int N)
{
int x = 1, y = 1, temp;
int i = 2;
while(true)
{
temp = y;
y = x + y;
x = temp;
i++;
if( i == N )
break;
}
return y;
}
25.完成下列程序
*
*.*.
*..*..*..
*...*...*...*...
*....*....*....*....*....
*.....*.....*.....*.....*.....*.....
*......*......*......*......*......*......*......
*.......*.......*.......*.......*.......*.......*.......*.......
#i nclude
#define N 8
int main()
{
int i;
int j;
int k;
---------------------------------------------------------
| |
| |
| |
---------------------------------------------------------
return 0;
}
#i nclude
#i nclude
#define N 8
int main()
{
int i;
int j;
int k;
for(i=N; i>=1; i--)
{
for(j=0; j {
cout<<"*";
for(k=1; k cout<<".";
}
cout<<"/n";
}
return 0;
}
"28 下列程序运行时会崩溃,请找出错误并改正,并且说明原因。"
// void append(int N) ;
//指针没有初始化:
//NewNode->left=NULL;
//NewNode->right=NULL;
#i nclude
#i nclude
typedef struct TNode{
TNode* left;
TNode* right;
int value;
} TNode;
TNode* root=NULL;
void append(int N);
int main()
{
append(63);
append(45);
append(32);
append(77);
append(96);
append(21);
append(17); // Again, 数字任意给出
return 0;
}
void append(int N)
{
TNode* NewNode=(TNode *)malloc(sizeof(TNode));
NewNode->value=N;
NewNode->left=NULL;
NewNode->right=NULL;
if(root==NULL)
{
root=NewNode;
return;
}
else
{
TNode* temp;
temp=root;
while((N>=temp->value && temp->left!=NULL) || (Nvalue && temp->right!=NULL))
{
while(N>=temp->value && temp->left!=NULL)
temp=temp->left;
while(Nvalue && temp->right!=NULL)
temp=temp->right;
}
if(N>=temp->value)
temp->left=NewNode;
else
temp->right=NewNode;
return;
}
}
算法:
1.什么是NPC,NP-Hard?
2.起泡排序的时间复杂度是多少?
说出至少一个比它更快的算法;
排序的极限时间复杂度是多少?
3.有一个链表,如何判断它是一个循环链表?
如果链表是单向的呢?
如果出现循环的点可能在任意位置呢?
如果缓存空间是有限的,比如是一个常数呢?
如果只能使用2 个缓存呢?
4.有一个文件,保存了若干个整数,如何以平均的概率随机得到其中的一个整数?
如果整数的个数是未知的呢?
如果整数是以字符串形式存放,如:(即如何得到随机的一个字符串)
123
-456

如果只允许便历文件一次呢?
5.用两组数据,都在内存中,对它们排序分别需要1 和2 分钟;那么使用两个线程一起排序,大概
需要多少时间?
C/C++:
1.C 与C++的异同,优劣;
2.C,C++,VC,BC,TC 的区别;
3.C++中try…catch 关键字的用法与优点;
4.枚举的用法,以及它与宏的区别;
5.const 的用法,以及声明const 变量与宏的区别;
const 的用法有四种:
区别:const 常量有数据类型, 而宏常量没有数据类型。编译器可以对前者进行类型安全检查,而
对后者只能进行字符替换,没有类型
安全检查。而且字符替换可能会带来料想不到的边界效应。
有些集成化工具可以对const 常量进行调试, 但不能对宏量进行调试。
6.C++中引用与指针的区别;
答:1 引用实际上是所引用的对象或变量的别名,而指针是包含所指向对象或变量的地址的变量。
2 引用在定义时必须初始化,而指针在定义时不初始化。
3 不可以有努NULL 的引用,而可以有指向NULL 的指针。
4 引用在初始化后不可以改变引用关系,而指针可以随时指向其他对象(非const 指针)。
7.C++中virtual 与inline 的含义分别是什么?
答:在基类成员函数的声明前加上virtual 关键字,意味着将该成员函数声明为虚函数。
inline 与函数的定义体放在一起,使该函数称为内联。inline 是一种用于实现的关键字,而不是用于
声明的关键字。
虚函数的特点;如果希望派生类能够重新定义基类的方法,则在基类中将该方法定义为虚方法,这样
可以启用动态联编。
内联函数的特点;使用内联函数的目的是为了提高函数的运行效率。内联函数体的代码不能过长,因
为内联函数省去调用函数
的时间是以代码膨胀为代价的。内联函数不能包含循环语句,因为执行循环语句要比调用函数的开销
大。
一个函数能否即是虚函数又是内联函数?
8.以下关键字的含义与用法:
extern,extern “C”,static,explicit,register,#undef,#ifndef
9.什么是函数重载与覆盖?
为什么C 不支持函数重载?
为什么C++能支持函数重载?
10.VC 中,编译工具条内的Debug 与Release 选项是什么含义?
11.编写my_memcpy 函数,实现与库函数memcpy 类似的功能,不能使用任何库函数;
void* mymemcpy(void* pvTo, const char* pvFrom, size_t size)
{
assert((dest != NULL) && (src != NULL));
byte* psTo = (byte*)pvTo;
byte* psFrom = (byte*)pvFrom;
while (size-- > 0)
{
*psTo++ = *psFrom++;
}
return pvTo;
}
12.编写my_strcpy 函数,实现与库函数strcpy 类似的功能,不能使用任何库函数;
答:char* my_strcpy(char* strdest, const char* strsrc)
{
assert(strdest != NULL) && (strsrc != NULL))
char* address = strdest;
while((*strdest++ = *strsrc++) != NULL)
return address;
}
13.编写gbk_strlen 函数,计算含有汉字的字符串的长度,汉字作为一个字符处理;
已知:汉字编码为双字节,其中首字节<0,尾字节在0~63 以外;(如果一个字节是-128~127)
14.函数assert 的用法?
答:断言assert 是仅在debug 版本起作用的宏,用于检查“不应该“发生的情况。程序员可以把assert
看成一个
在任何系统状态下都可以安全使用的无害测试手段。
15.为什么在头文件的最前面都会看到这样的代码:
#ifndef _STDIO_H_
#define _STDIO_H_
16.为什么数组名作为参数,会改变数组的内容,而其它类型如int 却不会改变变量的值?
答:当数组名作为参数时,传递的实际上是地址。而其他类型如int 作为参数时,由于函数参数值实
质上是实参的一份拷贝,被调
函数内部对形参的改变并不影响实参的值。
1.实现双向链表删除一个节点P,在节点P 后插入一个节点,写出这两个函数。
2.写一个函数,将其中的/t 都转换成4 个空格。
3.Windows 程序的入口是哪里?写出Windows 消息机制的流程。
4.如何定义和实现一个类的成员函数为回调函数?
5.C++里面是不是所有的动作都是main()引起的?如果不是,请举例。
6.C++里面如何声明const void f(void)函数为C 程序中的库函数?
7.下列哪两个是等同的
int b;
A const int* a = &b;
B const* int a = &b;
C const int* const a = &b;
D int const* const a = &b;
8.内联函数在编译时是否做参数类型检查?
void g(base & b){
b.play;
}
void main(){
son s;
g(s);
return;
}
3、WinMain
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
MSRA Interview Written Exam(December 2003,Time:2.5 Hours)
1 写出下列算法的时间复杂度。
(1)冒泡排序;
(2)选择排序;
(3)插入排序;
(4)快速排序;
(5)堆排序;
(6)归并排序;
2 写出下列程序在X86 上的运行结果。
struct mybitfields
{
unsigned short a : 4;
unsigned short b : 5;
unsigned short c : 7;
}test
void main(void)
{
int i;
test.a=2;
test.b=3;
test.c=0;
i=*((short *)&test);
printf("%d/n",i);
}
3 写出下列程序的运行结果。
unsigned int i=3;
cout<
4 写出下列程序所有可能的运行结果。
int a;
int b;
int c;
void F1()
{
b=a*2;
a=b;
}
void F2()
{
c=a+1;
a=c;
}
main()
{
a=5;
//Start F1,F2 in parallel
F1(); F2();
printf("a=%d/n",a);
}
5 考察了一个CharPrev()函数的作用。
6 对16 Bits colors 的处理,要求:
(1)Byte 转换为RGB 时,保留高5、6bits;
(2)RGB 转换为Byte 时,第2、3 位置零。
7 一个链表的操作,注意代码的健壮和安全性。要求:
(1)增加一个元素;
(2)获得头元素;
(3)弹出头元素(获得值并删除)。
8 一个给定的数值由左边开始升位到右边第N 位,如
0010<<1 == 0100
或者
0001 0011<<4 == 0011 0000
请用C 或者C++或者其他X86 上能运行的程序实现。
附加题(只有在完成以上题目后,才获准回答)
In C++, what does "explicit" mean? what does "protected" mean?
2、解释1NF、2NF、3NF、BCNF
第一范式(1NF)是指数据库表的每一列都是不可分割的基本数据项,同一列中不能有多个值,即实体中
的某个属性不能有多个值或者不能有重复的属性。如果出现重复的属性,就可能需要定义一个新的实体,
新的实体由重复的属性构成,新实体与原实体之间为一对多关系;简而言之,第一范式就是无重复的列。
第二范式(2NF)是在第一范式(1NF)的基础上建立起来的,即满足第二范式(2NF)必须先满足第一范
式(1NF)。第二范式(2NF)要求数据库表中的每个实例或行必须可以被惟一地区分。为实现区分通常需
要为表加上一个列,以存储各个实例的惟一标识。
满足第三范式(3NF)必须先满足第二范式(2NF)。简而言之,第三范式(3NF)要求一个数据库表中不
包含已在其它表中已包含的非主关键字信息。
在第三范式的基础上,数据库表中如果不存在任何字段对任一候选关键字段的传递函数依赖则符合BCNF 。
3、如何存储稀疏矩阵
好几种吧,还要看矩阵的类型
4、快排序在最好,最坏,平均情况下的时间复杂度与辅助空间复杂度
最好复杂度O(nlog2n) 最坏O(n^2) 平均O(nlog2n)
空间:最好log2n 最坏n 平均log2^n
1、不用任何变量交换a,b 两个变量
(看过...那个加减法吧)
2、用递归求最大公约数
#include
using namespace std;
// 求两个整型数的最大公约数
int gcd( int a, int b )
{
int temp = 2; // 公因子,从2 开始递增直到两个数中最小的一个
while (temp <= (a < b ? a : b))
{
if (a % temp == 0 && b % temp == 0)
{ // 都能被整除时递归
return temp * gcd( a / temp, b / temp );
}
else
{ // 有一个不能被整除则公因子加一
temp++;
}
}
return 1; // 最大公因子为1
}
3、举一个多态的例子
(多态...是不是必须要有指针才行啊?)
4、二叉平衡树
(。。。)
5、UNIX 进程包括那三个部分:...
(程序,数据和进程控制块PCB)
6、new 动态分配失败会抛出什么异常,C++中提供了那两个标准函数来设定异常处理HANLDER
(不知道)
7、EJB 都有那些Beans?区别
(....)
8、asp 和asp.net 的区别
(除了编译和解释外...)
9、JAVA 中的interface 和abstract class 区别
(.....)
10、logic thinking:如何证明一个电冰箱是否是好的。
(.....)
内联的定义,什么情况下选择内联。内连接与外连接的区别。
...
===========
1、100=9 9 9 9 9 9
9×9+9+9+9/9
2、根据pseudo code 判断下列哪个x 打印"Text 2"
If (x>4) then print "Text 1"
Else if (x>9) then print "Text 2"
Else print "Text 3")
(1)less than 0,(2)less than 4,(3)between 4 and 9,(4)>9,(5)none
5 吧
3、填bug report:a browser based software crashes when you type "-1" in a input field called ABC on
secong html page after loggong on on WindowXP platfor
m. And this happens every time you try typing "-1".You are not sure if any d
atabase servers are being used or not.
┏━━━━━━━━━━━━━━━━
┃ Serverity:┃ ┃Priority:┃ ┃
━━━━━━━━━━━━━━━━━
┃ ━━━━━━━━━━━━━━━━━━━┃
┃ Short Description┃ ┃
┃ ━━━━━━━━━┃━━━━━━━━━┃
┃ Reproducible? ┃ ┃
┃ ━━━━━━━━━┃━━━━━━━━━┃
┃ Recreate steps: ┃ ┃
┃ ━━━━━━━━━┃━━━━━━━━━┃
┃ Attachment: ┃ ┃
━━━━━━━━━━━━━━━━━━━━┃
┃ ━━━━━━━━━━━━━━━━━━━┃
┃ platform tested: ┃ ┃
┃ ━━━━━━━━━━━━━━━━━━━┃
┃ database server: ┃ ┃
┃ ━━━━━━━━━━━━━━━━━━━┃
┃ browser : ┃ ┃
━━━━━━━━━━━━━━━━━━━━┃
4、添加注释
//
//
//
//
private static final int SHORT_COLUMN_WIDTH=5;
private static final int INT_COLUMN_WIDTH=10;
private static final int LONG_COLUMN_WIDTH=19;
private static final int DOUBLE_COLUMN_WIDTH=23;
……
int decimalDigits=DataTypeInfo.getScale();
int widthOfNumber=DataTypeInfo.getPrecision();
int dataType=DataTypeInfo.numberValue;
//
//
//
if (decimalDigits==0){
if (widthOfNumber<=SHORT_COLUMN_WIDTH) {dataType=dataTypeInfo.signedInt16Value;}
else if (widthOfNumber<=INT_COLUMN_WIDTH) {dataType=dataTypeInfo.signedInt32Value;}
else if (widthOfNumber<=LONG_COLUMN_WIDTH) {dataType=dataTypeInfo.signedInt64Value;}
else if (widthOfNumber<=DOUBLE_COLUMN_WIDTH) {dataType=dataTypeInfo.numberValue;}
else {dataType=dataTypeInfo.numberValue;}
5、设计一个算法判断一个字符串是否是回文,并写出代码。
"A man a plan a canal panama"是回文。
6、定义函数,给出三个参数,从字符串inputString 中的下标indexOfChar 开始返回noOfChar 个字符。要
求找出尽可能多的错误情形
7、编一段代码,求两个int 的最大公约数
8、给出一段c++代码(关于构建器和虚析构器的调用问题),要求(1)其输出,(2)说明virtual destructor
的作用(role)
9、英文阅读理解。
写一个程序, 要求功能:求出用1,2,5 这三个数不同个数组合的和为100 的组合个数。
如:100 个1 是一个组合,5 个1 加19 个5 是一个组合。。。。请用C++语言写。
答案:最容易想到的算法是:
设x 是1 的个数,y 是2 的个数,z 是5 的个数,number 是组合数注意到0<=x<=100,0<=y<=50,0<=z=20,
所以可以编程为:
number=0;
for (x=0; x<=100; x++)
for (y=0; y<=50; y++)
for (z=0; z<=20; z++)
if ((x+2*y+5*z)==100)
number++;
cout< <
上面这个程序一共要循环100*50*20 次,效率实在是太低了事实上,这个题目是一道明显的数学问题,而
不是单纯的编程问题。我的解法如下:
因为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
因此,组合总数为100 以内的偶数+95 以内的奇数+90 以内的偶数+...+5 以内的奇数+1,
即为:
(51+48)+(46+43)+(41+38)+(36+33)+(31+28)+(26+23)+(21+18)+(16+13)+(11+8)+(6+3)+1
某个偶数m 以内的偶数个数(包括0)可以表示为m/2+1=(m+2)/2
某个奇数m 以内的奇数个数也可以表示为(m+2)/2
所以,求总的组合次数可以编程为:
number=0;
for (int m=0;m<=100;m+=5)
{
number+=(m+2)/2;
}
cout< <
这个程序,只需要循环21 次, 两个变量,就可以得到答案,比上面的那个程序高效了许多
倍----只是因为作了一些简单的数学分析
这再一次证明了:计算机程序=数据结构+算法,而且算法是程序的灵魂,对任何工程问
题,当用软件来实现时,必须选取满足当前的资源限制,用户需求限制,开发时间限制等种
种限制条件下的最优算法。而绝不能一拿到手,就立刻用最容易想到的算法编出一个程序了
事——这不是一个专业的研发人员的行为。
那么,那种最容易想到的算法就完全没有用吗?不,这种算法正好可以用来验证新算法
的正确性,在调试阶段,这非常有用。在很多大公司,例如微软,都采用了这种方法:在调
试阶段,对一些重要的需要好的算法来实现的程序,而这种好的算法又比较复杂时,同时用
容易想到的算法来验证这段程序,如果两种算法得出的结果不一致(而最容易想到的算法保
证是正确的),那么说明优化的算法出了问题,需要修改。
可以举例表示为:
#ifdef DEBUG
int simple();
#end if
int optimize();
......
in a function:
{
result=optimize();
ASSERT(result==simple());
}
这样,在调试阶段,如果简单算法和优化算法的结果不一致,就会打出断言。同时,在程
序的发布版本,却不会包含笨重的simple()函数。——任何大型工程软件都需要预先设计良
好的调试手段,而这里提到的就是一种有用的方法。
一个学生的信息是:姓名,学号,性别,年龄等信息,用一个链表,把这些学生信息连在一起, 给出一个
age, 在些链表中删除学生年龄等
于age 的学生信息。
#include "stdio.h"
#include "conio.h"
struct stu{
char name[20];
char sex;
int no;
int age;
struct stu * next;
}*linklist;
struct stu *creatlist(int n)
{
int i;
//h 为头结点,p 为前一结点,s 为当前结点
struct stu *h,*p,*s;
h = (struct stu *)malloc(sizeof(struct stu));
h->next = NULL;
p=h;
for(i=0;i
{
s = (struct stu *)malloc(sizeof(struct stu));
p->next = s;
printf("Please input the information of the student: name sex no age /n");
scanf("%s %c %d %d",s->name,&s->sex,&s->no,&s->age);
s->next = NULL;
p = s;
}
printf("Create successful!");
return(h);
}
void deletelist(struct stu *s,int a)
{
struct stu *p;
while(s->age!=a)
{
p = s;
s = s->next;
}
if(s==NULL)
printf("The record is not exist.");
else
{
p->next = s->next;
printf("Delete successful!");
}
}
void display(struct stu *s)
{
s = s->next;
while(s!=NULL)
{
printf("%s %c %d %d/n",s->name,s->sex,s->no,s->age);
s = s->next;
}
}
int main()
{
struct stu *s;
int n,age;
printf("Please input the length of seqlist:/n");
scanf("%d",&n);
s = creatlist(n);
display(s);
printf("Please input the age:/n");
scanf("%d",&age);
deletelist(s,age);
display(s);
return 0;
}
2、实现一个函数,把一个字符串中的字符从小写转为大写。
#include "stdio.h"
#include "conio.h"
void uppers(char *s,char *us)
{
for(;*s!='/0';s++,us++)
{
if(*s>='a'&&*s<='z')
*us = *s-32;
else
*us = *s;
}
*us = '/0';
}
int main()
{
char *s,*us;
char ss[20];
printf("Please input a string:/n");
scanf("%s",ss);
s = ss;
uppers(s,us);
printf("The result is:/n%s/n",us);
getch();
}
随机输入一个数,判断它是不是对称数(回文数)(如3,121,12321,45254)。不能用字符串库函数
/***************************************************************
1.
函数名称:Symmetry
功能: 判断一个数时候为回文数(121,35653)
输入: 长整型的数
输出: 若为回文数返回值为1 esle 0
******************************************************************/
unsigned char Symmetry (long n)
{
long i,temp;
i=n; temp=0;
while(i) //不用出现长度问题,将数按高低位掉换
{
temp=temp*10+i%10;
i/=10;
}
return(temp==n);
}
方法一
/* ---------------------------------------------------------------------------
功能:
判断字符串是否为回文数字
实现:
先将字符串转换为正整数,再将正整数逆序组合为新的正整数,两数相同则为回文数字
输入:
char *s:待判断的字符串
输出:

返回:
0:正确;1:待判断的字符串为空;2:待判断的字符串不为数字;
3:字符串不为回文数字;4:待判断的字符串溢出
---------------------------------------------------------------------------- */
unsigned IsSymmetry(char *s)
{
char *p = s;
long nNumber = 0;
long n = 0;
long nTemp = 0;
/*判断输入是否为空*/
if (*s == /'//0/')
return 1;
/*将字符串转换为正整数*/
while (*p != /'//0/')
{
/*判断字符是否为数字*/
if (*p /'9/')
return 2;
/*判断正整数是否溢出*/
if ((*p-/'0/') > (4294967295-(nNumber*10)))
return 4;
nNumber = (*p-/'0/') + (nNumber * 10);
p++;
}
/*将数字逆序组合,直接抄楼上高手的代码,莫怪,呵呵*/
n = nNumber;
while(n)
{
/*判断正整数是否溢出*/
101
if ((n%10) > (4294967295-(nTemp*10)))
return 3;
nTemp = nTemp*10 + n%10;
n /= 10;
}
/*比较逆序数和原序数是否相等*/
if (nNumber != nTemp)
return 3;
return 0;
}
方法二
/* ---------------------------------------------------------------------------
功能:
判断字符串是否为回文数字
实现:
先得到字符串的长度,再依次比较字符串的对应位字符是否相同
输入:
char *s:待判断的字符串
输出:

返回:
0:正确;1:待判断的字符串为空;2:待判断的字符串不为数字;
3:字符串不为回文数字
---------------------------------------------------------------------------- */
unsigned IsSymmetry_2(char *s)
{
char *p = s;
int nLen = 0;
int i = 0;
/*判断输入是否为空*/
if (*s == /'//0/')
return 1;
/*得到字符串长度*/
while (*p != /'//0/')
{
/*判断字符是否为数字*/
if (*p /'9/')
return 2;

nLen++;
p++;
}
/*长度不为奇数,不为回文数字*/
if (nLen%2 == 0)
return 4;
/*长度为1,即为回文数字*/
if (nLen == 1)
return 0;
/*依次比较对应字符是否相同*/
p = s;
i = nLen/2 - 1;
while (i)
{
if (*(p+i) != *(p+nLen-i-1))
return 3;
i--;
}
return 0;
}
求2~2000 的所有素数.有足够的内存,要求尽量快
答案:
int findvalue[2000]={2};
static int find=1;
bool adjust(int value)
{
assert(value>=2);
if(value==2) return true;
for(int i=0;i<=find;i++)
{
if(value%findvalue[i]==0)
return false;
}
findvalue[find++];
return true;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值