凭记忆联想,笔试编程题共有三个:
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;
}