实验一
任务一
#include <stdlib.h> #include <stdio.h> int main() { printf(" 0"" 0\n"); printf("<H>""<H>\n"); printf("I I""I I\n"); system("pause"); return 0; }
#include <stdlib.h>
#include <stdio.h>
int main()
{
printf(" 0\n");
printf("<H>\n");
printf("I I\n");
system("pause");
return 0;
}

任务二#include <stdlib.h>
#include <stdio.h>
int main()
{
int n,sum;
scanf("%d", &n);
sum = n*(n+1)/2;
printf("sum=%d\n",sum);
system("pause");
return 0;
}

第三个和第四个不行,因为如果n是奇数,在int类型下,除以二就会损失一部分数,,数会变小。
任务三
#include <stdlib.h>
#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);
system("pause");
return 0;
}

功能是将a和b的值互换
任务四
#include <stdlib.h>
#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);
system("pause");
return 0;
}

功能是实现个位数和百位数的互换
任务五
#include <stdlib.h>
#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");
system("pause");
return 0;
}

任务六
#include <stdlib.h>
#include <stdio.h>
int main()
{
int year;
year=(int)(1e9/3600/24/365+0.5);
printf("10亿秒等于%d年\n",year);
system("pause");
return 0;
}

任务七
#include <stdlib.h>
#include <stdio.h>
int main()
{
int n;
srand(time(0));
n=60+rand()%41;
printf("n=%d",n);
system("pause");
return 0;
}

任务八
#include <stdio.h>
int main()
{
char ans1,ans2;
printf("每次课前认真预习,及时复习了吗(输入y或者Y表示有,nN表示没有)");
ans1=getchar();
getchar();
printf("\n手动敲代码了吗(输入y或者Y表示有,nN表示没有)");
ans2=getchar();
if(ans1=='y'||ans1=='Y' && ans2=='y'||ans2=='Y')
printf("\n罗马不是一天建成的,继续保持\n");
else
printf("\n罗马不是一天毁灭的,快来建设吧\n");
system("pause");
return 0;
}
浙公网安备 33010602011771号