构造最接近圆周率的数。

11 views (last 30 days)
homawan
homawan on 19 Nov 2022
Answered: lehave on 19 Nov 2022
0 ~ 9 十个数字,构造出最接近圆周率的那个数。10个数全部使用,且不能重复.
比如37869/12054,你会有更好的结果吗?

Accepted Answer

lehave
lehave on 19 Nov 2022
clear;
A=perms(0:9);   %0到9全排列
u1=A(:,1)==0;   
u2=A(:,6)==0;   
u=u1|u2;
A(u,:)=[];      %剔除首位为0的数
k1=A(:,1:5);
k2=A(:,6:10);
w=[1e4,1e3,1e2,1e1,1];
k1=bsxfun(@times,k1,w);
k2=bsxfun(@times,k2,w);   
k1=sum(k1,2);
k2=sum(k2,2);    %将全排列转化为单个的五位数
t=k1./k2;
f=abs(t-pi);     
[a,b]=min(f);    %找出最近将pi的数字的位置
I1=k1(b)
I2=k2(b)
% A(b,:)

More Answers (0)

Categories

Find more on R Language in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!