帮忙用C或C++解决这两道题
来源:百度知道 编辑:UC知道 时间:2024/09/23 19:26:29
1. Suppose you have an array of integer.
Int [] my Array= {1, 2, 3, 4, 5}
Please implement a method to randomize that array, for example the array can be like:
2, 5,1,3,4 after you call the method. Use only the function rand() to generate a unique decimal number from 0 to 1.
2. Implement a method that returns a prime number from 1-100. Please do not hardcode any values.
Int [] my Array= {1, 2, 3, 4, 5}
Please implement a method to randomize that array, for example the array can be like:
2, 5,1,3,4 after you call the method. Use only the function rand() to generate a unique decimal number from 0 to 1.
2. Implement a method that returns a prime number from 1-100. Please do not hardcode any values.
1、仅用rand()函数,打乱数组。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int myArray[]= {1, 2, 3, 4, 5};
int i,j,n,m,t;
m=sizeof(myArray)/sizeof(int);
n=m+rand()%20;
for(i=0;i<m;i++)
{
j=rand()%m;
t=myArray[i];
myArray[i]=myArray[j];
myArray[j]=t;
}
return 0;////main函数要有返回值
}
2、找出1-100的质数。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n;
for(i=2;i<=100;i++)
{
for(n=2;n<i;n++)
{
if(i%n==0)
{
break;
}
}
if(n==i)
{
printf("%d ",i);
}
}
return 0;
}
写成中文行不?看不懂英文啊
随机数?
质数?