编写一个方法,计算kkk^^kk (共n个k,n>0)的值。例如k=2,n=5则为22222。调用方法求s1=6+66+666+6666+66666

本文介绍了一个简单的Java程序,该程序通过用户输入的k和n值进行特定的累加计算,并展示了如何使用Scanner类从标准输入读取数据。
import java.util.Scanner;
public class Add{
static double m=0.0,sum=0.0;
public static void main(String args[]){
double k,n;
System.out.println("请输入k和n");
Scanner sc=new Scanner(System.in);
k=sc.nextDouble();
n=sc.nextDouble();
for(double i=1.0;i<n; i++){
 m=m+print(k, i);
}
System.out.print(m);
}
  static double print(double k1,double n1){
    for(double i=1.0;i<n1 ;i++){
sum=sum*10+k1;
}
return sum;
}
用下列洪水算法,代替程序2中的DFS和BFS算法:洪水算法:function [Destination_fitness,Destination_position,Convergence_curve]=FLA(nPop,MaxIt,lb,ub,nVar,fobj) CostFunction = @(x) fobj(x); tic time1=0; VarSize = [1 nVar]; VarMin = lb; % Lower Bound of Variables VarMax = ub ; % Upper Bound of Variables NFEs=0; % Number of fitness evaluations Ne=5; % Number of weak population that will be replaced it=0; % Iteration counter %% Initialization POP.Position=[]; % Initialize Population Array POP.Cost=[]; Val=0*unifrnd(VarMin,VarMax,VarSize); BestSol.Cost=inf; % Initialize Best Solution Ever Found pop=repmat(POP,nPop,1); % Initialize Population Array %% Create Initial Population for i=1:nPop pop(i).Position=unifrnd(VarMin,VarMax,VarSize); %%Eq.(4) pop(i).Cost=CostFunction(pop(i).Position); Val(i,:)=0*unifrnd(VarMin,VarMax,VarSize); if pop(i).Cost<=BestSol.Cost BestSol=pop(i); end end while NFEs<nPop*MaxIt it=it+1; PK=(((((MaxIt*(it^2))+1)^0.5)+((1/((MaxIt/4)*it))*(log((((MaxIt*(it^2))+1)^0.5)+((MaxIt/4)*it)))))^((-2/3)))*(1.2/it); %%Eq.(5) for i=1:nPop %% Implementing phase I % Sorting the population [sortpop, ~]=sort([pop.Cost]); Pe_i=((pop(i).Cost-sortpop(1))/(sortpop(end)-sortpop(1)))^(2); %%Eq.(7) A=randperm(nPop); A(A==i)=[]; a=A(1); if rand>(rand+Pe_i) Val(i,:)=((PK^randn)/it)*unifrnd(VarMin,VarMax,VarSize); newsol.Position =pop(i).Position+Val(i,:); %%Eq.(6) newsol.Position=max(newsol.Position,VarMin); newsol.Position=min(newsol.Position,VarMax); else newsol.Position=BestSol.Position+rand(VarSize).*(pop(a).Position-pop(i).Position); newsol.Position=max(newsol.Position,VarMin); newsol.Position=min(newsol.Position,VarMax); end newsol.Cost=CostFunction(newsol.Position); NFEs=NFEs+1; if newsol.Cost<=pop(i).Cost pop(i)=newsol; end if newsol.Cost<=BestSol.Cost BestSol=newsol; end end %% Implementing phase II Pt=abs(sin(rand/(it))); %%Eq.(8) if rand<Pt [~, Sortorder]=sort([pop.Cost]); pop=pop(Sortorder); pop=pop(1:nPop-Ne); newpop=repmat(POP,Ne,1); for ie=1:Ne newpop(ie).Position=BestSol.Position+(rand)*(unifrnd(VarMin,VarMax,VarSize)); %%Eq.(9) newpop(ie).Cost=CostFunction(newpop(ie).Position); NFEs=NFEs+1; Val(nPop+ie,:)=0*unifrnd(VarMin,VarMax,VarSize); if newpop(ie).Cost<=BestSol.Cost BestSol=newpop(ie); end end pop=[pop newpop]; end % Store Record for Current Iteration Convergence_curve(it) =BestSol.Cost; % Show Iteration Information if toc>time1 time1=time1+0.5; disp(['Iteration ' num2str(NFEs) ': Best Cost = ' num2str( Convergence_curve(it))]); end end %% Results Destination_fitness=pop(1).Cost; Destination_position=pop(1).Position; %% Plotting Convergence_curve % % % semilogy(Convergence_curve, 'LineWidth', 1.5); % % % % % % % % % % % Set ylabel % % % ylabel('Log(Objective Function)'); % % % % % % % % % % % Set xlabel % % % xlabel('Iteration'); % % % % % % % % % % % Set the y-axis to semilog scale % % % set(gca, 'YScale', 'log'); % % % set(gca, 'FontName', 'Times New Roman'); % % % % % % % % % % % Display the box around the axes % % % box on; end 程序2:%% 步骤1:相关参数 mpc1 = IEEE33; % 配电网数据 Nb = 33; % 节点数 Nt = 24; % 时段数 Y_DG = [6, 8, 12, 23, 30]; % 光储系统接入位置 wf = 0.01*ones(1,Nb); % 负荷等级系数 wf([6, 12, 23, 30]) = 1; % 一级负荷 wf([1,5,8,14,18,21,22,27,29,32]) = 0.1; % 二级负荷 Ct = zeros(Nb, Nt); % 负荷时变需系数 Ct([6, 12, 23, 30], :) = 10; % 一级负荷的需度恒为10 Ct(setdiff(1:Nb, [6, 12, 23, 30]), :) = ones(Nb - 4 , 1)*10*[7.77960493860530,5.32481765803490,5.04294379929200,5.19839957470577,2.98814682221510,3.42926158320840,7.20423714015130,10.6852586809857,12.5476776005102,15.5211669393227,24.8696812348447,33.2302134035133,24.2862651332590,24.7988707426448,36.3317495052460,27.4948240683687,17.8808252142146,14.0969740403730,18.9045593211401,20.4888032159577,22.2151328923961,19.7627571226032,21.4368039620930,17.6810254063144]/36.3317; Ft = Ct.*(wf'*ones(1, Nt)); % 优先恢复系数 % 光伏出力 P_PV = [425, 200, 480, 120, 600; 700, 500, 720, 620, 720]; PL = [3305 , 4870]; % 负荷总量 P_load = mpc1.bus(:,3)/3.715*PL; % 各节点负荷 a_cut = zeros(1,Nb); % 削减系数 C_load1 = [3,13,20,24]; % 完全可控的负荷集合 a_cut(C_load1) = 1; % 完全可控 C_load2 = [2,7,10]; % 50%可控的负荷集合 a_cut(C_load2) = 0.5; % 50%可控 %% 步骤2:读取故障支路 fault_branch = [5, 25, 20]; % 故障支路 %% 步骤3:确定故障时刻 time0 = [7 , 15]; time = time0(index); % 当前时刻 if index ~= 1 && index ~= 2 error('输入有误请重新输入!!!') end %% 步骤4:确定优先恢复集合 F_Rload = {[6, 12, 15, 23, 28, 30], [6, 12, 23, 30, 32]}; %% 步骤5:采用BFS确定孤岛包含的负荷 island_node = cell(1, length(Y_DG)); % 各个孤岛包含的负荷节点 island_node_all = []; for k = 1:length(Y_DG) [island_node{k} , ~] = BFS(Y_DG(k) , P_PV(index,k)); island_node_all = [island_node_all, setdiff(island_node{k}, island_node_all)]; end % 将交叉的孤岛融合 index_island = zeros(length(Y_DG)); % 孤岛是否交叉的标志,例如index_island(1,2)=1,表示孤岛12有交叉 for k = 1:length(Y_DG) for kk = k + 1 : length(Y_DG) if ~isempty(intersect(island_node{k} , island_node{kk})) index_island(k , kk) = 1; % 如果孤岛出现交叉情况就把标志为置为1 end end end island_node_cross = cell(1, length(Y_DG) - sum(index_island(:))); % 将交叉的孤岛融合后的孤岛划分方案 island_cross_DG = cell(1, length(Y_DG) - sum(index_island(:))); % 融合后孤岛中包含的光伏节点 island_node_cross{1} = island_node{1}; island_cross_DG{1} = 1; kk = 2; for k = 2:length(Y_DG) if sum(index_island(k , :)) == 0 && sum(index_island(: , k)) == 0 island_node_cross{kk} = island_node{k}; island_cross_DG{kk} = k; kk = kk + 1; else cross_num = [find(index_island(k , :)) , find(index_island(: , k))]; for kkk = 1:length(cross_num) island_node_cross{min(cross_num)} = union(island_node_cross{min(cross_num)} , island_node{k}); island_cross_DG{min(cross_num)} = union(island_cross_DG{min(cross_num)} , k); end end end
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值