第一次上机作业

task1

//打印一个字符小人
#include <stdio.h>
#include <stdlib.h>
int main()
{
    printf(" 0 \n");
    printf("<H>\n");
    printf("I I\n");
    printf(" 0 \n");
    printf("<H>\n");
    printf("I I\n");
    system("pause");
    return 0;
 } 

 

//打印一个字符小人
#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;
 } 

 

写法3和写法4不能实现题目的要求

原因:由于运算顺序不同写法3先让n/2当n=51时会有误差,而运算4让(n+1)/2在前同样会

使计算n=50时出现误差

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;
}

 

 

示例11-13行的作用是:交换a和b的数值

task4

#include <stdio.h>
#include <stdlib.h>

int main ()
{
    int x,t,m;
    
    x=456;
    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;
}

 

 

实例line10-22实现的功能:将数字的个位十位百位上的数字变成百位十位个位上的数字。

task5

//从键盘上输入三个三角形的边长,判断其能否构成三角形

#include <stdio.h>
#include<stdlib.h>
int main()
{
    float a,b,c;
    //输入3条边长 
    scanf("%f,%f,%f",&a,&b,&c);
    //判断是否能构成三角形 
    if(a+b>c&&b+c>a&&c+a>b)
    printf("能构成三角形\n");
    else
    printf("不能构成三角形\n");
    system("pause");
    return 0;  
}

 

 

task6

//计算10亿秒等于多少年,并打印出
#include <stdio.h>
#include <math.h>
#include <stdlib.h> 

int main()
{
    int year;
    year = pow(10,9)/(365*24*60*60);
    printf("10亿秒=%d年\n",year);
    system("pause");
    return 0;
 } 

 

 

task7

//生成一个【 60,100】区间的随机数并打印出来
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int n;
    srand((unsigned)time(NULL));
    n = rand()%41+60;
    printf ("n=%d\n",n);
    system("pause");
    return 0;
 } 

 

 

task8

#include <stdio.h>
#include <stdlib.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罗马不是一天毁灭的,我们来建设吧\n");
    system("pause");
    return 0;
}

 

 

 

posted @ 2023-03-02 22:01  chen,,  阅读(12)  评论(0编辑  收藏  举报