先看昨天的成果:
勉强冲进前50.
今天花点时间做一些ACM的水题。
1.菲波那且数列
菲波那契(Fibonacci)数(简称菲氏数)定义为:
⎧ f (0) = 0
⎪
⎨ f (1) = 1
⎪ f (n) = f (n − 1) + f (n − 2) (n > 1且n ∈ 整数)
⎩
如果写出菲氏数列,则应该是:
0 1 1 2 3 5 8 13 21 34 ...
如果求其第 6 项,则应为 8。
求第 n 项菲氏数。
输入描述:输入数据含有不多于 50 个的正整数 n(0≤n≤46)。
输出描述:对于每个 n,计算其第 n 项菲氏数,每个结果应单独占一行。
输入样例
6 10
输出样例
8
55
解答:
#include <iostream>
#include <vector>
#include <numeric>
#include <fstream>
using namespace std;
int main()
{
//ifstream cin("aaa.txt");
vector<unsigned int> v;
unsigned int n;
v.push_back(0);
v.push_back(1);
for(int i=2;i<47;i++)
{
v.push_back(v[i-1]+v[i-2]);
}
while(cin>>n) cout<<v[n]<<endl;
return 0;
}
2.最大公约数
题目内容
求两个正整数的最大公约数。
输入描述:
输入数据含有不多于 50 对的数据,
每对数据由两个正整数
(0 < n1,
n2 < 232 )
组成。
输出描述:对于每组数据 n1 和 n2,计算最大公约数,每个计算结果应占单独一行。
输入样例
6 5 18 12
输出样例
1
6
解答:欧几里德算法(辗转相除)
#include <iostream>
#include <vector>
#include <numeric>
#include <fstream>
using namespace std;
int gcd(int,int);
int main()
{
//ifstream cin("aaa.txt");
int x,y;
while(cin>>x>>y)
{
cout<<gcd(x,y)<<endl;
}
return 0;
}
int gcd(int x,int y)
{
while(x != y)
{
if(x>y) x = x-y;
else y= y-x;
}
return x;
}
3.求fan数
正整数的各位数字之和被fans称为Fans数。 求输入数(长度<232)的Fans数!
Sample Input:
12345
56123
82
Sample Output:
15
17
10
解答:
#include <iostream>
#include <vector>
#include <numeric>
#include <fstream>
#include <string>
using namespace std;
int main()
{
//ifstream cin("aaa.txt");
string s;
//const int LINE_LENGTH = 233;
int i,sum;
//char str[LINE_LENGTH];
while(cin>>s)
{
//s=str;
sum = 0;
for(i=0;i<s.length();i++)
{
if(s[i]=='0')sum+=0;
else if(s[i]=='1')sum+=1;
else if(s[i]=='2')sum+=2;
else if(s[i]=='3')sum+=3;
else if(s[i]=='4')sum+=4;
else if(s[i]=='5')sum+=5;
else if(s[i]=='6')sum+=6;
else if(s[i]=='7')sum+=7;
else if(s[i]=='8')sum+=8;
else if(s[i]=='9')sum+=9;
}
cout<<sum<<endl;
}
return 0;
}
4.对称三位数素数
判断一个数是否为对称三位数素数。所谓“对称”是指一个数,倒过来还是该数。例
如,375 不是对称数,因为倒过来变成了 573。
输入描述:输入数据含有不多于 50 个的正整数(0<n< 232 )。
输出描述:
对于每个 n,
如果该数是对称三位数素数,
则输出
“Yes”
,
否则输出
“No”
。
每个判断结果单独列一行。
输入样例
11 101 272
输出样例
No
Yes
No
思路:
1、三位对称的判断-n>100&&n<1000&&n%10==n/100.
2、判断是否为素数-让该数除以2到该数的平方根,如果有一个数整除,那么为非素数.
#include <iostream>
#include <vector>
#include <numeric>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
bool isPrime(int n);
int main()
{
//ifstream cin("aaa.txt");
int n;
while(cin>>n)
{
cout<<(n>100&&n<1000&&n/100==n%10&&isPrime(n)?"Yes\n":"No\n");
}
return 0;
}
bool isPrime(int n)
{
int sqr = sqrt(n*1.0);
for(int i=2;i<sqr;i++)
{
if(n%i==0) return false;
}
return true;
}
5.01串排序
#include <iostream>
#include <vector>
#include <numeric>
#include <fstream>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
struct Comp
{
bool operator()(const string &s1,const string &s2)
{
if(s1.length()!=s2.length()) return s1.length()<s2.length();
int c1 = count(s1.begin(),s1.end(),'1');
int c2 = count(s2.begin(),s2.end(),'1');
return (c1 != c2 ? c1<c2 : s1<s2);
}
};
int main()
{
// ifstream cin("aaa.txt");
multiset <string,Comp> ms;
string s;
while(cin>>s)
{
ms.insert(s);
}
for(multiset<string,Comp>::iterator it = ms.begin();it!=ms.end();it++)
{
cout<<*it<<endl;
}
return 0;
}
由于以前没有A过题,所以上面的都是一些比较水的题,算是练手了。
C++里面的STL还是挺重要的。
今天就到这。