VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 625|回复: 2

为什么每个学生的随机数都是同一个啊!拜托了!!!

[复制链接]
66_avatar_middle
最佳答案
0 
在线会员 发表于 2020-5-5 13:35:25 | 显示全部楼层 |阅读模式
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
struct student
{
    string s_name;
    int score;
};
struct teacher
{
    string t_name;
    student s[5];
};
void printf_t_s(struct teacher t[], int len)
{
    string nameid = "ABCDE";
    for (int i = 0; i < len; i++)
    {
        srand((unsigned int)time(NULL));//添加随机数种子 作用:利用当前系统时间生成随机数,防止每次生成随机数都一样
        int suiji = rand() % 101;//系统生成随机数,101是0~100
        t[i].t_name = "teacher_";
        t[i].t_name += nameid[i];//可在其后利用下标和+=添加
        for (int j = 0; j < 5; j++)
        {
            t[i].s[j].s_name = "student_";
            t[i].s[j].s_name += nameid[j];
            t[i].s[j].score = suiji;
        }
    }
}
int main()
{
    struct teacher arrt[3];
    struct student arrs[5];
    int len = sizeof(arrt) / sizeof(arrt[0]);
    printf_t_s(arrt, len);
    for (int i = 0; i < 3; i++)//打印数组元素
    {
        cout << arrt[i].t_name << endl;
        for (int j = 0; j < 5; j++)
        {
            cout << arrt[i].s[j].s_name << " " << arrt[i].s[j].score << endl;
        }
    }
    return 0;
}#include<iostream>
#include<string>
#include<ctime>
using namespace std;
struct student
{
    string s_name;
    int score;
};
struct teacher
{
    string t_name;
    student s[5];
};
void printf_t_s(struct teacher t[], int len)//struct teacher t[]相当于用了指针,它知道要传进来一个数组,这样会更加直观
{
    string nameid = "ABCDE";
    for (int i = 0; i < len; i++)
    {
        srand((unsigned int)time(NULL));//添加随机数种子 作用:利用当前系统时间生成随机数,防止每次生成随机数都一样
        int suiji = rand() % 101;//系统生成随机数,101是0~100
        t[i].t_name = "teacher_";
        t[i].t_name += nameid[i];//可在其后利用下标和+=添加
        for (int j = 0; j < 5; j++)
        {
            t[i].s[j].s_name = "student_";
            t[i].s[j].s_name += nameid[j];
            t[i].s[j].score = suiji;
        }
    }
}
int main()
{
    struct teacher arrt[3];
    struct student arrs[5];
    int len = sizeof(arrt) / sizeof(arrt[0]);
    printf_t_s(arrt, len);
    for (int i = 0; i < 3; i++)//打印数组元素
    {
        cout << arrt[i].t_name << endl;
        for (int j = 0; j < 5; j++)
        {
            cout << arrt[i].s[j].s_name << " " << arrt[i].s[j].score << endl;
        }
    }
    return 0;
}




上一篇:为什么输出学生的后面都是E?拜托各位大佬了!!!
下一篇:VS2015卸载再安装后 启动卡住了 怎么办啊
66_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2020-5-5 13:36:39 | 显示全部楼层
问题真是层出不穷
94_avatar_middle
最佳答案
4 
donate_vip 发表于 2020-5-5 16:03:29 | 显示全部楼层
jia_you 发表于 2020-5-5 13:36
问题真是层出不穷

hello?
种子只需要初始化一次就可以

评分

参与人数 1威望 +2 驿站币 +2 热心值 +2 收起 理由
51_avatar_small Syc + 2 + 2 + 2 正解!

查看全部评分

您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-6-10 06:43

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表