河南第五届ACM省赛(最强DE 战斗力)(找规律+大数)

题目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=541

思路:找规律题,数据开小了,直接用Java改了一下直接A了,不过Java耗时有点长啊,注意类名要写成Main,不然会编译错误

1 1
2 2
3 3
4 4
5 3*2
6 3*3
7 3*4
8 3*3*2
9 3*3*3
10 3*3*4
11 3*3*3*2
12 3*3*3*3
13 3*3*3*4
14 3*3*3*3*2
15 3*3*3*3*3
16 3*3*3*3*4

AC代码:

import java.io.*;
import java.util.*;
import java.math.BigInteger;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		int t = in.nextInt();
		while(t-->0)
		{
			int n = in.nextInt();
			if(n == 1)
			{
				System.out.println(1);
				continue;
			}
			if(n == 2)
			{
				System.out.println(2);
				continue;
			}
			int sum = n / 3;
			int sum1 = n % 3;
			BigInteger sum2 = BigInteger.ONE;
	        if(sum1 == 2 || sum1 == 0)
	        {
	            for(int i=1; i<=sum; i++)
	            {
	                sum2 = sum2.multiply(BigInteger.valueOf(3));
	            }
	            if(sum1 == 2)
	                sum2 = sum2.multiply(BigInteger.valueOf(2));
	        }
	        else
	        {
	            for(int i=1; i<sum; i++)
	            {
	                sum2 = sum2.multiply(BigInteger.valueOf(3));
	            }
                sum2 = sum2.multiply(BigInteger.valueOf(4));
	        }
	        System.out.println(sum2);
		}
	}

}


错误代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x7f7f7f7f;//2139062143
typedef long long ll;
using namespace std;

int main()
{
    int t;
    int i,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        if(n == 1)
        {
            printf("1\n");
            continue;
        }
        if(n == 2)
        {
            printf("2\n");
            continue;
        }
        int sum = n / 3;
        int sum1 = n % 3;
        ll sum2 = 1;
        if(sum1 == 2 || sum1 == 0)
        {
            for(i=1; i<=sum; i++)
            {
                sum2 *= 3;
            }
            if(sum1 == 2)
                sum2 *= 2;
        }
        else
        {
            for(i=1; i<sum; i++)
            {
                sum2 *= 3;
            }
            sum2 *= 4;
        }
        printf("%I64d\n",sum2);
    }
    return 0;
}
很快就会超long long,必须得用大整数

你可能感兴趣的:(河南第五届ACM省赛(最强DE 战斗力)(找规律+大数))