Problem A: Random Permutations

Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 91  Solved: 54

Description

随机排列生成算法 (运行a.exe输出数字的个数,运行a.exe test时输出为一次随机的排列)

Input

The input will be a list of integers

Output

The number of the integers

Sample Input

1 2 3 4 5 6 7 8 9 10

Sample Output

10
 
思路:产生元素数目为n的无重随机数列
—>产生随机数,确定不重复后才加入数组
—>确定不重复用循环?循环后如何有把握新的数也无重复?
—>两层?三层?不保险
—>递归?写一写,三个函数,赋值,比较,主函数
—>搞定

 #include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int seed=time();
int b[]={};
int test(int b[],int i,int temp)
{
for (int j=;j<i;j++)
{
if (b[j]==temp) return -;
}
return ;
}
void give_test(int b[],int temp,int i,int n)
{
seed++;
srand(seed);
temp=rand()%n;
if (test(b,i,temp)==-)
{
seed++;
give_test(b,temp,i,n);
}
else b[i]=temp;
}
void random_permute(int a[],int n)
{
srand(seed);
b[]=rand()%n;
int temp=;
for (int i=;i<n;i++)
{
give_test(b,temp,i,n);
}
for (int i=;i<n;i++)
cout<<a[b[i]]<<" ";
}
int main(int argc,char *argv[])
{
int n;
cin>>n;
int a[];
for (int i=;i<n;i++)
cin>>a[i];
random_permute(a,n);
cout<<n<<endl;
return ;
}

验证可行

—>修改符合oj设计(argc,argv……受不了了,先睡……改天再说)

—>逗了,原题未输入元素个数……改一改再说

—>拓展:http://www.cnblogs.com/eaglet/archive/2011/01/17/1937083.html 不经过如此多比较去重的较高效算法

 

oj 1031 random permutation的更多相关文章

  1. numpy.random.shuffle()与numpy.random.permutation()的区别

    参考API:https://docs.scipy.org/doc/numpy/reference/routines.random.html 1. numpy.random.shuffle()   AP ...

  2. Python之np.random.permutation()函数的使用

    官网的解释是:Randomly permute a sequence, or return a permuted range. 即随机排列序列,或返回随机范围.我的理解就是返回一个乱序的序列.下面通过 ...

  3. np.random.shuffle(x)与np.random.permutation(x)

    来自:https://blog.csdn.net/brucewong0516/article/details/79012233 将数组打乱随机排列 两种方法: np.random.shuffle(x) ...

  4. Light OJ 1031 - Easy Game(区间dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1031 题目大意:两个选手,轮流可以从数组的任意一端取值, 每次可以去任意个但仅 ...

  5. LeetCode OJ 31. Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. 九度oj 1031 xxx定律 2009年浙江大学计算机及软件工程研究生机试真题

    题目1031:xxx定律 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5153 解决:3298 题目描述:     对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n ...

  7. LeetCode OJ:Next Permutation(下一排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. 九度OJ 1031:xxx定律 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6058 解决:3816 题目描述:     对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数 ...

  9. Light OJ 1060 - nth Permutation(组合数)

    题目大意: 给你一个字符串,问这个字符串按照特定顺序排列之后,第n个字符串是哪个? 题目分析: 首先我们要会求解总个数.也就是共有len个字符,每个字符有ki个,那么总组合方式是多少种? 总组合方式就 ...

随机推荐

  1. js:数据结构笔记14--高级算法

    动态规划: 递归是从顶部开始将问题分解,通过解决所有分解出小问题来解决整体问题: 动态规划从底部开始解决问题,将所有小问题解决,然后合并掉一个整体解决方案: function dynFib(n) { ...

  2. http://www.cnblogs.com/yjmyzz/p/3941043.html

    http://www.cnblogs.com/yjmyzz/p/3941043.html

  3. POJ2115 C Looooops(线性同余方程)

    无符号k位数溢出就相当于mod 2k,然后设循环x次A等于B,就可以列出方程: $$ Cx+A \equiv B \pmod {2^k} $$ $$ Cx \equiv B-A \pmod {2^k} ...

  4. c# windows service

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. BZOJ3640 : JC的小苹果

    设$f[i][j]$表示$hp$为$i$,在$j$点的概率,$d[i]$表示$i$的度数,$w[i]$表示经过$i$点要扣掉的血量. 对于$j$到$k$这条边,$f[i-w[k]][k]+=\frac ...

  6. word统计章节字数

    只有我这么无聊了..写论文的时候发现每次想看这个章节的字数统计时,还需要全选然后再看字数统计,太麻烦了.所以想着用宏写个能直接查看选定章节的字数统计. 具体方法如下: 建立宏:视图--宏--录制宏-- ...

  7. 【BZOJ】1602: [Usaco2008 Oct]牧场行走(lca)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1602 一开始以为直接暴力最短路,但是n<=1000, q<=1000可能会tle. 显然 ...

  8. 【BZOJ】1054: [HAOI2008]移动玩具(bfs+hash)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1054 一开始我还以为要双向广搜....但是很水的数据,不需要了. 直接bfs+hash判重即可. # ...

  9. Graph database_neo4j 底层存储结构分析(5)

    3.5 Property 的存储 下面是neo4j graph db 中,Property数据存储对应的文件: neostore.propertystore.db neostore.propertys ...

  10. 使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解

    本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据.2)创建json数组和解析json数组 1. 创建json,从json中获取数据 #include <stdio. ...