实验一
TASK 1
#include <stdio.h>
int main()
{
printf(" o o\n");
printf("<H> <H>\n");
printf("I I I I\n");
return 0 ;
}
#include <stdio.h>
int main()
{
printf(" o\n");
printf("<H>\n");
printf("I I\n");
printf(" o\n");
printf("<H>\n");
printf("I I\n");
return 0;
}
TASK 2
#include<stdio.h>
int main()
{
int n,sum;
scanf("%d",&n);
sum=n*(1+n)/2;
printf("sum = %d\n",sum);
return 0;
}
#include<stdio.h>
int main()
{
int n,sum;
scanf("%d",&n);
sum=(1+n)*n/2;
printf("sum = %d\n",sum);
return 0;
}
#include<stdio.h>
int main()
{
int n,sum;
scanf("%d",&n);
sum=n/2*(n+1);
printf("sum = %d\n",sum);
return 0;
}
#include<stdio.h>
int main()
{
int n,sum;
scanf("%d",&n);
sum=(n+1)/2*n;
printf("sum = %d\n",sum);
return 0;
}
第三与第四种写法是错误的,当测试数据为单数时,第三种有问题,整数型除以2时会丢失小数部分,当测试数据为偶数时,第四种有问题,与第三种同理
TASK 3
#include<stdio.h>
int main()
{
int a, b, t;
a = 3;
b = 4;
printf("a = %d, b = %d\n", a, b);
t = a;
a = b;
b = t;
printf("a = %d, b = %d\n", a, b);
return 0;
}
第11行至第13行是将两数互换
TASK 4
#include<stdio.h>
int main()
{
int x, t, m;
x = 123;
printf("x = %d\n", x);
t = 0;
m = x % 10;
t = t *10 + m;
x = x / 10;
m = x % 10;
t = t * 10 + m;
x = x / 10;
m = x % 10;
t = t * 10 + m;
x = x / 10;
printf("t = %d\n",t);
return 0;
}
t=321
t=654
第10至第22行作用为互换位置
TASK5
#include <stdio.h>
int main()
{
float a, b, c;
scanf("%f%f%f", &a, &b, &c);
if(a+b>c & a+c>b & b+c>a)
printf("能构成三角形\n");
else
printf("不能构成三角形\n");
return 0;
}
TASK 6
#include<stdio.h>
int main()
{
int year;
year=1000000000/60/60/24/365;
printf("十亿秒约等于%d年\n", year);
return 0;
}
TASK 7
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n;
srand(time(0));
n=rand()%40+61;
printf("n = %d\n", n);
return 0;
}
TASK 8
#include<stdio.h>
int main()
{
char ans1, ans2;
printf("每次课前认真预习·课后及时复习了没?(输入y或Y表示有, 输入n或表示没有) : ");
ans1 = getchar();
getchar();
printf("动手敲代码实践没有?(输入y或Y表示敲了,输入n或N表示木有敲) : ");
ans2 = getchar();
if ((ans1 == 'y' || ans1 == 'Y') && (ans2 == 'y' || ans2 == 'Y'))
printf("\n罗马不是一天建成的,继续保持哦:)\n");
else
printf("\n罗马不是一天毁灭的,我们来建设吧:)\n");
return 0;
}