JAVA—BigInteger||c++—高精度——Let‘s Play Another Game

题目描述

OK, if you finished previous game, let's play another game :D
Now n participants of this game sit around the table. Each minute one pair of neighbors can change their places. Find the minimum time (in minutes) required for all participants to sit in reverse order (so that left neighbors would become right, and right - left).

输入

The first line is the amount of tests. Each next line contains one integer n (1 ≤ n ≤ 1018) --- the amount of participants.

输出

For each number n of participants to game print on the standard output, on a separate line, the minimum time required for all participants to sit in reverse order.

样例输入 

3
4
5
6

样例输出 

2
4
6

 题意:给定的n个人按照顺时针的方向为1~6,现要求找到最少步数使得逆时针方向为1~6

思路:n分为两半,化一个圆为两条线的逆置,逆置后1不一定在6的位置,只需要逆时针存在1~6即可线性长度为n的逆置则是1+2+...+n-1,等差数列求和。

java不用写高精度版本:

import java.math.BigInteger;
import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while (t != 0) {
      t--;
      long n = sc.nextLong();
      long a = n / 2;
      long b = n / 2;
      if (n % 2 == 1)
        b++;
      a--;
      b--;
      long a1 = a + 1;
      long b1 = b + 1;
      if (a % 2 == 0)
        a = a / 2;
      if (b % 2 == 0)
        b = b / 2;
      if (a1 % 2 == 0)
        a1 = a1 / 2;
      if (b1 % 2 == 0)
        b1 = b1 / 2;
      BigInteger ai = new BigInteger(a + "");
      BigInteger ai1 = new BigInteger(a1 + "");
      BigInteger ans1 = ai.multiply(ai1);
      BigInteger bi = new BigInteger(b + "");
      BigInteger bi1 = new BigInteger(b1 + "");
      BigInteger ans2 = bi.multiply(bi1);
      System.out.println(ans1.add(ans2));
    }
  }
}

c++高精度版本:

#include <iostream>
#include <cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
vector<int> get1(vector<int>&a,int b)
{
    int tmp=0;
    for(int i=a.size()-1;i>=0;i--)
    {
        int p=a[i]*b/10;
        a[i]=a[i]*b%10;
        a[i]+=tmp;
        tmp=a[i]/10;
        tmp+=p;
        a[i]%=10;
    }
while(a[0]==0)
    a.erase(a.begin());
    return a;
}
vector<int> get(vector<int>&a,vector<int>&b)
{
    int tmp=0;
    int i=a.size();
    int j=b.size();
    a.insert(a.begin(),0);
    b.insert(b.begin(),0);

    if(i>=j)
    {
    while(i>=0&&j>=0)
    {
        a[i]+=b[j];
       a[i-1]+=a[i]/10;
        a[i]%=10;
        i--;
        j--;
    }
        return a;
    }else{
        while(i>=0&&j>=0)
    {
        b[j]+=a[i];
       b[j-1]+=b[j]/10;
        b[j]%=10;
        i--;
        j--;
    }
        return b;
    }
}
signed main()
{
    int t;
    scanf("%lld",&t);
    while(t--)
    {
    int n;
    scanf("%lld",&n);
    if(n==1)
    {
        puts("0");
        continue;
    }else if(n==2)
    {
        puts("1");
        continue;
    }else if(n==3)
    {
        puts("1");
        continue;
    }
    int a=n/2;
    int b=n/2;
    if(n%2)
        b++;
    a--;
    b--;
    int a1=a+1;
    int b1=b+1;
    if(a%2==0) a/=2;
    if(a1%2==0) a1/=2;
    if(b%2==0) b/=2;
    if(b1%2==0) b1/=2;
    vector<int>ax;
    vector<int>bx;
    int tmp=a;
    while(a)
    {
        ax.insert(ax.begin(),a%10);
        a/=10;
    }
    while(b)
    {
        bx.insert(bx.begin(),b%10);
        b/=10;
    }
    for(int i=0;i<20;i++)
    ax.insert(ax.begin(),0);
    for(int i=0;i<20;i++)
        bx.insert(bx.begin(),0);
    ax=get1(ax,a1);
    bx=get1(bx,b1);
    vector<int>c=get(ax,bx);
   int p;
    for(int i=0;i<c.size();i++)
        if(c[i]!=0)
    {
        p=i;
        break;
    }
    for(int i=p;i<c.size();i++)
        printf("%lld",c[i]);
    puts("");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值