• c# Java 微信红包算法


                int total_money_cent = 1000; // 红包总金额 单位:分
                int total_people = 8; // 抢红包总人数
           int[] array = new int[total_money_cent];
                for (int i = 0; i < total_money_cent; i++)
                {
                    array[i] = i;
                }
    
                Random random = new Random();
                for (int i = 1; i < total_people; i++)
                {
                    int index = random.Next(total_money_cent - i) + i;
                    int temp = array[i];
                    array[i] = array[index];
                    array[index] = temp;
                }
                array[total_people] = total_money_cent;
    
    
                Array.Sort(array, 0, total_people + 1);
                for (int i = 1; i <= total_people; i++)
                {
                    Console.Write("第 {0} 个红包:{1}.{2} 元,剩下 {3}.{4} 元
    ", i,
                        (array[i] - array[i - 1]) / 100, (array[i] - array[i - 1]) % 100,
                        (total_money_cent - array[i]) / 100, (total_money_cent - array[i]) % 100);
                }

     Java

    int total_money_cent; // 红包总金额
            int total_people; // 抢红包总人数
    
            total_money_cent = 1000;
            total_people = 8;
    
            int array[] = new int[total_money_cent];
            for (int i = 0; i < total_money_cent; i++) {
                array[i] = i;
            }
    
            Random random = new Random();
            for (int i = 1; i < total_people; i++) {
                int index = random.nextInt(total_money_cent - i) + i;
                int temp = array[i];
                array[i] = array[index];
                array[index] = temp;
            }
            array[total_people] = total_money_cent;
            Arrays.sort(array, 0, total_people + 1);
            for (int i = 1; i <= total_people; i++) {
                System.out.printf("第 %d 个红包:%d.%02d 元,剩下 %d.%02d 元
    ", i, 
                    (array[i] - array[i-1]) / 100, (array[i] - array[i-1]) % 100 ,
                    (total_money_cent - array[i]) / 100, (total_money_cent - array[i]) % 100);
            }
  • 相关阅读:
    八爪鱼 爬取微博中的图片到本地
    【简易采集】美团数据抓取方法 八爪鱼
    jeesite 的提示消息图标
    SpringBoot 入门 Demo
    spring 简单入门实例
    正则表达式之匹配替换
    数据结构之堆栈
    c#设计模式之装饰者模式
    c#设计模式之策略模式
    一个自然数在1700和1800之间,且被5除余3,被7除余4,被11除余6,求符合条件的数
  • 原文地址:https://www.cnblogs.com/micenote/p/7267207.html
Copyright © 2020-2023  润新知