java: Random 随机数组

TestRandom.java

import java.util.*;

class TestRandom {
      // 从 m个整数中随机抽取 n个整数,条件: m > n
      public static int[] genRandomArray(int m , int n) {
          if(m<=0) return null;
          if(n<=0) return null;
          if(m< n) return null;
          
           int[] intRet = new int[n]; 
           int intRd = 0; // 存放随机数
           int count = 0; // 记录生成的随机数个数
           boolean flag = false;  // 是否已经生成过标志
           while(count< n){
                Random rdm = new Random(System.currentTimeMillis());
                intRd = Math.abs(rdm.nextInt())%m +1;
                for(int i=0;i<count;i++){
                    if(intRet[i]==intRd){
                        flag = true;
                        break;
                    }else{
                        flag = false;
                    }
                }
                if(flag==false){
                    intRet[count] = intRd;
                    count++;
                }
          }
          return intRet;
      }
       
      public static void main(String[] args) {
          int m=0,n=0;
          if(args.length ==2){
              m = Integer.parseInt(args[0]);
              n = Integer.parseInt(args[1]);
          } else {
              System.out.println(" usage: java TestRandom m n ");
              return;
          }
          int[] ar = genRandomArray(m,n);
          if(ar==null){
              System.out.println(" Error: int array is null ");
              return;
          }
          for(int i=0; i<n; i++){
            System.out.print(" "+ar[i]);
          }
     }
}


在 C++ 的 Boost 库中,`boost::posix_time` 和 `boost::gregorian` 是两个不同的命名空间,分别用于处理时间与日期相关的类型和函数。它们的使用场景如下: ### `boost::posix_time` 命名空间 该命名空间主要用于处理时间点(`ptime`)和时间间隔(`time_duration`),也包含与完整时间戳相关的一些功能。以下类型的定义和函数需要加上 `boost::posix_time` 前缀: - **时间点类型**:`boost::posix_time::ptime` 用于表示一个具体的时间点,例如当前时间或某个特定时刻。 - **时间间隔类型**:`boost::posix_time::time_duration` 表示一段时间,例如小时、分钟和秒的差值。 - **时间周期类型**:`boost::posix_time::time_period` 用于描述一个时间段范围,可以判断某时间点是否在指定范围内[^2]。 - **获取当前时间函数**:例如 `boost::posix_time::second_clock::local_time()` 或 `boost::posix_time::microsec_clock::universal_time()`,用来获取系统当前的时间点。 - **转换函数**:如 `boost::posix_time::to_tm()`,用于将 `ptime` 转换为标准库中的 `tm` 结构[^3]。 ### `boost::gregorian` 命名空间 这个命名空间专注于日期操作,涵盖了日期对象及其相关计算。以下类型和函数需要加上 `boost::gregorian` 前缀: - **日期类型**:`boost::gregorian::date` 表示一个具体的日期,支持创建、比较以及加减天数等操作。 - **日期间隔类型**:`boost::gregorian::date_duration` 描述日期之间的差值,以天为单位。 - **日期周期类型**:`boost::gregorian::date_period` 用于表示一段日期范围,可以通过 `contains()` 方法判断某个日期是否处于周期内[^2]。 - **日期生成函数**:包括 `boost::gregorian::from_string()` 和 `boost::gregorian::from_undelimited_string()` 等,用于从字符串构造日期对象[^5]。 - **特殊日期枚举**:例如 `boost::date_time::min_date_time` 或 `boost::date_time::not_a_date_time`,用于创建具有特殊含义的日期对象。 - **日期访问函数**:例如 `is_not_a_date()` 判断日期是否无效,或者 `day()` 获取日期中的日部分。 ### 使用方式 在实际代码中,为了简化书写,通常会通过 `using namespace boost::posix_time;` 或 `using namespace boost::gregorian;` 引入整个命名空间。也可以显式地对每个类型或函数添加前缀,避免潜在的命名冲突。例如: ```cpp #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/gregorian/gregorian.hpp> int main() { // 使用 boost::posix_time 处理时间 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); // 使用 boost::gregorian 处理日期 boost::gregorian::date today = boost::gregorian::day_clock::local_day(); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值