To Beginners: HDU 2000~2013 Solutions

For students who start learning C language programming or C++ process-oriented programming, it is good to use online OJ website for practice training to improve their programming ability.There are many OJ websites at home and abroad. It is unrealistic and unnecessary to go and see each one and brush a topic.Even with an OJ website, it's difficult to finish all the 3-4,000 questions above.Therefore, we recommend two OJ websites.One is PKU JudgeOnline from Peking University. http://poj.org/ (POJ) for short; the other is the HDU Online Judge System of Hangzhou University of Electronic Science and Technology. http://acm.hdu.edu.cn/ (HDU) for short.It is helpful to combine these two websites for training in programming.

There are more than 3,000 questions on POJ, most of them in English, which seems taller; there are nearly 6,000 questions on HDU, some in Chinese, which are more grounded than POJ.The titles on these two OJ websites are not organized by ease or by theme (HDU is slightly better, and sometimes there are small concepts of feature collections).Therefore, for beginners, it is a question which brushes to look for.The title is difficult, unable to start, impairs confidence and interest, and simple brushing will have little effect on the improvement.Sometimes I browse the Internet and often find some articles with one water topic and another. I bring them to see for myself, but that's not necessarily the case.As a beginner in programming, you are a primary school student. There are so many college students who tell you that calculus is simple, and the quadratic equation of one variable is not worth mentioning. It works the same way.Don't care too much about what other people say. Just find something suitable for you according to your own level.

For this reason, I have compiled a short story topic: To Beginners.The main purpose is to help beginners find suitable training topics on POJ and HDU.Don't joke, Bulls. It's a good way.

* Let's look at the HDU first.85% of the 100 questions Pro.ID from 2000 to 2099 in HDU Title Set Volume 11 are Chinese. There is no language barrier to the understanding of the title.And these 100 questions themselves are classified as some minor topics, such as 2000-2032, which is classified as "C Language Programming Exercise (1)~"C Language Programming Exercise (5)". It is just right for beginners to train as exercises.Below I will give the AC program of these 100 questions in the form of a question for your reference.

HDU 2000: ASCII Code Sorting

Simple branch selection structure.Enter the three letters a,b,c to make a smaller than B by comparing the exchange of a and b; compare the exchange of a and C to make a smaller than c; so a is the smallest letter; and finally compare the exchange of B and C to make B smaller than 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; 
}

HDU 2001: Calculate the distance between two points

Simple formula calculation.Two-point coordinates (x1,y1) and (x2,y2) are known, and the distance formula for two points is sqrt ((x2-x1)* (x2-x1)+ (y2-y1)* (y2-y1).

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

HDU 2002:2002 Calculating Spherical Volume

 

Simple formula calculation questions.Given the radius r, the formula for calculating the volume of a sphere is V=4/3*PI*r3.

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

HDU 2003: Evaluating Absolute Values

Simple selection structure 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; 
}

HDU 2004: Achievement Conversion

Multiple branch nested 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; 
}

HDU 2005: What day?

switch...case...Flexible use of the structure.

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

Or application of arrays.

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

HDU 2006: Product of odd numbers

Simple loop structure.

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

HDU 2007: Sum of Squares and Sum of Cubes

Simple loop structure.

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

HDU 2008: Numeric Statistics

The number of numbers greater than 0, equal to 0, and less than 0 is counted by the selection structure in the loop.

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

HDU 2009: Sum of Count Columns

Simple loop structure.

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

HDU 2010: Number of daffodils

Simple loop structure.Set a, b, and C to represent the hundred, ten, and individual of the three-digit x, respectively,

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

HDU 2011: Polynomial Sum

Simple loop structure.Switch between even subtraction and odd addition.Define the variable flag, with an initial value of 1, then flag=-flag after each loop, which is 1, -1,1, -1, -1,...To switch between addition and subtraction.

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

HDU 2012: prime number determination

Abstract the decision of a prime number as a function.The prototype is: 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; 
}

HDU 2013: Ta Tao Ji

Simple loop iteration.The iteration is num=2*(num+1); the initial value of the iteration is 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    

Keywords: C++ Programming C ascii less

Added by Design on Sun, 15 Sep 2019 04:43:31 +0300