• 第四次实验


    1.编写程序,输入一个整数x,按照下式输出对应的y值。

          

    #include<stdio.h>
    int main()
    {    
        int x,y,z;
        printf("输入一个整数");
        scanf("%d",&x);
        z=0;
        if (x%2==0)
        {
            for (y=2;y<=x;y+=2)
            z=z+y;
            printf("结果为%d",z);
        }
        else
        {
            for (y=1;y<=x;y+=2)
            z=z+y;
            printf("结果为%d",z);
        }
        return 0;
    }

    2.编程求1-1/2+1/3-1/4+1/5- … +1/99-1/100,结果保留两位小数。

    #include<stdio.h>
    int main()
    {
        double a,b,c,d;
        a=0;
        b=0;
        for(d=1;d<=99;d=d+2)
        {
            a=a+1/d;
        }
        for(d=2;d<=100;d=d+2)
        {
            b=b-1/d;
        }
        c=a+b;
        printf("%.2lf",c);
        return 0;    
    }

    3.输出2000年至3000年所有闰年的年号,每输出10个年号换一行。最后统计一共有多少个闰年。

    #include<stdio.h>
    int main()
    {
        int x,y=0;
        for(x=2000;x<=3000;x++)
        {
            if(x%400==0||x%4==0&&x%100!=0)
            {
                printf("%d ",x);
                y++;
                if(y%10==0)
                {
                    printf("\n");
                }
            }
        }
        printf("总共有%d个闰年",y);
        return 0;
    }

     4.输入一个实数x和一个整数m,计算xm

    #include<stdio.h>
    int main()
    {
        float x,y=1.0;
        int a,b;
        printf("输入一个实数x,和一个整数b");
        scanf("%f%d",&x,&b);
        for(a=1;a<=b;a++)
        {
            y=y*x;
        }
        printf("%.2f",y);
        return 0;
    }

    5.输入一串字符,分别统计其中字母、空格、数字和其他字符的个数。

    #include<stdio.h>
    int main()
    {
        int a,b,c,d,e;
        a=0;
        b=0;
        c=0;
        d=0;
        e=0;
        printf("请输入一串字符\n");
        while(e!='\n')
        {
            scanf("%c",&e);
            if((e>='A'&&e<='Z')||(e>='a'&&a<='z'))
            {
                a=a+1;
            }
            else if(e>='0'&&e<='9')
            {
                b=b+1;
            }
             else if(e==' ')
             {
                 c=c+1;
             }
             else
             {
                 d=d+1;
             }
        }
        printf("一共有%d个字符,%d个字母,%d个空格,%d个其他\n",a,b,c,d);
        return 0;
    }

    6.输入一批数(正数和负数),输入0结束,分别计算其中正数的平均值和负数的平均值,

    #include<stdio.h>
    int main()
    {
        int i=0,n=0;
        float a,b,c,d,e;
        for(scanf("%f",&a);a;scanf("%f",&a))
        {
            if(a>0)
            {
                i=i+1;
                d=d+a;
            }
            else
            {
                n=n+1;
                e=e+a;
            }
            if(i==0)
            {
                   b=0;
            }
            else
            {
                 b=d/i;
            }
            if(n==0)
            {
                c=0;
            }
            else
            {
                  c=e/n;
            }
        }
    
        printf("正数的平均数为;%f\n",b);
        printf("附属的平均数为;%f\n",c);
        return 0;
    }

    7.输出1000以内的所有素数,每行10个,最后输出一共有多少个素数。(每列对齐)

    #include<stdio.h>
    int main()
    {
        int a,b,c,d;
        b=0;
        c=0;
        for(a=1;a<=1000;a=a+1)
        {
            b=b+1;
            for(d=2;d<=b-1;d++)
            {
                if(b%d==0)
                {
                    break;
                }
            }
            if(d==b)
            {
                printf(" %03d",d);
                c=c+1;
                if(c%10==0)
                printf("\n");
            }
        }
        printf("一共有%d个质数\n",c);
        return 0;
    }

    8.打印下列图形

                 

    #include<stdio.h>
    int main()
    {
        int a,b;
        for(a=1;a<=5;a++)
        {
            for(b=1;b<=a-1;b++)
            printf(" ");
            for(b=1;b<=11-2*a;b++)
            printf("*");
            printf("\n");
        }
        return 0;
    }

    实验总结:1

    2

    3运算符:

    增量运算符单独使用时,前缀形式和后缀形式的结果是一样的。优点:优点:自增、自减运算生成的代码效率更高。缺点:

    可读性差:例如: (n++)+(n++) 不同的编译器产生的运行结果不同。

    总结:

    1.乘积要初始化为1

    2.循环体中应有使循环趋向于结束的语句。

    3.无限循环中,通过在循环体中加break语句对循环进行控制。

  • 相关阅读:
    烟花散尽漫说无(參考资料)
    [Leetcode]-Majority Element
    百度移动搜索測试电面
    可删除超炫&amp;多种特效的Card视图(改造自cardsui-for-android开源项目),提供DEMO下载
    关闭 You need to use a Theme.AppCompat theme (or descendant) with this activity解决方法
    Android Snackbar使用方法及小技巧-design
    Android Intent Action 大全
    android权限大全
    Android 判断SD卡是否存在及容量查询
    Android中蓝牙的基本使用----BluetoothAdapter类简介
  • 原文地址:https://www.cnblogs.com/xiongx/p/6005049.html
Copyright © 2020-2023  润新知