To Beginners: HDU 2033~2043 Solutions

Continue to give the AC program of HDU 2033~2043 below for your reference.The 10 questions 2033-2043 are classified as "ACM Programming Final Examination (2006/06/07)" and "2005 Experimental Class Short Term Examination".

HDU 2033: Everyone loves A+B

Simple branch structure.

#include <stdio.h>
int main()
{
    int n,ah,am,as,bh,bm,bs;
    scanf("%d",&n);
    while (n--)
    {
        scanf("%d%d%d%d%d%d",&ah,&am,&as,&bh,&bm,&bs);
        bs+=as;
        if (bs>=60)
        {
            bm+=bs/60;
            bs=bs%60;
        }
        bm+=am;
        if (bm>=60)
        {
            bh+=bm/60;
            bm=bm%60;
        }
        bh+=ah;
        printf("%d %d %d\n",bh,bm,bs);
    }
    return 0; 
}

HDU 2034: Everyone loves A-B

Set the arrays a, b, C to represent collections A, B, C, where C=A-B.
Each element a[i] i n set A is treated with a loop for (i=0; i<n; i++), and for element a[i], a loop for (j=0; j<m; j++) is used to find whether a[i] exists i n set B, and if a[i] does not appear i n set B, a[i] is saved i n array C as a n element i n set C.
After getting the array c, sort the array C from smallest to largest using bubble sort before output.(

#include <stdio.h>
int main()
{
    int n,m,i,j,cnt,t;
    int a[101],b[101],c[101];
    while (1)
    {
        scanf("%d%d",&n,&m);
        if (n==0 && m==0)
            break;
        for (i=0;i<n;i++)        // Input Set A Elements of
            scanf("%d",&a[i]);
        for (i=0;i<m;i++)        // Input Set B Elements of
            scanf("%d",&b[i]);
        cnt=0;                  // aggregate C=A-B Number of elements of
        for (i=0;i<n;i++)
        {
            for (j=0;j<m;j++)  // Find Current Collection A Elements of a[i]Is it in a collection B Exists
                if (a[i]==b[j]) break;
            if (j==m)          // If a[i]Not in collection B Appears in
                c[cnt++]=a[i];
        }
        if (cnt==0)
           printf("NULL\n");
        else
        {
            for (i=0;i<cnt-1;i++) //Sort by bubble
                for (j=0;j<cnt-1-i;j++)
                    if (c[j]>c[j+1])
                    {
                        t=c[j];
                        c[j]=c[j+1];
                        c[j+1]=t;
                    }
            for (i=0;i<cnt;i++)
            {
                printf("%d ",c[i]);
            }
            printf("\n");
        }
    }
    return 0; 
}

HDU 2035: Everyone loves A^B

A^B is calculated using a fast power operation.

#include <stdio.h>
int mod_pow(int x,int n,int m)  // Fast Power Operation
{
    int ret=1;
    while (n!=0)
    {
        if (n&1) ret=ret*x%m;
        x=x*x%m;
        n/=2;
    }
    return ret;
}
int main()
{
    int a,b;
    while (1)
    {
        scanf("%d%d",&a,&b);
        if (a==0 && b==0) break;
        printf("%d\n",mod_pow(a,b,1000));
    }
    return 0; 
}

HDU 2036: The Spring of Reform

 

HDU 2037: No AC this summer vacation

Define a structure
      struct showtime
      {
           int begin;
           int end;
      };
Used to save the start and end times of TV programs.Define the structure array show[101] to save the input TV program.
Solve with greed.Arrange TV programs (i.e. structured array show) from small to large by end time (and from large to small by start time if end time system).
Set lastend=show[0].end first, because the end time of the first element must be the earliest, and then iterate through the elements of the array from left to right. If the start time of the current element is greater than lastend, you can see a complete program, count, and modify the lastend to equal the end time of the current element.Until all elements of the array have been traversed.

#include <stdio.h>
#include <algorithm>
using namespace std;
struct showtime
{
    int begin;
    int end;
}; 
bool cmp(showtime a ,showtime b)
{
    if(a.end != b.end)    
        return a.end < b.end;
    else
        return a.begin > b.begin;
}
int main()
{
    showtime show[101];
    int n,i,cnt,lastend;
    while (scanf("%d",&n) && n!=0)
    {
        for (i = 0;i<n;i++)
        {
            scanf("%d%d",&show[i].begin,&show[i].end);
        }
        sort(show,show+n,cmp);     
        cnt = 1;
        lastend = show[0].end;
        for (i = 0;i < n ;i++)
        {
            if(lastend <= show[i].begin)
            {
                cnt++;
                lastend = show[i].end;
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

HDU 2039: Triangle

Simple branch structure.When three edges of a triangle are input, a triangle can be formed if both sides of the triangle are satisfied and larger than the third edge.

#include <stdio.h>
int main()
{
    int m;
    double a,b,c;
    scanf("%d",&m);
    while (m--)
    {
        scanf("%lf%lf%lf",&a,&b,&c);
        if (a+b>c && a+c>b && b+c>a)
           printf("YES\n");
        else
           printf("NO\n");
    }
    return 0;
}

HDU 2040: Affinity number

Abstract a function, int calcSum(int n), that calculates the sum of all true factors of integer n.

#include <stdio.h>
#include <math.h>
int calcSum(int n)
{
    int sum=1,i,t;
    t=(int)sqrt(1.0*n);
    for (i=2;i<=t;i++)
        if (n%i==0)
            sum+=i+n/i;
    return sum;
}
int main()
{
    int m,a,b;
    scanf("%d",&m);
    while (m--)
    {
        scanf("%d%d",&a,&b);
        if (calcSum(a)==b && calcSum(b)==a)
           printf("YES\n");
        else
           printf("NO\n");
    }
    return 0;
}

HDU 2041: Super stairs

Set f[i] to indicate the number of ways to go up to the staircase I I.Apparently going up to Level I, you can get there by going up Level 1 on Stair i-1 or by going up Level 2 on Stair i-2.
f[i]=f[i-1]+f[i-2].

#include <stdio.h>
int main()
{
    int n,m,i;
    int f[41]={0,1,1};
    for (i=3;i<=40;i++)
        f[i]=f[i-1]+f[i-2];
    scanf("%d",&n);
    while (n--)
    {
        scanf("%d",&m);
        printf("%d\n",f[m]);
    }
    return 0;
}

HDU 2042: Not Easy Series Two

Simple iterative processing.The initial value of the iteration, num=3, is num=2*(num-1);.

#include <stdio.h>
int main()
{
    int n,a,i,num;
    scanf("%d",&n);
    while (n--)
    {
        scanf("%d",&a);
        num=3;
        for (i=1;i<=a;i++)
            num=2*(num-1);
        printf("%d\n",num);
    }
    return 0;
}

HDU 2043: Password

Simple string processing.

#include <stdio.h>
#include <string.h>
int main()
{
    int m,i,f1,f2,f3,f4;
    char str[51];
    scanf("%d",&m);
    while (m--)
    {
        scanf("%s",str);
        if (strlen(str)<8 || strlen(str)>16)
           printf("NO\n");
        else
        {
            f1=f2=f3=f4=0;
            for (i=0;str[i]!='\0';i++)
                if (str[i]>='0' && str[i]<='9')
                    f3=1;
                else if (str[i]>='A' && str[i]<='Z')
                    f1=1;
                else if (str[i]>='a' && str[i]<='z')
                    f2=1;
                else
                    f4=1;
            if (f1+f2+f3+f4>=3)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}

Keywords: C++ Programming Spring

Added by fodder on Mon, 16 Sep 2019 02:41:35 +0300