实验任务1
#include <stdlib.h>
#include <stdio.h>
#include<time.h>
#define N 5
int main() {
int number;
int i;
srand(time(0));
for (i = 1; i < N; ++i) {
number = rand() % 500 + 1;
printf(“20228329%04d\n”, number);
}
return 0;
}
问题1:功能是产生一个以时间为种子500及以内的随机数并赋值给number
问题2:产生一个根据时间变化的随机学号
实验任务2
#include <stdlib.h>
#include <stdio.h>
#include<time.h>
int main() {
int j, x, y;
printf(“Guess which day is your lucky day.\nYou totally have three chances to try.\n”);
srand(time(0));
y = rand() % 30+1;
for (j = 1; j <= 3; j++) {
printf(“Enter your answer:”);
scanf_s(“%d”, &x);
if (y == x) { printf(“Yes,you are right.\n”); break; }
else if (x > y) { printf(“More\n”); }
else if (x < y) { printf(“Less\n”); }
}
if (y != x){
printf(“Your chances are used up.The true date is:%d”,y);
}
return 0;
}
实验任务3
#include <stdlib.h>
#include <stdio.h>
int main() {
char y;
printf(“Enter the color of traffic light:\n”);
printf(“r for red,g for green,y for yellow\n”);
while (1) {
y = getchar();
getchar();
if (y == ‘r’)printf(“Stop! \n”);
else if (y == ‘g’)printf(“go go go \n”);
else if (y == ‘y’)printf(“Wait a minute \n”);
else printf(“Something must be wrong \n”);
if (y == EOF)break;
}
return 0;
}
实验任务4
#include <stdlib.h>
#include <stdio.h>
#include<math.h>
int main() {
while (1) {
double s, ans = 0.0;
int k = 0;
int n, a, i, m;
scanf_s(“%d %d”, &n, &a);
for (i = 1; i <= n; i++) {
for (m = 1; m <= i; m++) {
k = a * (pow(10, m) – 1) / 9;
}
ans += double (i) / k;
}
printf(“n=%d,a=%d,s=%lf \n”, n, a, ans);
}
return 0;
}
实验任务5
#include <stdlib.h>
#include <stdio.h>
#include<math.h>
int main() {
int j = 1, k = 1;
int m=1, ans;
while (j <= 9) {
k = 1,m=1;
while (k <= j) {
ans = 0;
ans = m * j;
printf(“%d * %d = %d “, m, j, ans), k++;
m++;
}
j++;
printf(“\n”);
}
return 0;
}
实验任务6
#include <stdlib.h>
#include <stdio.h>
#include<math.h>
int main() {
int line,m,n,j;
scanf_s(“%d”, &line);
for (m = 1; m <= line; m++) {
for (j = m; j > 1; j–) {
printf(“\t”);
}
for (n = 2 * m – 1; n <= 2 * line – 1; n++) {
printf(” o \t”);
}
printf(“\n”);
for (j = m; j > 1; j–) {
printf(“\t”);
}
for (n = 2 * m – 1; n <= 2 * line – 1; n++) {
printf(“<H>\t”);
}
printf(“\n”);
for (j = m; j > 1; j–) {
printf(“\t”);
}
for (n = 2 * m – 1; n <= 2 * line – 1; n++) {
printf(“I I\t”);
}
printf(“\n”);
}
return 0;
}
当输入为n时:
第i行,需要打印2n-2i+1个字符小人
第i行,需要打印i-1个空白
原文地址:http://www.cnblogs.com/molsonxx/p/16807909.html