实验2
实验一
#include<stdio.h>
int main() {
int num;
scanf("%d", &num);
printf("2049%04d\n", num);
scanf("%d", &num);
printf("2049%04d\n", num);
scanf("%d", &num);
printf("2049%04d\n", num);
return 0;
}
输入的数未满四位,在前面用零补齐
#include<math.h>
int main() {
int num;
while(scanf("%d", &num)!=EOF)
printf("2049%04d\n", num);
return 0;
}
实验二
#include<stdio.h>
int main() {
char ans1, ans2;
printf("复习了没?(输入y或Y表示复习了,输入n或N表示没复习):");
ans1 = getchar();
getchar();
printf("动手敲代码了没(输入y或Y表示敲了,输入n或N表示没敲)");
ans2 = getchar();
getchar();
if ((ans1=='y'||ans1=='Y')&& (ans2 == 'y' || ans2 == 'Y'))
{
printf("罗马不是一天建成的:)\n");
}
else
{
printf("罗马不是一天毁灭的。。。\n");
}
return 0;
}
实验三
#include<stdio.h>
int main() {
char x;
x = getchar();
if (x >= '0' && x <= '9') {
printf("%c是数字字符\n", x);
}
else if ((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'))
{
printf("%c是英文字母\n", x);
}
else {
printf("%c是其他字符",x);
}
return 0;
}
实验四
#include<stdio.h>
int main() {
float s, ps,n, a, top, bot;
top = 1;
bot = 0;
s = 0;
while (scanf("%f,%f", &n, &a) == 2) {
while (top <= n)
{
bot = a + bot * 10;
ps = top / bot;
s = s + ps;
top++;
}
printf("%f\n", s);
}
return 0;
}
实验五
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
const int N = 5;
int main() {
int x, n;
srand(time(0));
for(n=1;n<=N;n++){
x = rand() % 31+1;
printf("%3d", x);
}
printf("\n");
return 0;
}
11行100改为31+1
实验六
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main() {
int x,date,n;
n = 0;
printf("猜猜2021年12月哪一天会是你的lucky day\n\n开始喽,你有三次机会,猜吧(1~31):");
srand(time(0));
x = rand() % 31+1;
while(n<3)
{
scanf("%d", &date);
if (date > x)
{
printf("你猜的日期晚了,lucky day悄悄溜到前面了\n\n");
if (n < 2)
{
printf("再猜");
}
}
else if (date < x)
{
printf("你猜的日期早了,lucky day还没到呢\n\n");
if (n < 2)
{
printf("再猜");
}
}
else if (date==0)
{
printf("猜中啦");
exit(0);
}
n++;
}
printf("次数用完啦,偷偷告诉你:12月,你的lucky day 是:%d", x);
return 0;
}
实验七
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main() {
int n,x;
x = 0;
scanf("%d", &n);
while (x<n)
{
x++;
printf(" O\n");
printf("<H>\n");
printf("I I");
printf("\n\n\n");
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main() {
int n,x,y,z;
x = 0;
y = 0;
z = 0;
scanf("%d", &n);
while (x<n)
{
x++;
printf(" O");
printf("\t");
}
printf("\n");
while (y < n)
{
y++;
printf("<H>");
printf("\t");
}
printf("\n");
while (z < n)
{
z++;
printf("I I");
printf("\t");
}
return 0;
}
#include<stdio.h>
int main() {
int n,x,y,z,num,line,bac1,bac2,bac3;
line=0;
scanf("%d", &n);
num = (2 * n - 1);
while (line < n)
{
x = 0;
y = 0;
z = 0;
bac1 = 0;
bac2 = 0;
bac3 = 0;
while (bac1<line)
{
bac1++;
printf(" \t");
}
while (x < num)
{
x++;
printf(" O");
printf("\t");
}
printf("\n");
while (bac2 < line)
{
bac2++;
printf(" \t");
}
while (y < num)
{
y++;
printf("<H>");
printf("\t");
}
printf("\n");
while (bac3< line)
{
bac3++;
printf(" \t");
}
while (z < num)
{
z++;
printf("I I");
printf("\t");
}
printf("\n");
num--;
num--;
line++;
}
return 0;