アプリ版:「スタンプのみでお礼する」機能のリリースについて

C言語 プログラム

1~45の数字の中からランダムに15ペアの組み合わせと残りの数字を表示するようなプログラムをつくりたいのですが,どなたか作ってくれませんか?

以下のような表示例みたいなのが,嬉しいです。お願いします。


Ex.

./ random

6-27
5-22
4-19
1-21
15-14
33-40
17-36
37-28
13-42
23-41
34-24
7-35
38-11
12-8
26-18

43
9
3
25
16
2
44
29
30
39
45
10
20
32
31

A 回答 (3件)

乱数とソートが出来れば作れると思う。



#include <stdio.h>
#include <stdlib.h>
typedef struct _h_t{int n; double r;}h_t;
int cmp(const h_t *a,const h_t *b);
int main(){h_t h[45];int i;srand((unsigned)time(NULL));for(i=0;i<45;i++){h[i].n=i+1;h[i].r=(double)rand()/RAND_MAX;}qsort(h,45,sizeof(h_t),(int(*)(const void*,const void*))cmp);for(i=0;i<15;i++){printf("%d-%d\n",h[i*2].n,h[i*2+1].n);}for(i=30;i<45;i++){printf("%d\n",h[i].n);}return 0;}
int cmp(const h_t *a,const h_t *b){if(a->r<b->r)return -1;else if(a->r>b->r)return 1;return 0;}

参考URL:http://codepad.org/ZhYwjH4B
    • good
    • 0
この回答へのお礼

ありがとうございました。
これを基に頑張って作ってみます。

お礼日時:2013/05/24 23:22

作りたいけれど作ってほしいという微妙な心理が盛り込まれた質問ですね。


Cのプログラムは自分で書いていただくとして、ヒントをかねてC++のプログラムを作ってみました。

#include <array>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iterator>
#include <boost/iterator/counting_iterator.hpp>

int main()
{
  std::array<int, 45> a;
  std::copy(boost::make_counting_iterator<int>(1), boost::make_counting_iterator<int>(46), a.begin());
  std::random_shuffle(a.begin(), a.end());
  std::for_each(boost::make_counting_iterator<int>(0), boost::make_counting_iterator<int>(15),
    [&a](int i) { std::cout << a[i * 2] << '-' << a[i * 2 + 1] << std::endl; });
  std::cout << std::endl;
  std::copy(a.begin() + 15 * 2, a.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
    • good
    • 0
この回答へのお礼

これを基に頑張ります。
ありがとうございました。

お礼日時:2013/05/24 23:21

>プログラムをつくりたいのですが,どなたか作ってくれませんか?



この文に矛盾を含んでいることにお気づきでしょうか。
プログラムを作りたい、というのは、質問者さんの意志ですよね。
ところが、すぐ後では、だれかに作ってほしい、と書かれています。

ご自分でコードを書きたいのかそうではないのか、どちらなんでしょうか。
    • good
    • 0
この回答へのお礼

そうですね。
作ってほしかったです。
日本語気をつけます。

お礼日時:2013/05/24 23:22

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!