0

与えられたのは、a、b、c、d を想定した名前のリストです。これらの名前のシークレット サンタを生成したいのですが、許可されている出力は次のとおりです。

name secretsanta
a      b
b      d
c      a
d      c

注: 複数の名前で同じシークレット サンタを取得する場合を除き、すべての出力が有効です。

a->a
b->a
c->d
d->b

上記は許可されていません

また、入力に対して正しい出力を一度取得し、同じ入力で再度実行すると、同じ結果が再び得られることはありません。つまり、 a->b b->d c->a d->c これはすぐに繰り返されません。その後、異なる出力が少なくとも 1 回表示された後にのみ、繰り返すことができます。

以下は私が試したコードです:

do
{
    total_size=ss.santa.size();     
    Collections.shuffle(ss.santa);
    System.out.println("Below is the list of names with their secret santas");
    System.out.println("Participant    Secret Santa");
    Iterator<?> itr=ss.names.iterator();

    while(itr.hasNext())
    {
        String name=(String)itr.next(); 
        String SecretName;
        do
        {
            int rand=r.nextInt(total_size);
            SecretName=ss.santa.get(rand);          
        }while(name.equals(SecretName) || s.contains(SecretName) );
        s.add(SecretName);
        System.out.println(name+"    "+SecretName);         
    }

    s.removeAll(ss.names);
    Collections.shuffle(ss.santa);
    System.out.println("do you want to rerun??");
    System.out.println(" 1-YES 2-NO");
    choice=scn.nextInt();
}while(choice==1);
4

1 に答える 1