#6904: 請問一下,如何加快ㄋ??謝謝


cse011417 (哈哈哈)

學校 : 國立臺中高級工業職業學校
編號 : 22273
來源 : [42.77.142.213]
最後登入時間 :
2024-03-20 11:49:15
d218. 加法與次方的奧妙 -- Project Euler | From: [114.33.41.76] | 發表日期 : 2012-08-13 23:01

#include<iostream>
#include<string.h>
#define MAX 5000
using namespace std;
void Print(int n[])//印出
{
    int i;
    for (i=MAX-1; i>0; i--)
        if(n[i]!=0) break;
    for ( ; i>=0; i--)
        cout << n[i];
    cout << endl;
}
void add(int a[MAX], int b[MAX], int c[MAX])//加法
{
    for (int i=0; i<MAX; i++)   // 對應的位數相加
        c[i] = a[i] + b[i];

    for (int i=0; i<MAX-1; i++) // 一口氣進位
    {
        c[i+1] += c[i] / 10;    // 進位
        c[i] %= 10;             // 進位後餘下的數
    }
}
void mul(int a[MAX], int b, int c[MAX])//大數乘int
{
    for (int i=0; i<MAX; i++)
        c[i] = a[i] * b;

    for (int i=0; i<MAX-1; i++) // 一口氣進位
    {
        c[i+1] += c[i] / 10;
        c[i] %= 10;
    }
}
void Zero(int n[])//歸零
{
    int i;
    for (i=0; i<MAX; i++)
        n[i]=0;
}
int main()
{
    int a[MAX],b,c,d[MAX];
    Zero(d);
    for(b=1;b<=1000;b++)
    {
        Zero(a);
        a[0]=1;
        for(c=1;c<=b;c++)
        {
            mul(a,c,a);
        }
        add(a,d,d);
    }
    Print(d);
}

 
#8011: Re:請問一下,如何加快ㄋ??謝謝


scott (scott)

學校 : 新北市立板橋高級中學
編號 : 27138
來源 : [67.22.22.217]
最後登入時間 :
2023-11-22 06:25:22
d218. 加法與次方的奧妙 -- Project Euler | From: [118.168.176.115] | 發表日期 : 2013-07-28 23:46

#include
#include
#define MAX 5000
using namespace std;
void Print(int n[])//印出
{
    int i;
    for (i=MAX-1; i>0; i--)
        if(n[i]!=0) break;
    for ( ; i>=0; i--)
        cout << n[i];
    cout << endl;
}
void add(int a[MAX], int b[MAX], int c[MAX])//加法
{
    for (int i=0; i
        c[i] = a[i] + b[i];

    for (int i=0; i
    {
        c[i+1] += c[i] / 10;    // 進位
        c[i] %= 10;             // 進位後餘下的數
    }
}
void mul(int a[MAX], int b, int c[MAX])//大數乘int
{
    for (int i=0; i
        c[i] = a[i] * b;

    for (int i=0; i
    {
        c[i+1] += c[i] / 10;
        c[i] %= 10;
    }
}
void Zero(int n[])//歸零
{
    int i;
    for (i=0; i
        n[i]=0;
}
int main()
{
    int a[MAX],b,c,d[MAX];
    Zero(d);
    for(b=1;b<=1000;b++)
    {
        Zero(a);
        a[0]=1;
        for(c=1;c<=b;c++)
        {
            mul(a,c,a);
        }
        add(a,d,d);
    }
    Print(d);
}
 
 
XD http://w3.math.sinica.edu.tw/math_media/d252/25210.pdf 
 
ZeroJudge Forum