HDOJ1001-1005题解

简介:

1001--Sum Problem(http://acm.hdu.edu.cn/showproblem.php?pid=1001

#include <stdio.h>

int sum(int n)
{
    if(n % 2) 
        return (n + 1) / 2 * n;
    else
        return (n / 2) * (n + 1);
}

int main()
{
    int n;
    while(scanf("%d",&n) != EOF){
        printf("%d\n\n",sum(n));
    }
    return 0;
}

1002--A + B Problem II(http://acm.hdu.edu.cn/showproblem.php?pid=1002

简单题:大数的运算

注意格式(Case的首字母大写、各种空格、每一行之间有空行,最后一行没有空行)!

给出几组测试数据:

6
1 2
1 0
9999 1
1 999999
5555 4445
112233445566778899 998877665544332211

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void solve()
{
    int n,i,j,k,flag,t,cas,L;
    char a[1001],b[1001],c[1002];
    scanf("%d",&n);
    getchar();
    cas=1;
    while(n--)
    {
        flag=0;
        memset(a,'\0',sizeof(a));
        memset(b,'\0',sizeof(b));
        memset(c,'\0',sizeof(c));
        scanf("%s",a);
        scanf("%s",b);
        printf("Case %d:\n",cas);
        printf("%s",a);
        printf(" + ");
        printf("%s",b);
        printf(" = ");
        strrev(a);
        strrev(b);
        k=i=0;
        L=(strlen(a)>strlen(b)?strlen(b):strlen(a));
        while(i<L)
        {
            t=(a[i]-'0')+(b[i]-'0')+flag;
            flag=(t>=10?1:0);
            c[k++]=t%10+'0';
            i++;
        }
        if(a[i]=='\0')
        {
            while(b[i]!='\0') {
                t=b[i++]-'0'+flag;
                c[k++]=t%10+'0';
                flag=(t>=10?1:0);
            }
        }
        else
        {
            while(a[i]!='\0') {
                t=a[i++]-'0'+flag;
                c[k++]=t%10+'0';
                flag=(t>=10?1:0);
            }
        }
        if(flag) c[k]='1';
        else k--;
        while(k>=0)
        {
            printf("%c",c[k]);
            k--;
        }
        printf("\n");
        if(n>0) printf("\n");
        cas++;
    }
}

int main()
{
    solve();
    return 0;
}

1003--Max Sum(http://acm.hdu.edu.cn/showproblem.php?pid=1003

#include<iostream>
#include<cstdio>
using namespace std;

void solve()
{
    int T, i, max,sum,n,num,begin,end,t,con;
    while(cin>>T) {
        for(i = 1; i <= T; i++) {
            max = -1000000;
            sum = 0;
            n = con = 0;
            cin>>n;
            num = n;
            begin = end = 0;
            while(n--) {
                cin>>t;
                sum += t;
                con++;
                if(sum > max) {
                    begin = con;
                    max = sum;
                    end = num - n;
                }
                if(sum < 0) {
                    sum = 0;
                    con = 0;
                }
            }
            cout<<"Case "<<i<<":"<<endl<<max<<" "<<end-begin+1<<" "<<end<<endl;  
            if(i != T) cout<<endl;  
        }
    }
}

int main()
{     
    solve();
    return 0;
}

1004--Let the Balloon Rise(http://acm.hdu.edu.cn/showproblem.php?pid=1004

#include<iostream>
#include<cstdio>
#include<map>
#include<string>
#include<cstring>
using namespace std;

void solve()
{
    int T, max, t;
    map<string, int> color_count;
    string color;
    while(cin>>T && T) {
        max = 0;
        while(T--) {
            cin>>color;
            ++color_count[color];
        }
        map<string, int>::const_iterator ii, it;
        it = color_count.begin();
        while(it != color_count.end()) {
            if(max < it->second) {
                max = it->second;
                ii = it;
            }
            it++;
        }
        cout<<ii->first<<endl;
        color_count.clear();
    }
}

int main()
{     
    solve();
    return 0;
}

1005--Number Sequence(http://acm.hdu.edu.cn/showproblem.php?pid=1005

#include<iostream>
using namespace std;

void solve()
{
    int A, B, n;
    while(cin>>A>>B>>n && (A || B || n)) {
        int a[2] = {1, 1};
        for(int i = 0; i < (n %49 - 1) / 2; i++) {
            a[0] = (A * a[1] + B * a[0]) % 7;
            a[1] = (A * a[0] + B * a[1]) % 7;
        }
        if(n % 2) {
            cout<<a[0]<<endl;
        } else {
            cout<<a[1]<<endl;
        }
    }
}

int main()
{
    solve();
    return 0;
}
目录
相关文章
|
6月前
HDU-2089-不要62
HDU-2089-不要62
33 0
|
人工智能 Java
2021杭电多校5-Arrary-hdu7020
Array Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 965 Accepted Submission(s): 312 Problem Description Given an integer array a[1…n].
178 0
2021杭电多校5-Arrary-hdu7020
HDU2203亲和串
博客水平见水平......目前阶段就是这么菜,我会好好努力的!毕业直接拿到阿里offer!
1229 0
|
Java 人工智能 Windows
|
定位技术
hdu 4771 Stealing Harry Potter's Precious
点击打开链接 题意:题目给定一个n*m的地图,地图有一个起点标记为'@',还有'#'表示不能够走的,'.'表示可以走。给定k个点,问从起点开始把这k个点走过去的最小步数。
793 0