您的位置:首页 > 其它

【算法入门经典】 第二章

2014-05-08 13:33 323 查看

习题2-1 位数(digit)

输入一个不超过10^9的正整数,输出他的位数。不使用任何数学函数。

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

#define pi 3.14159265

int main()
{
int a;
while(cin>>a)
{
int count = 1;
int div = 10;
while(a/div!=0)
{
div=10*div;
count++;
}
cout<<count<<endl;
}
}


习题2-2 水仙花数(daffodil)
abc = a^3 + b^3 + c^3

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

#define pi 3.14159265

int main()
{
for(int i=100;i<=999;i++)
{
int a = i/100;
int b = i/10%10;
int c = i%10;
if(pow(double(a),3)+pow(double(b),3)+pow(double(c),3) == i)
cout<<a<<' '<<b<<' '<<c<<' '<<':'<<i<<endl;
}
}

习题2-3  韩信点兵(hanxin)

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

#define pi 3.14159265

int main()
{
int a,b,c;
while(cin>>a>>b>>c)
{
int res = a*70+b*21+c*15;
while(res>105)
res-=105;
if(res>100||res<10)
cout<<"no result"<<endl;
else
cout<<res<<endl;
}
}


习题2-4 倒三角形(triangle)

输出n层的倒三角形,n<=20

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

#define pi 3.14159265

int main()
{
int n;
while(cin>>n)
{
int begin = 0;
int end = (n-1)*2;
for(int i=0;i<n;i++)
{
for(int j=0;j<(n-1)*2+1;j++)
{
if(j>=begin&&j<=end)
cout<<'*';
else
cout<<' ';
}
cout<<endl;
begin++;
end--;
}
}
}

习题2-6 调和级数

输入n,计算1/1 + 1/2 + 1/3 +... + 1/n ,保留3位小数。

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

#define pi 3.14159265

int main()
{
int n;
while(cin>>n)
{
double res=0;
for(int i=1;i<=n;i++)
{
res+=double(1)/double(i);
}
cout<<fixed<<setprecision(3)<<res<<endl;
}
}

习题2-7 近似计算

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

#define pi 3.14159265

int main()
{
double m = 1;
double res = 0.0;
int count = 0;
while(true)
{
if(count%2 == 0)
res+=1/m;
else
res-=1/m;
if(1/m<pow(double(10),-6))
break;
else
m+=2;
count++;
}
cout<<res<<endl;
}

习题2-8 子序列之和

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

#define pi 3.14159265

int main()
{
int n,m;
while(cin>>n>>m)
{
double res = 0.0;
for(int i=n;i<=m;i++)
res+=1/i/i;//陷阱 不能用i*i,会溢出
cout<<res<<endl;
}
}

习题2-9 分数化小数

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

int main()
{
int a,b,c;
while(cin>>a>>b>>c)
{
double res = double(a)/double(b);
cout<<fixed<<setprecision(c)<<res<<endl;
}
}

习题2-10 排列
思路1.用dfs回溯来对9个数进行全排列,在全排列中取前三位为A,中三位为B,后三位为C,判断是否符合。

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

int	a[9] = {1,2,3,4,5,6,7,8,9};
bool visited[9] = {false};
int res[9];

void permutation(int pos,int h)
{
if(pos==9)
{
int x = res[0]*100+res[1]*10+res[2];
int y = res[3]*100+res[4]*10+res[5];
int z = res[6]*100+res[7]*10+res[8];
if(x*2 == y && x*3==z)
//cout<<res[0]<<res[1]<<res[2]<<','<<res[3]<<res[4]<<res[5]<<','<<res[6]<<res[7]<<res[8]<<endl;
cout<<x<<','<<y<<','<<z<<endl;
return;
}

for(int i=0;i<9;i++)
{
if(visited[i] == false)
{
visited[i] = true;
res[pos] = a[i];
permutation(pos+1,i+1);
visited[i] = false;
}
}

}

int main()
{

permutation(0,0);
}

结果为:

192,384,576

219,438,657

273,546,819

327,654,981

思路2.参考了牛人的简单代码,思路是将a锁定在100-333,因为a最小为100(三位数)

#include <iostream>
#include <set>
using namespace std;

int main(void)
{
int x=100;
for( ; x<= 333;x++)
{
set<int> s;
int y=x*2;
int z=x*3;
s.insert(x/100);
s.insert(x/10%10);
s.insert(x%10);
s.insert(y/100);
s.insert(y/10%10);
s.insert(y%10);
s.insert(z/100);
s.insert(z/10%10);
s.insert(z%10);
if(s.size()==9)
{
int all=0;
set<int>::iterator it;
for(it = s.begin();it!=s.end();it++)
all+=*it;
if(all==45)
cout<<x<<' '<<y<<' '<<z<<endl;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法入门经典