PAT 數據結構 2-06 數列求和

2-06. 數列求和
時間限制
50 ms
內存限制
32000 kB
代碼長度限制
8000 B
判題程序
Standard

http://pat.zju.edu.cn/contests/ds/2-06

給定某數字A(1<=A<=9)以及非負整數N(0<=N<=100000),求數列之和S = A + AA + AAA + … + AA…A(N個A)。例如A=1, N=3時,S = 1 + 11 + 111 = 123。

輸入格式說明:

輸入數字A與非負整數N。

輸出格式說明:

輸出其N項數列之和S的值。

樣例輸入與輸出:

序號輸入輸出
1
1 3
123
2
6 100
7407407407407407407407407407407407407407407407407407407407407407407407407407407407407407407407407340
3
1 0
0

這道題目一直超時,剛開始用的是java寫的,剛開始以爲是大整數問題,所以就直接利用java.Math裏面的BigInteger類來完成,結果超時了

初始代碼:

import java.util.*;
import java.math.BigInteger;
class Main
{
  public static void main(String[] args)
  {
    int a,n;
    Scanner sc = new Scanner(System.in);
    char aaa = '0';
    System.out.println((int)aaa);
    while(sc.hasNextInt())
    {
      a = sc.nextInt();
      n = sc.nextInt();
      long t = System.currentTimeMillis();
      BigInteger bi = new BigInteger(String.format("%d",a));
      BigInteger temp = new BigInteger(String.format("%d",a));
      BigInteger biSum = BigInteger.ZERO;
      for(int i = 0;i<n;i++)
      {
         biSum = biSum.add(temp);
         temp = temp.multiply(BigInteger.TEN);
         temp =temp.add(bi);
      }
      System.out.println(biSum.toString());
      System.out.println(System.currentTimeMillis()-t);
    }
  }
}

以6 100爲例運行時間是17ms

6 100
74074074074074074074074074074074074074074074074074074074074074074074074074074074
07407407407407407340
17

第二種是:

import java.util.*;
import java.math.BigInteger;
class Main2
{
  public static void main(String[] args)
  {
    int a,n;
    Scanner sc = new Scanner(System.in);
    //char aaa = '0';
   // System.out.println((int)aaa);
    while(sc.hasNextInt())
    {
      a = sc.nextInt();
      n = sc.nextInt();
      long t = System.currentTimeMillis();
      BigInteger bi = new BigInteger(String.format("%d",a));
      BigInteger temp = new BigInteger(String.format("%d",a));
      BigInteger biSum = BigInteger.ZERO;
      for(int i = 0;i<n;i++)
      {
         biSum = biSum.add(temp);
         temp = temp.multiply(BigInteger.TEN);
         temp =temp.add(bi);
      }
      System.out.println(biSum.toString());
      System.out.println(System.currentTimeMillis()-t);
    }
  }
}

第三種方法:採用技巧,把所有數進行累加,好像把所有數手算的時候把所有的數進行相加!

import java.util.Scanner;

class Main3
{
	public static void main(String[] args)
	{
		 Scanner sc = new Scanner(System.in);
		 int[] sum = new int[100000];
		 int mod = 0;
		 int i;
	
		 int a = sc.nextInt();
		 int n = sc.nextInt();
		 long l = System.currentTimeMillis();
		 if(n==0)
		 {
		 	System.out.println(0);
		 	return;
		 }
		 	for(i =0;i<n||(i>=n&&mod!=0);i++)
		 	{
		 	  sum[i] = (a*(n-i)+mod)%10;
		 	  mod = (a*(n-i)+mod)/10;
		 	}
		  for(int j =i-1;j>=0;j--)
		  {
		  	System.out.print(sum[j]);
		  }
		  System.out.printf("\n");
		  System.out.println(System.currentTimeMillis()-l);
		 }

}

第三種方法應該是最優的了,可是還是超時,怒用C++

#include<stdio.h>

int main()
{
    int sum[100010];
    int a,n;
    scanf("%d%d",&a,&n);
    int i,mod;
    if(n==0)
    {
            printf("0\n");
            return 0;
    }
   	for(i =0;i<n||(i>=n&&mod!=0);i++)
    {
		  sum[i] = (a*(n-i)+mod)%10;
		  mod = (a*(n-i)+mod)/10;
    }
	 for(int j =i-1;j>=0;j--)
	 {
		printf("%d",sum[j]);
	 }
     printf("\n");
    return 0;
}

好的,通過了,看來PAT沒有給java雙倍時間啊,無力吐槽

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章