程序员的算法趣题Perl版(四)

2024-05-01 00:58
文章标签 算法 程序员 perl 趣题

本文主要是介绍程序员的算法趣题Perl版(四),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

间隔的时间有点长,但是我并没有放弃啊同志们~~~好吧,只有我自己看。。。

第十一题
用斐波那契数列中的每一个数除以其数位上所有数字之和,求出能整除的数。我这里设置的范围是前1000个数字。

#! perl# 20180108# this program is used to find out some numbers in Fibonacci , and the numbers can be divisible
# by the sum of its every single number;
use strict;# the special Fibonacci;
my @fibonacci;
# the maximum of times that the recusive function is executed 
my $max = 1000;
&createFibonacci(1, 1, 0, \@fibonacci);# print
foreach(@fibonacci) {print $_ . "\n";
}sub createFibonacci {my $first = shift;my $second = shift;# the my $count = shift;# the array pointmy $p = shift;my $third = $first + $second;# push if the number is matchedpush @$p, $third if &check($third);$count ++;# the length of array now;my $size = @$p;return 0 if($size == 11);return 0 if $count == $max;# if not contiue...&createFibonacci($second, $third, $count, $p);
}sub check {my $num = shift;my $leng = length $num;my $sum = &sum($num, $leng);return 1 if ($num % $sum) == 0;0;
}# this function is used to sum a number`s every single numbers;
sub sum {my $num = shift;my $leng = shift;my $sum = 0;for(my $i = 0; $i < $leng; $i ++) {return $num if $leng == 1;$sum += substr $num, $i, 1;}$sum;
}

答案:
2
3
5
8
21
144
2584
14930352
86267571272
498454011879264
160500643816367088

第十二题
求在计算平方根的时候,最早让0-9全部出现的最小整数。

#! perl# 20180109
# 20180110use strict;for(my $i = 2; $i < 5000; $i ++) {if(&check($i, 0)) {print "$i\n";last;}
}sub check {# the number checkedmy $num = shift;# '0' means that includes integer, '1' means just decimalmy $justDecimal = shift;# get the square root of a numbermy $sqrtR = sprintf "%10.10f", sqrt $num;# split with '.'my @allNum = split /\./, $sqrtR;# this array is used to check if the number is repeatmy @arr;if($justDecimal) {# just split the decimal part@arr = split //, $allNum[1];} else {my @integerArr = split //, $allNum[0];my @decimalArr = split //, $allNum[1];# include integer, get 10 numbers from the left@arr = (@integerArr, @decimalArr)[0..9];}&isRepeat(\@arr);
}sub isRepeat {# the point of the arraymy $p = shift;# key is numbers, value is timesmy %hash = ();my @uniqueArr = grep {# put the number which is found only once into the hash++ $hash{$_} < 2;} @$p;# the array is repeat if the size of the array is not 10return 1 if $#uniqueArr + 1 == 10;
}

答案:
包含整数部分:1362
只有小数部分:143

第十三题
给字母赋值,求使下列算式成立的解有多少种。字母代表的数字不可以相等。

#! perl# 20180114
# 20180116# this guy is used to compute READ + WRITE + TALK = SKILL;use Algorithm::Combinatorics qw(combinationscombinations_with_repetitionvariationsvariations_with_repetitiontuplestuples_with_repetitionpermutationscircular_permutationsderangementscomplete_permutationspartitionssubsets);
use strict;my $start = time;
my @numbers = (0..9);my @result;
my $count = 0;&sum(\@numbers, 10);foreach (@result) {print "$_\n";
}print "$count \n";my $duration = time - $start;
print "$duration s\n";sub sum {my $numbersP = shift;my $k = shift;# permutationmy $iter = variations($numbersP, $k);while(my $item = $iter -> next) {&isRight($item);}
}sub isRight {my @str_arr = @{@_[0]};my ($r, $e, $a, $d, $w, $i, $t, $l, $k, $s) = @str_arr;# The highest number of digits can not be zero.return 0 if $r == 0 || $w == 0 || $t == 0 || $s == 0;my $read = $r * 1000 + $e * 100 + $a * 10 + $d;my $write = $w * 10000 + $r * 1000 + $i * 100 + $t * 10 + $e;my $talk = $t * 1000 + $a * 100 + $l * 10 + $k;my $skill = $s * 10000 + $k * 1000 + $i * 100 + $l * 10 + $l;if ($read + $write + $talk == $skill) {push @result, "$read + $write + $talk = $skill";$count ++;return 1;}0
}

答案:
10种
1632 + 41976 + 7380 = 50988
2543 + 72065 + 6491 = 81099
4905 + 24689 + 8017 = 37611
5094 + 75310 + 1962 = 82366
5096 + 35710 + 1982 = 42788
5180 + 65921 + 2843 = 73944
5270 + 85132 + 3764 = 94166
7092 + 37510 + 1986 = 46588
7092 + 47310 + 1986 = 56388
9728 + 19467 + 6205 = 35400

这篇关于程序员的算法趣题Perl版(四)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/950343

相关文章

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Python中的随机森林算法与实战

《Python中的随机森林算法与实战》本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房... 目录1、随机森林算法概述2、随机森林的原理3、实现步骤4、分类案例:使用随机森林预测鸢尾花品种4.1

Perl 特殊变量详解

《Perl特殊变量详解》Perl语言中包含了许多特殊变量,这些变量在Perl程序的执行过程中扮演着重要的角色,:本文主要介绍Perl特殊变量,需要的朋友可以参考下... perl 特殊变量Perl 语言中包含了许多特殊变量,这些变量在 Perl 程序的执行过程中扮演着重要的角色。特殊变量通常用于存储程序的

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第