您的位置:首页 > 编程语言 > C#

体验C#——用几个小题看一下“集合”

2014-12-09 11:04 381 查看

一、随机将若干枚一角,5角,一元的硬币放在一个狭小的黑丝管道里,现要取出一枚5角的硬币使用,求首次取出5角硬币所用的次数。(用Stack类)

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

namespace StackCoinBox
{
    class CoinBox
    {
        //定义容纳硬币的栈
        Stack CoinStack = new Stack();
        //投放硬币,1,10,5
        public void PushCoin()
        {
            int number;
            Console.WriteLine("请输入硬币的面额,按0退出");
            number = int.Parse(Console.ReadLine());
            while (number != 0)
            {
                CoinStack.Push(number);
                number = int.Parse(Console.ReadLine());
            }
        }
        //统计拿到第一个5角硬币用的次数
        public int getCount()
        {
            
            int i=CoinStack.Count;
            while (i>0)
            {
                int number = (int)CoinStack.Pop();
                if (number == 5)
                {
                    return i;
                }
                i--;
                
            }
            return 0;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            CoinBox coinBox = new CoinBox();
            coinBox.PushCoin();
            Console.WriteLine("第一次拿到5角的硬币所用的次数为{0}次", coinBox.getCount());
            Console.ReadKey();
        }
    }
}

二、随机产生100个数放在队列中,统计队列中指定数的个数并输出(用Queue类)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace QueueNumber
{
    class QueueNumber
    {
        Queue NumQueue = new Queue();
        //向队列中随机添加数字
        public void addNum()
        {
            Random random = new Random();
            int i = 100;
            while (i > 0)
            {
                NumQueue.Enqueue(random.Next(100)+1);//将对象添加至Queue的结尾处
                i--;
            }
        }
        //查看队列的具体情况
        public void showQueue()
        {
            //遍历队列
            Console.WriteLine("这是队列的具体情况:");
            foreach (Object e in NumQueue)
            {
                Console.Write(" {0}",(int)e);//队列中保存的是Object对象,应当进行类型强制转化
            }
            Console.WriteLine();
        }
        //输入目的数,得到该数字出现的次数。
        public int getCount()
        {
            int i = NumQueue.Count;
            int count = 0;
            Console.WriteLine("请输入一个数");
            int n = int.Parse(Console.ReadLine());
            //遍历队列
            while (i > 0)
            { 
                if ((int)NumQueue.Dequeue() == n)<span style="font-family: Arial, Helvetica, sans-serif;">//移除并返回Queue开始出的对象</span>
                {
                    count++;
                }

                i--;
            }
            return count;
        }
    }
    class Program
    {
        
        static void Main(string[] args)
        {
            QueueNumber queueNumber = new QueueNumber();
            queueNumber.addNum();
            queueNumber.showQueue();
            Console.WriteLine("该数字出现的次数为{0}",queueNumber.getCount());
            Console.ReadKey();
        }
    }
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: