• 第四次作业


    1.编写程序,输入一个整数x,按照下式输出对应的y值。     
     
     
    #include<stdio.h>
    int main()
    {
        int x,y,i;
        y=0;
        printf("请输入一个整数\n");
        scanf("%d",&x);
        if(x%2==0)
        {
            for(i=2;i<=x;i+=2)
            {
                y=y+i;
            }
        }
        else
        {
            for(i=1;i<=x;i+=2)
            {
                y=y+i;
            }
        }
        printf("%d",y);
        return 0;
    }

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

    #include<stdio.h>
    int main()
    {
        int i;
        float y1,y2,y;
        for(i=2,y1=0;i<=100;i+=2)
            y1=y1+1.0/i;
        printf("%.2f\n",y1);
        for(i=1,y2=0;i<=99;i+=2)
            y2=y2+1.0/i;
        printf("%.2f\n",y2);
        y=y2-y1;
        printf("%.2f",y);
        return 0;
    
    }

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

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

    4.输入一个实数x和一个整数m,计算xm,不允许调用pow()函数。 

    #include<stdio.h>
    int main()
    {
    float x,y;
    int m,i;
    y=1;
    printf("输入一个实数\n");
    scanf("%f",&x);
    printf("输入一个整数\n");
    scanf("%d",&m);
    if(m>=0)
    {
    for(i=1;i<=m;i++)
    y=y*x;
    }
    else
    {
    for(i=0;i>m;i-=1)
    y=y*(1.0/x);
    }
    
    printf("%.2f",y);
    return 0;
    }

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

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

      8.打印下列图形     
    #include<stdio.h>
    int main()
    {
    
        int i,j;
        for(i=0,j=1;i<=5;i++)
        {
             for(j=1;j<=i+1;j++)
            {
                printf(" ");
            }
             for(j=1;j<=9-i*2;j++)
            {
                printf("*");
            }
           
            
            printf("\n");
        }
        return 0;
    }

     7.

    #include <stdio.h>
    int main()
    {
        int a,b1=1,b2=1,c1,c2;
        float d1,d2;
        printf("请输入一批数\n");
        scanf("%d",&a);
        c1=0;
        c2=0;
        while(a!=0)
        {
            if(a>0)
            {
                c1+=a;
                b1++;
                d1=(float)c1/(b1-1);
                
            }
            else
            {
                c2+=a;
                b2++;
                d2=(float)c2/(b2-1);
            }
            scanf("%d",&a);
        }
            if(b1-1==0)
            {
                printf("没有正数\n");
            }
            else
            {
                
                printf("正数平均值为%.2f\n",d1);
            }
        
            if(b2-1==0)
            {
                printf("没有负数\n");
            }
            else
            {
                
                printf("负数平均值为%.2f\n",d2);
            }
        
        return 0;
    }

    7.

    #include<stdio.h>
    #include<math.h>
    int main()
    {
        int i, j ,k=0,flag;
        for(j=2;j<=1000;j++)
        {
            flag=1;
            for(i=2;i<j-1;i++)
            {
                if(j%i==0)
                {
                    flag=0;
                    break;
                }
            }
            if(flag!=0)
            {
                printf("%-3d ",j);
                k++;
                if(k%10==0&&k!=0)
            {
                printf("\n");
            }
            }
        }
        printf("质数一共%d个",k);
        return 0;
    }

    知识总结:

    while(表达式2)    特点:先执行表达式,后执行循环体

    {

     语句1

    语句2

    表达式3;

    }

    do                     特点:限制性循环体,后判断表达式

    {

    语句1

    语句2

    表达式3;

    }while(表达式2);

    fabs()是绝对值,得用#include<math.h>

    循环的时候要声明变量

    实验总结

    一定要声明变量,要明确是整形变量还是符点型变量

  • 相关阅读:
    [LeetCode] 456. 132 Pattern
    [LeetCode] 606. Construct String from Binary Tree
    [LeetCode] 536. Construct Binary Tree from String
    [LeetCode] 925. Long Pressed Name
    [LeetCode] 652. Find Duplicate Subtrees
    [LeetCode] 743. Network Delay Time
    [LeetCode] 1209. Remove All Adjacent Duplicates in String II
    [LeetCode] 1047. Remove All Adjacent Duplicates In String
    [LeetCode] 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
    [LeetCode] 859. Buddy Strings
  • 原文地址:https://www.cnblogs.com/xuan0826/p/6011851.html
Copyright © 2020-2023  润新知