首页 > ACM题库 > HDU-杭电 > hdu 1977 Consecutive sum II-高精度-[解题报告]C++
2013
12-26

hdu 1977 Consecutive sum II-高精度-[解题报告]C++

Consecutive sum II

问题描述 :

Consecutive sum come again. Are you ready? Go ~~
1    = 0 + 1
2+3+4    = 1 + 8
5+6+7+8+9  = 8 + 27

You can see the consecutive sum can be representing like that. The nth line will have 2*n+1 consecutive numbers on the left, the first number on the right equal with the second number in last line, and the sum of left numbers equal with two number’s sum on the right.
Your task is that tell me the right numbers in the nth line.

输入:

The first integer is T, and T lines will follow.
Each line will contain an integer N (0 <= N <= 2100000).

输出:

The first integer is T, and T lines will follow.
Each line will contain an integer N (0 <= N <= 2100000).

样例输入:

3
0
1
2

样例输出:

0 1
1 8
8 27

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void mult(char a[],char b[],char s[])
{
    int i,j,k = 0,alen,blen,sum = 0,res[65][65]= {0},flag = 0;
    char result[65];
    alen = strlen(a);
    blen = strlen(b);
    for(i = 0; i<alen; i++)
    {
        for(j = 0; j<blen; j++)
            res[i][j] = (a[i]-'0')*(b[j]-'0');
    }
    for(i = alen-1; i>=0; i--)
    {
        for(j = blen-1; j>=0; j--)
        {
            sum = sum+res[i+blen-j-1][j];
        }
        result[k] = sum%10;
        k++;
        sum = sum/10;
    }
    for(i = blen-2; i>=0; i--)
    {
        for(j = 0; j<=i; j++)
        {
            sum = sum+res[i-j][j];
        }
        result[k] = sum%10;
        k++;
        sum = sum/10;
    }
    if(sum)
    {
        result[k] = sum;
        k++;
    }
    for(i = 0; i<k; i++)
        result[i]+='0';
    for(i = k-1; i>=0; i--)
        s[i] = result[k-1-i];
    s[k] = '\0';
    while(1)
    {
        if(strlen(s)!=strlen(a) && s[0] == '0')
            strcpy(s,s+1);
        else
            break;
    }
}

void add(char a[],char b[],char back[])
{
    int i,j,k,up,x,y,z,l;
    char *c;
    if(strlen(a) > strlen(b))
        l = strlen(a)+2;
    else
        l = strlen(b)+2;
    c = (char*)malloc(l*sizeof(char));
    i = strlen(a)-1;
    j = strlen(b)-1;
    k = 0;
    up = 0;
    while(j>=0 || i>=0)
    {
        if(i<0) x = '0';
        else
            x = a[i];
        if(j<0) y = '0';
        else
            y = b[j];
        z = x-'0'+y-'0';
        if(up)
            z++;
        if(z>9)
        {
            up = 1;
            z%=10;
        }
        else
            up = 0;
        c[k++] = z+'0';
        i--;
        j--;
    }
    if(up)
        c[k++] = '1';
    i = 0;
    c[k] = '\0';
    for(k-=1; k>=0; k--)
        back[i++] = c[k];
    back[i] = '\0';
}

int main()
{
    int k;
    char a[100],b[100],t[100];
    scanf("%d%*c",&k);
    while(k--)
    {
        scanf("%s",a);
        add(a,"1",b);
        strcpy(t,a);
        mult(t,t,a);
        mult(a,t,a);
        strcpy(t,b);
        mult(t,t,b);
        mult(b,t,b);
        printf("%s %s\n",a,b);
        memset(a,'\0',sizeof(a));
        memset(b,'\0',sizeof(b));
    }
    return 0;
}
#include <stdio.h>

int main()
{
    int k;
    scanf("%d",&k);
    while(k--)
    {
        __int64 n,m;
        scanf("%I64d",&n);
        m = n+1;
        n = n*n*n;
        m = m*m*m;
        printf("%I64d %I64d\n",n,m);
    }

    return 0;
}

解题转自:http://blog.csdn.net/libin56842/article/details/8815720


  1. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }

  2. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }