| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 980 人关注过本帖
标题:[问题]老师留的一道C#的作业?我不太懂??
只看楼主 加入收藏
龙行小天下
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-3-31
收藏
 问题点数:0 回复次数:7 
[问题]老师留的一道C#的作业?我不太懂??
利用随机数模拟10000个人的年龄和性别,以5年为一个年龄段对年龄和性别进行统计,给出统计结果(for和if...else语句的使用)

下面给出随机数的生成过程:
Random ran=new Randow();
ran.Next(max Value);//生成不大于 max Value值的非负整数
ran.Next(min Value,max Value);//生成介于min Value值 和 max Value值之间的一个整数

请教大家

[[it] 本帖最后由 龙行小天下 于 2008-3-31 16:58 编辑 [/it]]
搜索更多相关主题的帖子: Value ran 作业 max 老师 
2008-03-31 16:44
龙行小天下
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2008-3-31
收藏
得分:0 
顶一下,会的朋友帮帮忙吧
2008-03-31 18:34
blueskyss
Rank: 1
来 自:湖北
等 级:新手上路
帖 子:81
专家分:0
注 册:2008-2-19
收藏
得分:0 
static void Main(string[] args)
        {
            int[][] arr = new int[1000][];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = new int[2];
            }

            Random ran = new Random();
            for (int j = 0; j < arr.Length; j++)
            {
                arr[j][0] = ran.Next(1, 120);
                arr[j][1] = ran.Next(0,2);
            }
            int sum1 = 0;
            int sum2 = 0;
            for (int j = 0; j < arr.Length; j++)
            {
                if (arr[j][0] >= 0 && arr[j][0] <= 5)
                {
                    sum1++;
                    if (arr[j][1] ==0)
                    {
                        sum2++;
                    }
                  
                }
               
            }
            Console.WriteLine("0~5岁的人数为:{0}男性为:{1} ", sum1, sum2);
}
2008-03-31 20:18
blueskyss
Rank: 1
来 自:湖北
等 级:新手上路
帖 子:81
专家分:0
注 册:2008-2-19
收藏
得分:0 
只写了一部分
2008-03-31 20:21
冢骨
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2008-3-14
收藏
得分:0 
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] p = new int[2,10000];//第一行为人的年龄,第二行为性别用0表示女,用1表示男。
            int[,] all = new int[2, 20];//第一行统计以5年为年龄段的女人人数,第二行统计以5为年龄段男人的人数。
            int c = 0;

            for (int i = 0; i < 20; i++)
            {
                all[0, i] = 0;
                all[1, i] = 0;
            }
            
            
            
            Random ran=new Random();
            for (int i = 0; i < 10000; i++)
            {
               
                p[0,i] = ran.Next( 0,100 );//对人进行随机赋年龄值。
                p[1, i] = ran.Next(0,2);//用0表示女,1表示男。

                if (0 < p[0, i] && p[0, i] <= 5)//判断是否在年龄段内。
                {
                    if ( p[1,i]==0 )//判断是否为女。
                    {
                        all[0,0]++;//这个年龄段的女人人数加1.
                    }
                    else//不为女则为男。
                    {
                        all[1, 0]++;//这个年龄段的男人人数加1.
                    }
                }

                if (5 < p[0, i] && p[0, i]<= 10)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,1]++;
                    }
                    else
                    {
                        all[1,1]++;
                    }
                }

                if (10 < p[0, i] && p[0, i] <= 15)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,2]++;
                    }
                    else
                    {
                        all[1,2]++;
                    }
                }

                if (15 < p[0, i] && p[0, i] <= 20)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,3]++;
                    }
                    else
                    {
                        all[1,3]++;
                    }
                }

                if (20 < p[0, i] && p[0, i] <= 25)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,4]++;
                    }
                    else
                    {
                        all[1,4]++;
                    }
                }

                if (25 < p[0, i] && p[0, i] <= 30)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,5]++;
                    }
                    else
                    {
                        all[1,5]++;
                    }
                }

                if (30 < p[0, i] && p[0, i] <= 35)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,6]++;
                    }
                    else
                    {
                        all[1,6]++;
                    }
                }

                if (35 < p[0, i] && p[0, i] <= 40)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,7]++;
                    }
                    else
                    {
                        all[1,7]++;
                    }
                }

                if (40 < p[0, i] && p[0, i] <= 45)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,8]++;
                    }
                    else
                    {
                        all[1,8]++;
                    }
                }

                if (45 < p[0, i] && p[0, i] <= 50)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,9]++;
                    }
                    else
                    {
                        all[1,9]++;
                    }
                }

                if (50 < p[0, i] && p[0, i] <= 55)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,10]++;
                    }
                    else
                    {
                        all[1,10]++;
                    }
                }

                if (55 < p[0, i] && p[0, i] <= 60)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,11]++;
                    }
                    else
                    {
                        all[1,11]++;
                    }
                }

                if (60 < p[0, i] && p[0, i] <= 65)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,12]++;
                    }
                    else
                    {
                        all[1,12]++;
                    }
                }

                if (65 < p[0, i] && p[0, i] <= 70)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,13]++;
                    }
                    else
                    {
                        all[1,13]++;
                    }
                }

                if (70 < p[0, i] && p[0, i] <= 75)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,14]++;
                    }
                    else
                    {
                        all[1,14]++;
                    }
                }

                if (75 < p[0, i] && p[0, i] <= 80)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,15]++;
                    }
                    else
                    {
                        all[1,15]++;
                    }
                }

                if (80 < p[0, i] && p[0, i] <= 85)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,16]++;
                    }
                    else
                    {
                        all[1,16]++;
                    }
                }

                if (85 < p[0, i] && p[0, i] <= 90)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,17]++;
                    }
                    else
                    {
                        all[1,17]++;
                    }
                }

                if (90 < p[0, i] && p[0, i] <= 95)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,18]++;
                    }
                    else
                    {
                        all[1,18]++;
                    }
                }

                if (95 < p[0, i] && p[0, i] <= 100)
                {
                    if (p[1, i] == 0)
                    {
                        all[0,19]++;
                    }
                    else
                    {
                        all[1,19]++;
                    }
                }
            }

            for (int i = 0; i < 20; i++)
            {
                Console.Write("{0}-", c);
                c += 5;
                Console.WriteLine("{0}: woman {1} , man {2}", c, all[0, i], all[1, i]);
            }

           
        }
    }
}

[[it] 本帖最后由 冢骨 于 2008-3-31 20:52 编辑 [/it]]
2008-03-31 20:46
冢骨
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2008-3-14
收藏
得分:0 
感觉运算时间有点久
各位大虾看看能不能优化
2008-03-31 20:55
冢骨
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2008-3-14
收藏
得分:0 
楼主 如果老师给出答案你记得要发上来啊
2008-03-31 22:35
bccnjason
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-3-30
收藏
得分:0 
感觉速度还可以
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication2
{
    class people
    {
        private int age;
        private bool sex;

        public people()
        {
            age = 0;
            sex = true ;
        }

        public void setpeople()
        {
            Random r = new Random();
            age = r.Next(1, 100);
            int s = r.Next(0, 1);
            if (s == 0)
                sex = true;
            else
                sex = false;
        }

        public int Age
        {
            set { age = value; }
            get { return age; }
        }
        public bool Sex
        {
            set { sex = value; }
            get { return sex; }
        }
            

        
        public people(int a, bool s)
        {
            this.age = a;
            this.sex = s;
        }

      
    }
     
    class Program
    {
        

        static void Main(string[] args)
        {
            
            Random r = new Random();
            
            
            people[] qun = new people[10000];
            //初始化人群
            for (int i = 0; i < qun.Length; i++)
            {
                people p = new people();
                p.Age = r.Next(1, 100);
                int s = r.Next(0, 2);
                if (s == 1)
                {
                    p.Sex  = true;
                }
                else
                {
                    p.Sex = false;
                }
                qun[i] = p;            


            }

            //统计工作
            int[,] tongji = new int[20,2];

            for (int i = 0; i < qun.Length; i++)
            {
                int a = qun[i].Age/5;
                tongji[a,0] = tongji[a,0] + 1;
                if (qun[i].Sex == true)
                {
                    tongji[a,1] = tongji[a,1] + 1;
                }
                else
                {}

            }

            //显示工作
            for (int i = 0; i < 20; i++)
            {
                System.Console.WriteLine(i*5+"到"+(i*5+5)+"岁的人人数是:" + tongji[i,0].ToString() + ";男:" + tongji[i,1].ToString() + "人;女:" + (tongji[i,0] - tongji[i,1]).ToString() + "人;");
            }   
           

            
            System.Console.ReadLine();

        }
    }
}
2008-03-31 22:44
快速回复:[问题]老师留的一道C#的作业?我不太懂??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038848 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved