C++笔试题(二)

匿名网友 匿名网友 发布于: 2015-08-30 00:00:00
阅读 173 收藏 0 点赞 0 评论 0

1、请定义一个宏,比较两个数a、b的大小,不能使用大于、小于、if语句
2、如何输出源文件的标题和目前执行行的行数
3、两个数相乘,小数点后位数没有限制,请写一个高精度算法
4、写一个病毒
5、有A、B、C、D四个人,要在夜里过一座桥。他们通过这座桥分别需要耗时1、2、5、10分钟,只有一支手电,并且同时最多只能两个人一起过桥。请问,如何安排,能够在17分钟内这四个人都过桥?

2005年腾讯招聘
选择题(60)
c/c++ os linux 方面的基础知识 c的Sizeof函数有好几个!
程序填空(40)
1.(20) 4空x5
不使用额外空间,将 A,B两链表的元素交叉归并
2.(20) 4空x5
MFC 将树序列化 转存在数组或 链表中!

1.请定义一个宏,比较两个数a、b的大小,不能使用大于、小于、if语句
// 这样转向定义应该不算违规吧!^_^
#include “stdafx.h”
#include
#include
using namespace std;

#define Cmp(x,y) compare(x,y)

int compare(int a,int b)
{
a^=(1<<31); b^=(1<<31); int i=31; while((i^-1) && !((a&(1<>i)&1)?1:-1):0;
}

int _tmain()
{
int c;
c = Cmp(5,4);
cout<0:a>b
2.如何输出源文件的标题和目前执行行的行数
cout << "Filename " << __FILE__ << " Line " << __LINE__ << endl; 3.两个数相乘,小数点后位数没有限制,请写一个高精度算法 算法提示: 输入 string a, string b; 计算string c=a*b; 返回 c; 1, 纪录小数点在a,b中的位置l1,l2, 则需要小数点后移动位置数为l=length(a)+length(b)-l1-l2-2; 2, 去掉a,b中的小数点,(a,b小数点后移,使a,b变为整数) 3, 计算c=a*b; (同整数的大数相乘算法) 4, 输出c,(注意在输出倒数第l个数时,输出一个小数点。若是输出的数少于l个,就补0) du51(郁郁思扬)的答案: 变为整数求就行了.输入的时候记一下,小数点位置..输出再做点文章就行了. 下面的是大整数的运算. #include
using namespace std;
#define MAX 10000
struct Node{
int data;
Node *next;
};
void output(Node *head)
{
if(!head->next&&!head->data)return;
output(head->next);
cout<data;
}
void Mul(char *a,char *b,int pos)
{
char *ap=a,*bp=b;
Node *head=0;
head=new Node;head->data=0,head->next=0; //头
Node *p,*q=head,*p1;
int temp=0,temp1,bbit;
while(*bp) //若乘数不为空 ,继续.
{
p=q->next;p1=q;
bbit=*bp-48; //把当前位转为整型
while(*ap||temp) //若被乘数不空,继续
{
if(!p) //若要操作的结点为空,申请之
{
p=new Node;
p->data=0;
p->next=0;
p1->next=p;
}
if(*ap==0)temp1=temp;
else { temp1=(p1->data)+(*ap-48)*bbit+temp;ap++; }
p1->data=temp1%10; //留当前位
temp=temp1/10; //进位以int的形式留下.
p1=p;p=p->next; //被乘数到下一位
}
ap=a;bp++;q=q->next; //q进下一位
}
p=head;
output(p); //显示
cout<next;
delete head;
head=p;
}
}
int main()
{
cout<<"请输入两个数"<
using namespace std;
#define MAX 10000
struct Node{
int data;
Node *next;
};
void output(Node *head,int pos)
{
if(!head->next&&!head->data)return;
output(head->next,pos-1);
cout<data;
if(!pos)cout<<"."; } void Mul(char *a,char *b,int pos) { char *ap=a,*bp=b; Node *head=0; head=new Node;head->data=0,head->next=0; //头
Node *p,*q=head,*p1;
int temp=0,temp1,bbit;
while(*bp) //若乘数不为空 ,继续.
{
p=q->next;p1=q;
bbit=*bp-48; //把当前位转为整型
while(*ap||temp) //若被乘数不空,继续
{
if(!p) //若要操作的结点为空,申请之
{
p=new Node;
p->data=0;
p->next=0;
p1->next=p;
}
if(*ap==0)temp1=temp;
else { temp1=(p1->data)+(*ap-48)*bbit+temp;ap++; }
p1->data=temp1%10; //留当前位
temp=temp1/10; //进位以int的形式留下.
p1=p;p=p->next; //被乘数到下一位
}
ap=a;bp++;q=q->next; //q进下一位
}
p=head;
output(p,pos); //显示
cout<next;
delete head;
head=p;
}
}
int main()
{
cout<<"请输入两个数"<
#include

#define Max 100000000
int a[Max+10];

int cmp(const void *a, const void *b)
{
int *x = (int *) a;
int *y = (int *) b;
return *x-*y;
}

int main()
{
int n=0;
while(scanf(“%d”,&a[n])==1) n++;
qsort(a,n,4,cmp);
for(int i=0;i<3;i++) printf("%d",a[ i ]); return 1; } 5、有A、B、C、D四个人,要在夜里过一座桥。他们通过这座桥分别需要耗时1、2、5、10分钟,只有一支手电,并且同时最多只能两个人一起过桥。请问,如何安排,能够在17分钟内这四个人都过桥? Solution:关键是时间最长的两个人必须同时过桥 The First Time: A(1)和B(2)过桥,A(1)返回 Cost:1+2 The Second Time: C(5)和D(10)过桥,B(2)返回 Cost:10+2 The Third Time A(1)和B(2)过桥 Cost:2 Total Time Cost: (1+2)+(10+2)+2=17 minutes

评论列表
文章目录