一道C++笔试题



题目:
面试厦门欧乐时道编程题拿出来大家共同学习下:从1到100000中随意取出两个数然后将其余99998个数打乱放入A中现在要求遍历A就要找到取出那两个数要求:最多只能定义5个变量不能定义大家有兴趣试试
地址:http://topic.csdn.net/u/20081217/09/6dd9feb6-2142-4833-abdb-ed672c0cb858.html


可以这么考虑设那连个数为x和y获得x+y和x*x+y*y那么就可以算出x和y出来
x+y可以从1到100000和减去的和得出x*x+y*y可以从1到100000平方和减去的平方和获得然后就可以计算具体x和y同理如果找3个数那么将要技术立方和以此类推

下面是本人所写代码



# <stdio.h>
# <time.h>
# <stdlib.h>
# <iostream>
# MAX 100000
void show( *.gif' />, len)
{
std::cout<<.gif' />[len-2]<<'\t'<<.gif' />[len-1]<<'\n';
;
for( i=0;i<len;i)
{
(i%100)
std::cout<<std::endl;
std::cout<<.gif' />[i]<<'\t';
}
std::cout<<'\n';
}
run( *.gif' />, len)
{
unsigned long expectSum=(1+len+2)*(len+2)/2;
unsigned long expectDSum=(len+1)*(len+1)+(len+2)*(len+2);
unsigned long .gif' />Sum=0;
unsigned long .gif' />DSum=0;
for( i=0;i<len;i)
{
expectDSum(i+1)*(i+1);

.gif' />Sum.gif' />[i];
.gif' />DSum.gif' />[i]*.gif' />[i];
}
std::cout<<"expectSum:"<<expectSum<<'\t'
<<"expectDSum:"<<expectDSum<<'\t'
<<".gif' />Sum:"<<.gif' />Sum<<'\t'
<<".gif' />DSum:"<<.gif' />DSum<<'\n';

unsigned long A=expectDSum-.gif' />DSum;
unsigned long B=expectSum-.gif' />Sum;
unsigned long C=(2*A-B*B);
std::cout<<"x+y="<<B<<'\t'
<<"x*x+y*y:"<<A<<'\n'
<<"2*A-B*B:"<<C<<'\n';
unsigned long x_y=sqrt(C);
std::cout<<"x-y:"<<x_y<<'\n';
std::cout<<"x:"<<(x_y+B)/2<<'\n';
std::cout<<"y:"<<(B-x_y)/2<<'\n';
0;
}

{
.gif' />[MAX];
for( i=0;i<MAX;i)
.gif' />[i]=i+1;

show(.gif' />,MAX);

srand(time(NULL));
for( i=0;i<MAX;i)
{
start=rand%MAX;
temp=.gif' />[i];
.gif' />[i]=.gif' />[start];
.gif' />[start]=temp;
}
show(.gif' />,MAX);

run(.gif' />,MAX-2);
0;
}
Tags: 

延伸阅读

最新评论

发表评论