致初學者(一): HDU 2000~ 2013題解

      對於開始學習C語言程序設計或C++程序設計面向過程部分的同窗來講,利用在線OJ網站進行實踐訓練,對提升本身的編程能力頗有好處。國內外OJ網站不少,每一個都去看看,去刷個題,是不現實的,也不必。即便一個OJ網站,上面3~4千道題也難所有刷完。所以,給你們推薦兩個OJ網站。一個是北京大學的PKU JudgeOnline(http://poj.org/ ),簡稱POJ;另外一個是杭州電子科技大學的HDU Online Judge System (http://acm.hdu.edu.cn/),簡稱 HDU。將這兩個網站結合起來用於程序設計的訓練是有幫助的。編程

       POJ上面有3千多道題,絕大部分是英文題目,顯得高大上些;HDU上有近6千道題,有部分中文題目,比POJ接地氣些。這兩個OJ網站上的題目編排順序不是按難易程度,也不是按專題(HDU稍微好些,有時還有些專題集錦的小概念)。所以,對初學者而言,究竟找哪些題刷是個問題。題目難了,無從下手,傷信心和興趣,老刷簡單的,對提升做用就不大了。有時逛網絡,又常常發現一些文章這水題一枚,那水題又一枚,本身拿來一看,又不必定是那回事。做爲一個程序設計初學者,你就是一小學生,有那麼一嘚瑟的大學生跟你講微積分簡單,一元二次方程不值得提,道理同樣。別過多在乎別人的說法,本身根據本身的水平,找到適合本身作的事就行了。數組

      有鑑於此,我整理了一個隨筆專題:致初學者。主要想幫助初學者在POJ和HDU上找到合適的題目訓練。大牛們別笑話,路過好了。網絡

      咱們先看HDU。HDU 題目集第11卷中Pro.ID號爲2000~2099這100道題中85%是中文題目,題意理解起來不存在語言障礙。而且這100道題自己被歸結爲一些小專題,好比2000~2032這33道題就被歸結爲「C語言程序設計練習(一) 」~「C語言程序設計練習(五) 」,對於初學者做爲習題進行訓練恰到好處。下面我以題解的形式先給出這100道題的AC程序,供你們參考。ide

HDU 2000:  ASCII碼排序函數

      簡單的分支選擇結構。輸入a,b,c三個字母,比較交換a和b,使a比b小;比較交換a和c,使a比c小;這樣a就是最小的字母了;最後比較交換b和c,使b比c小。學習

#include <stdio.h>
int main()
{
    char c1,c2,c3,temp;
    while (scanf("%c%c%c",&c1,&c2,&c3)!=EOF)
    {
        if (c1>c2)
        {
            temp=c1; c1=c2; c2=temp;
        }
        if (c1>c3)
        {
            temp=c1; c1=c3; c3=temp;
        }
        if (c2>c3)
        {
            temp=c2; c2=c3; c3=temp;
        }
        printf("%c %c %c\n",c1,c2,c3);
        getchar();
    }
    return 0; 
}
View Code

HDU 2001:計算兩點間的距離網站

      簡單公式計算。已知兩點座標(x1,y1)和(x2,y2),兩點距離公式爲sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))。spa

#include <stdio.h>
#include <math.h>
int main()
{
    double x1,x2,y1,y2,dis;
    while (scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2)!=EOF)
    {
        dis=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
        printf("%.2lf\n",dis);
    }
    return 0; 
}
View Code

HDU 2002:2002 計算球體積 設計

       簡單公式計算題。已知半徑r,球的體積計算公式爲:V=4/3*PI*r33d

#include <stdio.h>
#define PI 3.1415927
int main()
{
    double r,v;
    while (scanf("%lf",&r)!=EOF)
    {
        v=4.0/3*PI*r*r*r;
        printf("%.3lf\n",v);
    }
    return 0; 
}
View Code

HDU 2003: 求絕對值 

      簡單選擇結構 if (x<0)  x=-x;

#include <stdio.h>
int main()
{
    double x;
    while (scanf("%lf",&x)!=EOF)
    {
        if (x<0)
            x=-x;
        printf("%.2lf\n",x);
    }
    return 0; 
}
View Code

HDU 2004:成績轉換 

     多分支嵌套 if …else if ……

#include <stdio.h>
int main()
{
    int score;
    while (scanf("%d",&score)!=EOF)
    {
        if (score<0 || score>100)
            printf("Score is error!\n");
        else if (score<60)
            printf("E\n");
        else if (score<70)
            printf("D\n");
        else if (score<80)
            printf("C\n");
        else if (score<90)
            printf("B\n");
        else 
            printf("A\n");
    }
    return 0; 
}
View Code

HDU 2005:第幾天?

    switch …case …結構的靈活使用。

#include <stdio.h>
int main()
{
    int year,month,day,d;
    while (scanf("%d/%d/%d",&year,&month,&day)!=EOF)
    {
        d=0;
        switch(month-1)
        {
            case 11:d+=30;
            case 10:d+=31;
            case 9:d+=30;
            case 8:d+=31;
            case 7:d+=31;
            case 6:d+=30;
            case 5:d+=31;
            case 4:d+=30;
            case 3:d+=31;
            case 2:d+=28;
                 if (year%4==0 && year%100!=0 || year%400==0) d++;
            case 1:d+=31;
        }
        d=d+day;
        printf("%d\n",d);
      }
      return 0; 
}
View Code

   或數組的應用。

#include <stdio.h>
int main()
{
    int table[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int year,month,day,d,i;
    while (scanf("%d/%d/%d",&year,&month,&day)!=EOF)
    {
        d=0;
        for (i=1;i<=month-1;i++)
            d+=table[i];
        if (month>2 && (year%4==0 && year%100!=0 || year%400==0)) 
            d++;
        d=d+day;
        printf("%d\n",d);
      }
      return 0; 
}
View Code

HDU 2006:求奇數的乘積 

      簡單循環結構。

#include <stdio.h>
int main()
{
    int n,x,p,i;
    while (scanf("%d",&n)!=EOF)
    {
         p=1;
         for (i=1;i<=n;i++)
         {
             scanf("%d",&x);
             if (x%2) 
                 p*=x;
         }
         printf("%d\n",p);
    }
    return 0; 
}
View Code

HDU 2007:平方和與立方和 

      簡單循環結構。

#include <stdio.h>
int main()
{
    int x,y,i,t,sum1,sum2;
    while (scanf("%d%d",&x,&y)!=EOF)
    {
       if (x>y)
       {
           t=x; x=y; y=t;
       }
       sum1=sum2=0;
       for (i=x;i<=y;i++)
       {
             if (i%2) 
                 sum2+=i*i*i;
             else
                 sum1+=i*i;
       }
       printf("%d %d\n",sum1,sum2);
    }
    return 0; 
}
View Code

HDU 2008:數值統計 

      循環體中用選擇結構分別統計大於0、等於0和小於0的數的個數。

#include <stdio.h>
int main()
{
    int n,i,a,b,c;
    double x;
    while (scanf("%d",&n) && n!=0)
    {
       a=b=c=0;
       for (i=1;i<=n;i++)
       {
           scanf("%lf",&x);
           if (x>0)
               c++;
           else if (x<0)
              a++;
           else
              b++;
       }
       printf("%d %d %d\n",a,b,c);
    }
    return 0; 
}
View Code

HDU 2009:求數列的和 

      簡單循環結構。

#include <stdio.h>
#include <math.h>
int main()
{
    int n,m,i;
    double x,sum;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
       x=1.0*n;
       sum=0.0;
       for (i=1;i<=m;i++)
       {
           sum+=x;
           x=sqrt(x);
       }
       printf("%.2lf\n",sum);
    }
    return 0; 
}
View Code

HDU 2010:水仙花數

     簡單循環結構。設a,b,c分別表示3位數x的百位、十位和個位,則

     a=x/100 ;     b=x%100/10;     c=x%10;

#include <stdio.h>
#include <math.h>
int main()
{
    int n,m,i,a,b,c,cnt;
    while (scanf("%d%d",&m,&n)!=EOF)
    {
       cnt=0;
       for (i=m;i<=n;i++)
       {
           a=i/100;
           b=i%100/10;
           c=i%10;
           if (i==a*a*a+b*b*b+c*c*c)
           {
               cnt++;
               if (cnt!=1)
                   printf(" ");
               printf("%d",i);
           }
       }
       if (cnt==0)
           printf("no\n");
       else
           printf("\n");
    }
    return 0; 
}
View Code

HDU 2011:多項式求和 

      簡單循環結構。爲進行奇數項相加,偶數項相減的切換。定義變量flag,初始值爲1,以後每循環一次後flag=-flag,即 1,-1,1,-1,1,-1,…,從而實現加減的切換。

#include <stdio.h>
#include <math.h>
int main()
{
    int n,m,i,flag;
    double sum;
    scanf("%d",&m);
    while (m--)
    {
       sum=0.0;
       flag=1;
          scanf("%d",&n);
       for (i=1;i<=n;i++)
       {
           sum+=flag*1.0/i;
           flag=-flag;
       }
       printf("%.2lf\n",sum);
    }
    return 0; 
}
View Code

HDU 2012:素數斷定

      將素數的斷定抽象爲一個函數。原型爲: bool isPrime(int x);

#include <stdio.h>
#include <math.h>
bool isPrime(int x)
{
    int i,n;
    n=(int)sqrt(1.0*x);
    for (i=2;i<=n;i++)
        if (x%i==0) return false;
    return true;
}
int main()
{
    int x,y,i,cnt;
    while (1)
    {
       scanf("%d%d",&x,&y);
       if (x==0 && y==0) break;
       cnt=0;
       for (i=x;i<=y;i++)
       {
           if (isPrime(i*i+i+41))
               cnt++;
       }
       if (cnt==y-x+1)
           printf("OK\n");
       else
           printf("Sorry\n");
    }
    return 0; 
}
View Code

HDU 2013:蟠桃記

      簡單循環迭代計算。迭代式爲  num=2*(num+1);  迭代初值num=1。

#include <stdio.h>
int main()
{
    int n,day,num;
    while (scanf("%d",&n)!=EOF)
    {
        num=1;
       for (day=n-1;day>=1;day--)
       {
           num=2*(num+1);
       }
       printf("%d\n",num);
    }
    return 0; 
}
View Code
相關文章
相關標籤/搜索