第四次作業

來源:互聯網
上載者:User

標籤:多少   結束   div   alt   作業   har   oat   空格   ges   

一、實驗內容

1、實驗要求:編寫程式,輸入一個整數x,按照下式輸出對應的y值。

    

代碼:

#include<stdio.h>int main(){    int i,x,y=0;    printf("請輸入x的值:\n");    scanf("%d",&x);    if(x<1)    {        printf("error:\n");    }    else    {        if(x%2!=0)        {            for(i=1;i<=x;i+=2)            {                y=y+i;            }        }        else        {            for(i=2;i<=x;i+=2)            {                y=y+i;            }        }    }    printf("y=%d",y);    return 0;}

程式運行結果:

2、實驗要求:編程求1-1/2+1/3-1/4+1/5- … +1/99-1/100,結果保留兩位小數。

代碼:

#include <stdio.h>int main(){    int x;    float a=1,y;    y=0;    for(x=1;x<=100;x++)    {        if(x%2==1)        {            y=y+a/x;        }        else        {            y=y-a/x;        }    }    printf("%.2f:\n",y);    return 0;}

程式運行結果:

3、實驗要求:輸出2000年至3000年所有閏年的年號,每輸出10個年號換一行。最後統計一共有多少個閏年。

代碼:

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

程式運行結果:

4、實驗要求:輸入一個實數x和一個整數m,計算xm,不允許調用pow()函數。

代碼:

#include<stdio.h>int main(){    float x,y=1;    int m,i;    printf("請輸入一個整數和一個實數:\n");    scanf("%d%f",&m,&x);    for(i=1;i<=m;i++)    {        y=y*x;    }    printf("%f",y);    return 0;}

程式運行結果:

5、實驗要求:輸入一串字元,分別統計其中字母、空格、數字和其他字元的個數。

代碼:

#include<stdio.h>int main(){    int n1,n2,n3,n4;    char c;    n1=0;    n2=0;    n3=0;    n4=0;    printf("請輸入一串字元:\n");    while((c=getchar())!=‘\n‘)    {        if(c<=‘z‘&&c>=‘a‘||c<=‘Z‘&&c>=‘A‘)        {            n1++;        }        else if(c==‘ ‘)        {            n2++;        }        else if(c>=‘0‘&&c<=‘9‘)        {            n3++;        }        else        {            n4++;        }    }    printf("字母=%d,空格=%d,數字=%d,其他=%d",n1,n2,n3,n4);    return 0;}

 程式運行結果:

6,實驗要求:輸入一批數(正數和負數),輸入0結束,分別計算其中正數的平均值和負數的平均值,

代碼:

#include <stdio.h>int main(){    int x,n1=0,n2=0;    float sum1=0,sum2=0;    scanf("%d",&x);    while(x!=0)    {        if (x>0)        {            sum1=sum1+x;            n1++;        }        else if (x<0)        {            sum2=sum2+x;            n2++;        }        scanf("%d",&x);    }     printf("正數平均值:%.2f負數平均值:%.2f",sum1/n1,sum2/n2);     return 0;}

程式運行結果:

 

7、實驗要求:輸出1000以內的所有素數,每行10個,最後輸出一共有多少個素數。(每列對齊)

代碼:

#include<stdio.h>#include<math.h>int main(){    int n=0,i,x,y,z;    for(i=2;i<=1000;i++)    {        y=1;        z=sqrt(i);        for(x=2;x<=z;x++)        {            if(i%x==0)            {                y=0;                break;            }        }        if(y==1)        {            n++;            if(n%10==0)            {                printf("\n");            }            else            {                printf("%4d",i);            }        }    }    return 0;}

程式性運行結果:

8、實驗要求:列印下列圖形

             

代碼:

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

程式運行結果:

二、本次課知識點總結

1、for(迴圈變數賦初值;迴圈條件;迴圈變數增值)

   {迴圈體語句}              ①分號不能省略;②適用於迴圈次數已知的迴圈問題;

2、while(迴圈條件)

    {迴圈體語句}             ①迴圈體有可能一次也不執行;②迴圈體內應有使運算式得值趨向假的操作,從而退出迴圈;③適用於迴圈次數已知由                                      給定條件控制

3、do

    {迴圈體語句;}

    while(迴圈條件);     ①迴圈體至少要執行一次

4、break

①在迴圈語句中一般與if使用;②break語句不能用於迴圈語句中和switch語句之外的任何其他語句中;

5、continue

與if一起使用,只能用在迴圈語句中;

三、實驗總結

1、注意分號的書寫,do...while語句中後面需要加分號;

2、開根號的表達方式為sqrt();

3、判斷是否為字元的表達方式是 ((c=getchar())!=‘\n‘);

4、迴圈體中printf不用加大括弧;

5、注意每個迴圈語句試用的條件。

 

第四次作業

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.