c#随机输出14个字符串代码

using System;

namespace CSharp_Shell
{
public static class Program
{
public static void Main()
{
string[] z = { “中”, “国”, “人”, “民”, “大”, “家”, “好” }; // 您可以替换为您想要的汉字
Console.Write("\n输出随机组合:\n\n");

        Random random = new Random();

        for (int i = 0; i < 7; i++)
        {
            Console.Write(z[random.Next(z.Length)]);
            Console.Write(z[random.Next(z.Length)]);
           
        }
    }
}

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的遗传算法实现板材排版的C#代码示例: ```csharp using System; using System.Collections.Generic; namespace BoardCuttingGA { class Program { static void Main(string[] args) { // 初始化参数 int populationSize = 20; int generations = 100; double mutationRate = 0.01; double crossoverRate = 0.7; int elitismCount = 2; // 定义板材和零件 Board board = new Board(4, 8); List<Part> parts = new List<Part>() { new Part(2, 3), new Part(1, 4), new Part(3, 2), new Part(1, 2), new Part(2, 1) }; // 创建遗传算法并运行 GeneticAlgorithm ga = new GeneticAlgorithm(populationSize, board, parts, elitismCount, mutationRate, crossoverRate); ga.Run(generations); // 输出结果 Console.WriteLine("Best solution found:"); Console.WriteLine(ga.BestChromosome.ToString()); Console.WriteLine("Fitness: " + ga.BestChromosome.Fitness); Console.ReadLine(); } } // 板材类 class Board { public int Width { get; set; } public int Height { get; set; } public Board(int width, int height) { Width = width; Height = height; } } // 零件类 class Part { public int Width { get; set; } public int Height { get; set; } public Part(int width, int height) { Width = width; Height = height; } } // 布局类 class Layout { public List<Part> Parts { get; set; } public Layout(List<Part> parts) { Parts = parts; } // 计算适应度 public double CalculateFitness(Board board) { int remainingWidth = board.Width; int remainingHeight = board.Height; foreach (Part part in Parts) { if (part.Width > remainingWidth || part.Height > remainingHeight) return 0; if (part.Width <= remainingWidth && part.Height <= remainingHeight) { remainingWidth -= part.Width; remainingHeight -= part.Height; } } return (double)(board.Width * board.Height - remainingWidth * remainingHeight); } // 转换为字符串 public override string ToString() { string result = ""; foreach (Part part in Parts) { result += "(" + part.Width + ", " + part.Height + ") "; } return result; } } // 染色体类 class Chromosome { public List<Part> Parts { get; set; } public double Fitness { get; set; } public Chromosome(List<Part> parts, Board board) { Layout layout = new Layout(parts); Fitness = layout.CalculateFitness(board); Parts = parts; } // 转换为字符串 public override string ToString() { return new Layout(Parts).ToString(); } } // 遗传算法类 class GeneticAlgorithm { private int populationSize; private Board board; private List<Part> parts; private int elitismCount; private double mutationRate; private double crossoverRate; private List<Chromosome> population; public Chromosome BestChromosome { get; private set; } public GeneticAlgorithm(int populationSize, Board board, List<Part> parts, int elitismCount, double mutationRate, double crossoverRate) { this.populationSize = populationSize; this.board = board; this.parts = parts; this.elitismCount = elitismCount; this.mutationRate = mutationRate; this.crossoverRate = crossoverRate; this.population = new List<Chromosome>(); } // 初始化种群 private void InitializePopulation() { for (int i = 0; i < populationSize; i++) { List<Part> randomParts = new List<Part>(); foreach (Part part in parts) { if (new Random().NextDouble() > 0.5) randomParts.Add(new Part(part.Width, part.Height)); } population.Add(new Chromosome(randomParts, board)); } } // 计算适应度总和 private double CalculateFitnessSum() { double sum = 0; foreach (Chromosome chromosome in population) { sum += chromosome.Fitness; } return sum; } // 选择操作 private Chromosome Select() { double fitnessSum = CalculateFitnessSum(); double rand = new Random().NextDouble() * fitnessSum; double runningSum = 0; foreach (Chromosome chromosome in population) { runningSum += chromosome.Fitness; if (runningSum > rand) return chromosome; } return population[population.Count - 1]; } // 交叉操作 private Chromosome Crossover(Chromosome parent1, Chromosome parent2) { List<Part> childParts = new List<Part>(); int crossoverPoint = new Random().Next(0, parts.Count - 1); for (int i = 0; i < crossoverPoint; i++) { childParts.Add(parent1.Parts[i]); } for (int i = crossoverPoint; i < parts.Count; i++) { childParts.Add(parent2.Parts[i]); } return new Chromosome(childParts, board); } // 变异操作 private void Mutate(Chromosome chromosome) { foreach (Part part in chromosome.Parts) { if (new Random().NextDouble() < mutationRate) { part.Width = Math.Max(1, part.Width + new Random().Next(-1, 2)); part.Height = Math.Max(1, part.Height + new Random().Next(-1, 2)); } } chromosome.Fitness = new Layout(chromosome.Parts).CalculateFitness(board); } // 运行遗传算法 public void Run(int generations) { InitializePopulation(); for (int i = 0; i < generations; i++) { population.Sort((x, y) => y.Fitness.CompareTo(x.Fitness)); BestChromosome = population[0]; List<Chromosome> newPopulation = new List<Chromosome>(); for (int j = 0; j < elitismCount; j++) { newPopulation.Add(population[j]); } while (newPopulation.Count < populationSize) { Chromosome parent1 = Select(); Chromosome parent2 = Select(); if (new Random().NextDouble() < crossoverRate) { Chromosome child = Crossover(parent1, parent2); newPopulation.Add(child); } else { newPopulation.Add(parent1); } } foreach (Chromosome chromosome in newPopulation) { if (new Random().NextDouble() < mutationRate) { Mutate(chromosome); } } population = newPopulation; } } } } ``` 在这个示例中,我们定义了一个 `Board` 类来表示板材,一个 `Part` 类来表示零件,以及一个 `Layout` 类来表示布局。我们还定义了一个 `Chromosome` 类来表示染色体,其中包含一个零件列表和一个适应度值。最后,我们创建了一个 `GeneticAlgorithm` 类来实现遗传算法。 在 `GeneticAlgorithm` 类中,我们首先使用 `InitializePopulation` 方法初始化种群。然后,在 `Run` 方法中,我们对种群进行进化。每一代,我们按适应度从高到低对种群进行排序,并选择最佳染色体作为当前最优解。然后,我们使用选择、交叉和变异操作生成新的染色体,并将它们加入到新的种群中。 在选择操作中,我们按适应度总和随机选择一个染色体。在交叉操作中,我们随机选择一个交叉点,并将两个父染色体的部分合并成一个新的子染色体。在变异操作中,我们随机选择一个零件,并随机增加或减少它的宽度或高度。 最后,我们输出找到的最佳解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EYYLTV

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值