#4760: 如何再加速


popular10347 (ICPC// 哪時能唸到高等演算法T^T)

學校 : 元智大學
編號 : 11351
來源 : [1.169.118.99]
最後登入時間 :
2012-10-29 00:22:54
d056. 10013 - Super long sums -- UVa10013 | From: [125.231.174.108] | 發表日期 : 2011-01-16 21:40

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

int main()
{
          int t;
          int number1[1000001];
          int number2[1000001];
          int* result;

         while(scanf("%d",&t) != EOF)
        {
              for(int i=0;i<t;i++)
             {
                   int digit;
                   scanf("%d",&digit);
   
                   result=new int[digit];

                   int j;
                   for(j=0;j<digit;j++)
                         scanf("%d %d",&number1[j],&number2[j]);

                  int carry=0;
                  j--;
                 while(j>=0)
                {
                     result[j]=number1[j]+number2[j]+carry;
                     if(result[j]>9)
                            carry=1;
                     else
                            carry=0;

                    result[j]%=10;

                    j--;
                }

                for(int k=0;k<digit;k++)
                           cout << result[k];
                cout << endl;
                delete []result;
 

            }
       }

 

    //system("pause");
    return 0;
}
    

以上是我的程式碼,想詢問各位高手們

這這要如何加速?? 看了有人80ms就完成了

 想要麻煩高手們指點迷津,謝謝^^

     

 
#7782: Re:如何再加速


akira0331 (小迷糊)

學校 : 不指定學校
編號 : 26613
來源 : [203.70.194.240]
最後登入時間 :
2013-07-29 09:30:29
d056. 10013 - Super long sums -- UVa10013 | From: [203.70.194.240] | 發表日期 : 2013-05-22 14:01

#include
#include
using namespace std;

int main()
{
          int t;
          int number1[1000001];
          int number2[1000001];
          int* result;

         while(scanf("%d",&t) != EOF)
        {
              for(int i=0;i             {
                   int digit;
                   scanf("%d",&digit);
   
                   result=new int[digit];

                   int j;
                   for(j=0;j                         scanf("%d %d",&number1[j],&number2[j]);

                  int carry=0;
                  j--;
                 while(j>=0)
                {
                     result[j]=number1[j]+number2[j]+carry;
                     if(result[j]>9)
                            carry=1;
                     else
                            carry=0;

                    result[j]%=10;

                    j--;
                }

                for(int k=0;k                           cout << result[k];
                cout << endl;
                delete []result;
 

            }
       }

 

    //system("pause");
    return 0;
}
    

以上是我的程式碼,想詢問各位高手們

這這要如何加速?? 看了有人80ms就完成了

 想要麻煩高手們指點迷津,謝謝^^

 

需要做輸入優化,我的程式沒有做優化是224ms,有做優化是60ms,差了3倍多

 
ZeroJudge Forum