每日锻炼--2

-module(daily_two).

-export([sum/1, sum/2]).
-export([create_list/1, create_list2/1, create_rList/1]).
-export([print_list/1, print_odd/1, print_even/1]).
-export([hanoi/1]).
-export([cross_arith/1]).

%% 计算1~N个数的和
sum(0) -> 0;
sum(N) -> sum(N - 1) + N.


%% 计算M到N的和
sum(M, N) when M > N -> do_sum(N, M);
sum(M, N) when M =< N -> do_sum(M, N).

do_sum(0, N) -> sum(N);
do_sum(N, N) -> N;
do_sum(M, N) -> N + do_sum(M, N - 1). 



%% 创建一个整数列表
% 方法-1
create_list(0) -> [];
create_list(N) -> do_create(N - 1, N).

do_create(0, N) -> [N];
do_create(M, N) -> [N - M | do_create(M - 1, N)].

% 方法-2
create_list2(N) -> do_list([], N).

do_list(L, 0) -> L;
do_list(L, N) -> do_list([N|L], N - 1).


%% 创建一个反向的整数列表
create_rList(0) -> [];
create_rList(1)	-> [1];
create_rList(N) -> [N | create_rList(N - 1)].


%% 打印1~N个数
print_list(0) -> do_print(0);
print_list(N) -> print_list(N - 1, N).

print_list(0, N) -> do_print(N);
print_list(M, N) -> do_print(N - M),
		  print_list(M - 1, N). 

do_print(N) -> io:format("Number:~p~n", [N]).
do_even_print(N) -> io:format("Even Number:~p~n", [N]).
do_odd_print(N) -> io:format("Odd Number:~p~n", [N]).

%% 打印1~N之间的偶数
print_even(0) -> do_even_print(0);
print_even(N) -> print_even(N, N).

print_even(0, N) -> 
			if
				(N rem 2 =:= 0) -> 				%% erlang has not '%' operator,confict with note
					do_even_print(N)			%% Other condition is not needed,because is end
			end;
print_even(M, N) ->
			Value = N - M,
			if
				Value rem 2 =:= 0 ->
					do_even_print(Value),
					print_even(M - 2, N);
				Value rem 2 =/= 0 ->			%% unequal operator is =/=, not =!= in erlang
					print_even(M - 1, N)
			end.


%% 打印1~N之间的奇数
print_odd(0) -> do_odd_print(0);
print_odd(N) -> print_odd(N, N).

print_odd(1, N) -> 
		if
			(N rem 2 =/= 0) ->
				do_odd_print(N);
			(N rem 2 =:= 0) ->		%% if is not true condition,there is a error in runtime
				over
		end;
print_odd(M, N) -> 
		Value = N - M,
		if
			Value rem 2 =/= 0 ->
				do_odd_print(Value),
				print_odd(M - 2, N);
			Value rem 2 =:= 0 ->
				print_odd(M - 1, N)
		end.

%% 实现汉诺塔的移动
%% =====================Steps=========================================
%% 汉诺塔的算法就3个步骤:
%% 第一,把a上的n-1个盘通过c移动到b。
%% 第二,把a上的最下面的盘移到c。
%% 第三,因为n-1个盘全在b上了,所以把b当做a重复以上步骤就好了
%% -------------------------------------------------------------------
%% A----> Origin tower	B----> Assist tower	C----> Destination tower
%% N----> disk numbers
%% ===================================================================
hanoi(N) -> move(N, a, b, c). 	%% if no "",there will be warn.

move(1, A, _B, C) -> 
		io:format("Move disk 1 from ~p to ~p~n", [A, C]);		%% when only 1 disk, move from A to C directly
move(N, A, B, C) -> move(N - 1, A, C, B),					%% first move N-1 disks from A to B in the help of C
		io:format("Move disk ~p from ~p to ~p~n", [N, A, C]),		%% move biggest disk from A to C
		move(N - 1, B, A, C).						%% as N-1 disks has moved to B, now start with B


%% 交叉计算,偶数减,奇数加(1-2+3-4+5-6+7-8***N) 
cross_arith(1) -> 1;
cross_arith(N) -> 
	io:format("0"),
	cross_arith(1, N, 0).

cross_arith(N, N, Sum) -> 
	Value = do_arith(N, Sum),
	io:format("=~p~n", [Value]);
cross_arith(M, N, Sum) -> 
	Value = do_arith(M, Sum),
	cross_arith(M + 1, N, Value).

do_arith(M, Sum) ->
	if
		M rem 2 =:= 0 -> 
			io:format("-~p", [M]),
			Sum - M;
		M rem 2 =/= 0 -> 
			io:format("+~p", [M]),
			Sum + M
	end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值