[V0.1.0]Random problem

input file:in.txt    output file:out.txt
time limit: 500ms
language:ASM、C、C++、C#、Java、VB

Difficulty:★
Describe:

    Assume that you have m unit cubes(whose edge lengths is 1) and a 3 - dimensional container .Your simple task is to put these cubes in random different integer coordinates of the container.

Input:

    The first line:Three interger x,y,z(0<x,y,z<50)separated by whitespace, denoting the lengths of the 3 - dimensional container on each dimensional ;

    The second line:A single interger m,denoting the number of cubes(0<m<=x*y*z);

    The following m lines:A single string,denoting the name of each unit cube.
Output:
    For each unit cube, output one line containing the coordinate of the cube in the format shown in the sample output.
Sample Input:

2 3 2
4
方块A
方块B
方块C
方块D
Sample Output:

方块A (2,1,2)
方块B (1,2,2)
方块C (1,1,2)
方块D (2,3,1)

Solution:

//C#4.0 Console Application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace RapidProtyping0_1_0
{

    struct point3d
    {
        public int x, y, z;
        public point3d(int x, int y, int z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            using (StreamReader sr = new StreamReader("in.txt", System.Text.Encoding.GetEncoding("GB2312")))
            {
                using (StreamWriter sw = new StreamWriter("out.txt"))
                {
                    readinfo(sr, sw);
                }
            }
        }


        static void readinfo(StreamReader sr, StreamWriter sw)
        {
            int x, y, z;
            string line = sr.ReadLine();
            setXYZ(out x, out y, out z, line);
            int n = Convert.ToInt32(sr.ReadLine());
            point3d[] parray = new point3d[n];
            for (int i = 0; i < n; i++)
            {
                string line2 = sr.ReadLine();
                int x1, y1, z1;
                getRandomPoint(parray, i, x, y, z, out x1, out y1, out z1);
                point3d pt = new point3d(x1, y1, z1);
                parray[i] = pt;
                sw.WriteLine(line2 + " (" + x1 + "," + y1 + "," + z1 + ")");
            }

        }

        static private void getRandomPoint(point3d[] plist, int i, int x, int y, int z, out int x1, out int y1, out int z1)
        {
            do
            {
                Random r = new Random();
                x1 = r.Next(1, x + 1);
                y1 = r.Next(1, y + 1);
                z1 = r.Next(1, z + 1);
            }
            while (Exist(x1, y1, z1, plist, i));

        }

        static private bool Exist(int x1, int y1, int z1, point3d[] plist, int i)
        {
            for (int j = 0; j < i; j++)
            {
                if (plist[j].x == x1 && plist[j].y == y1 && plist[j].z == z1)
                    return true;
            }
            return false;
        }

        static private void setXYZ(out int x, out int y, out int z, string line)
        {
            string[] s = line.Split(' ');
            x = Convert.ToInt32(s[0]);
            y = Convert.ToInt32(s[1]);
            z = Convert.ToInt32(s[2]);
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值