链家网2018秋招内推编程题

凭记忆联想,笔试编程题共有三个:

1.求两个数之和。

要求:输入a,b,输出a与b之和;

例如:

输入:

1 2

3 4

5 6

输出:

3

7

11

C++实现:

#include<iostream>
using namespace std;
int main(){
	int a,b;
	while(cin>>a>>b){
		cout<<a+b<<endl;
	}
	return 0;
}


2.求第n个最小质数。

要求:输入n,输出第n个最小的质数,最大数不超过10000;

例如:

输入:

10

输出:

29

提示:前十个质数:2,3,5,7,11,13,17,19,23,29;

C++实现:

#include<iostream>
#include<math.h>
using namespace std;

int main(){
	int n,res,count=0;
	cin>>n;
	for(int i=2;i<10000;i++){
		int j=2;
		for(j=2;j<sqrt(i);j++){
			if(i%j==0) break;
		}
		if(j>sqrt(i)){
			count++;
			res=i;
			if(count>=n) break;
		}
	}
	cout<<res<<endl;
	return 0;
}

3.求斐波拉斯数列。

要求:输入n,输出第n个对应的斐波拉斯数;

例如:

输入:

8

输出:

21

C++实现:

#include<iostream>
#include<vector>
using namespace std;
int fabo(int& m){
	vector<int> res;
	res.push_back(1);
	res.push_back(1);
	for(int i=2;i<m;i++){
		res.push_back(res[i-2]+res[i-1]);
	}
	return res.back();
}

int main(){
	int n;
	cin>>n;
	cout<<fabo(n)<<endl;
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值