erlang初學小練習

2021-06-17 00:49:10 字數 3611 閱讀 9868

-module(delive).

-compile(export_all).

sum(x, y) ->

if  x >= 9, y >= 9 ->

io:format("~w * ~w = ~w~n", [x, y, x * y]);

x > y ->

io:format("~w * ~w = ~w", [x, y, x * y]),

sum(x, y + 1);

x =< y ->

io:format("~w * ~w = ~w~n", [x, y, x * y]),

sum(x + 1, 1)

end.

sum() ->

sum(1, 1).

上面是九九乘法表

-module(servertest).

-export([start/0, stop/0, myget/1, myset/2]).

start() ->

register(testserver, spawn(fun loop/0)).

stop() ->

testserver ! stop.

myget(key) ->

testserver ! ,

receive

->

response

end.

myset(key, value) ->

testserver ! ,

receive

->

response

end.

loop() ->

receive

->

from ! ,

loop();

->

put(key, value),

from ! ,

loop();

->

from ! stop;

_ ->

error

end.

題目一

有1,2, 3, 4個數字,能組成多少個互不相同且不重複的三位數?都是多少?

-module(c1).

-export([count/1]).

count(n)  ->

c = [x * 100 + y * 10 + z ||

x <- n, y <- n, z <- n, x =/= y, y =/= z, x =/= z

],length(c).

題目二

企業發放的獎金根據利潤提成,利潤低於或等於10萬元,獎金可提10%;利潤高於10萬元低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分可提成7.5%;20萬元到40萬元,高於20萬元的部分,可提成5%。

-module(c2).

-export([profit_count/1]).

profit_count(n)  ->

if n =< 100000             ->

p = 100000 * 0.1;

n > 100000, n < 200000  ->

p = 100000 * 0.1 + (n - 100000) * 0.075;

n >= 200000, n < 400000 ->

p = (n - 200000) * 0.05

end,

io:format("the profit is ~p~n", [p]).

題目三

乙個整數,它加上100後是乙個完全平方數,再加上268又是乙個完全平方數,請問該數是多少?

-module(c3).

-export([sqrt_num/0, sqrt_num/1]).

sqrt_num()   ->

lists:filter(fun(x) -> is_integer(x) end, [sqrt_num(x) || x <- lists:seq(1, 100)]).

sqrt_num(x) ->

t1 = x + 100,

r1 = trunc(math:sqrt(t1)),

t2 = x + 268,

r2 = trunc(math:sqrt(t2)),

if(t1 == r1 * r1) and (t2 == r2 * r2)  ->

x;true ->

okend.

題目四輸入某年某月某日,判斷這一天是這一年的第幾天?

-module(c4).

-export([which_day/3]).

which_day(y, m, d)  ->

list = [month_length(year, month) || year <- [y], month <- lists:seq(1, m - 1)],

list_size(list, d).

month_length(year, month) ->

leap = if

year rem 400 == 0  ->

leap;

year rem 100 == 0  ->

not_leap;

year rem 4 == 0    ->

leap;

true               ->

not_leap

end, 

case month of

3 -> 31;

2 when leap == leap -> 29;

2 -> 28;

1 -> 31;

4 -> 30

end.

list_size([head | tail], result) ->

list_size(tail, head + result);

list_size(, result) ->

result.

題目五列印所有的「水仙花數」所謂的「水仙花」數即乙個三位數,其各位數字的立方和等於該數

-module(c7).

-export([print_flower/0]).

print_flower()  ->

[x * 100 + y * 10 + z || x <- lists:seq(1, 9), y <- lists:seq(0, 9), z <- lists:seq(0, 9), x * x * x + y * y * y + z * z * z == x * 100 + y * 10 + z].

題目六講乙個正整數分解質因數,如90=2*3*3*5

-module(c8).

-export([prime_factor/1]).

prime_factor(n) ->

h  =  [i || i <- lists:seq(2, n)],

prime(h, n).

prime([h | t], n) ->

ifn rem h =:= 0 ->

io:format("~w ", [h]),

prime_factor(n div h);

true ->

prime(t, n)

end;

prime(, _) ->

ok.

初學分頁的小練習

package com.cn.toolsutil public class dividepage 構造方法 public dividepage int pagesize,int recordcount 總頁數 public int getpagecount 包含,起始索引為0 public int ...

erlang基礎練習 排序

module sort export bubble 1,select1 1,select2 1 氣泡排序法 第一種 bubble l bubble sort l,length l 1 bubble sort l,0 l bubble sort h t num result bubble once h...

Erlang實戰練習(六)

本文主要講為文字建立索引,通過使用程序字典的方式為文字建立索引,儘管專家提倡盡量避免使用程序字典,但是在初學階段很容易地就使用了程序字典,當然,除了程序字典方式外,還有其它方式,後面章節我將會使用另外一種方式來儲存單詞 行數 出現次數,也即erlang提供的模組 ets erlang term st...