zoukankan      html  css  js  c++  java
  • 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
    
    ",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,'',sizeof(a));
            memset(b,'',sizeof(b));
            memset(c,'',sizeof(c));
            scanf("%s",a);
            scanf("%s",b);
            printf("Case %d:
    ",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]=='')
            {
                while(b[i]!='') {
                    t=b[i++]-'0'+flag;
                    c[k++]=t%10+'0';
                    flag=(t>=10?1:0);
                }
            }
            else
            {
                while(a[i]!='') {
                    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("
    ");
            if(n>0) printf("
    ");
            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
  • 相关阅读:
    CSS优化,提高性能的方法有哪些?
    稀疏数组(SparseArray)(Go)
    Go
    Vue 实战-6 rest 重置表单不生效原因
    Go
    Vue 实战-5 批量导出 excel功能
    Vue 实战-4 表格展开行
    Vue 实战-3 vue 中使用watch 监听 el-input属性值
    Vue 实战-2 输入框加搜索图标
    Vue 实战-1 去掉 input [number] 默认增减箭头样式
  • 原文地址:https://www.cnblogs.com/wuyudong/p/HDOJ1001-1005.html
Copyright © 2011-2022 走看看