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 <string.h> #include <iostream> 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))^(b&(1<<i)))) i--; return (i^-1)?(((a>>i)&1)?1:-1):0; } int _tmain() { int c; c = Cmp(5,4); cout<<c<<endl; return 0; } jruv (~~~一叶落而知天下秋~~~) 的答案: #define COMPARE(a,b) ((a)-(b)) //<0: a<b =0:a==b>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<iostream> 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<<head->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<<endl; while(head) //释放空间 { p=head->next; delete head; head=p; } } int main() { cout<<"请输入两个数"<<endl; char test1[MAX],test2[MAX]; cin.getline(test1,MAX,'/n'); cin.getline(test2,MAX,'/n'); Mul(strrev(test1),strrev(test2)); system("PAUSE"); return 0; } 上面大整数已经写了.你加几个东西就行了. #include<iostream> 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<<head->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<<endl; while(head) //释放空间 { p=head->next; delete head; head=p; } } int main() { cout<<"请输入两个数"<<endl; char test1[MAX],test2[MAX],*p; int pos=0; cin.getline(test1,MAX,'/n'); cin.getline(test2,MAX,'/n'); if(p=strchr(test1,'.')) { pos+=strlen(test1)-(p-test1)-1; do { p++; *(p-1)=*p; }while(*p); } if(p=strchr(test2,'.')) { pos+=strlen(test2)-(p-test2)-1; do { p++; *(p-1)=*p; }while(*p); } Mul(strrev(test1),strrev(test2),pos); system("PAUSE"); return 0; }
4.写一个病毒 cout<<"一个病毒"<<endl; (开玩笑的,没搞过,^_^) 5.让你在100000000个浮点数中找出最大的10000个,要求时间复杂度优。 //本算法使用快排,O(n*lg(n)) //最低可以找到线性算法,使用预先区域统计划分!类试于构造Quad Trees! 写起来代码会长些! #include <stdio.h> #include <stdlib.h> #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:关键是时间最长的两个人必须同时过桥 |