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

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

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

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

 

posted @ 2014-04-05 19:50  wuyudong  阅读(440)  评论(0编辑  收藏  举报
Top_arrow