C++随机数小程序
#include <iostream>
#include <time.h>
#include <windows.h>
#include <tchar.h>
using namespace std;
//打印界面
void love() {
float x, y;
for (y = 1.5f; y > -1.5f; y -= 0.1f)
{
for (x = -1.5f; x < 1.5f; x += 0.05f)
{
float a = x * x + y * y - 1;
if ((a*a*a - x * x*y*y*y) <= 0)//心形方程
cout << '*';
else cout << " ";
}
cout << endl;
}
}
int main() {
love();
cout << endl;
int MaxNumber = 0;
cout << "请输入最大数:";
cin >> MaxNumber;
cout << "最大数为->" << MaxNumber << endl;
unsigned rspeed;
srand(time(NULL));
int rnumber = rand() % MaxNumber+ 1; //因为是从0开始的所以要+1
printf("随机数结果:%d", rnumber);
getchar();
return 0;
}
效果图


这个C++程序首先展示了一个心形图案,然后生成并显示了一个介于0到用户指定最大数之间的随机数。程序使用了srand和rand函数来生成随机数,并结合时间函数确保每次运行的随机数序列不同。
2098

被折叠的 条评论
为什么被折叠?



