实验1

task1

编程代码

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

 

 运行截图

 

 task2

编程代码

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

运行图片

 

task2

编程代码

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

运行图片

问题

写法1  sum=n*(n+1)/2  和写法2  sum=(n+1)*n/2  可以实现计算1+2+3+...+n的结果

写法3和写法4不能  因为其定义为整数类型,先  /2会导致数值不准确

 

task3

编程代码

#include<stdio.h>
#include<stdlib.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 的值的互换

 

task4

编程代码

#include<stdio.h>
#include<stdlib.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; 
}

运行截图

问题使t为x的数值倒过来

 

 

task5

 编程代码

#include<stdio.h>
#include<stdlib.h>
int main()
{
   float a,b,c;
   scanf("%f %f %f",&a,&b,&c);
   if(a+b>c&&a-b<c)
       printf("能构成三角形\n");
   else
       printf("不能构成三角形\n");
        system("pause");
    return 0; 
}

运行截图

 

task6

编程代码

#include<stdio.h>
#include<stdlib.h>
int main()
{
   int year;
       year=1e+9/31536000+0.5;
       printf("10亿秒约等于%d年\n",year);
        system("pause");
    return 0; 
}

 

运行截图

 

 

task7

 

编程代码

 

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

 

运行截图

 

 

 

task8

 

编程代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
  char ans1,ans2;
  printf("每次课前认真预习,课后认真复习了没?(输入y或Y表示有,输入n或N表示没有):");
      ans1=getchar();
  getchar();
      printf("\n动手敲代码了没?(输入y或Y表示敲了,输入n或N表示没有敲):");
      ans2=getchar();

  if((ans1=='y'||ans1=='Y')&&(ans2=='y'||ans2=='Y'))
  printf("\n罗马不是一天建成的,继续保持哦\n");
  else
      printf("\罗马不是一天毁灭的,我们来建设吧\n");
        system("pause");
    return 0; 
}

 

运行图片

 

 

posted @ 2023-03-07 11:00  无良无常  阅读(16)  评论(0编辑  收藏  举报