新手求教啊,求大神些进来帮忙.
题目:编写一个程序,利用stand()产生20个随机数,将所有偶数改成相应的负数,奇数不变,最后输出改变后的个数值。求解啊。。
#include <cstdio> #include <cstdlib> #include <ctime> #include <conio.h> // 程序入口 int wmain(int argc, wchar_t argv[], wchar_t envp[]) { srand(static_cast<unsigned int>(time(NULL))); rand(); for (int i = 0; i < 20; ++i) { int number = rand(); if ((number % 2) == 0) { number *= -1; } wprintf_s(L"%d\n", number); } _getwch(); return EXIT_SUCCESS; }
#include<stdio.h> #include<stdlib.h> #include<time.h> int main() { int i,n; srand((unsigned)time(NULL)); for(i=1;i<=20;i++){ n=rand(); if(n%2==0) n*=-1; printf("%d\n",n); } return 0; }