HDU 6298 Maximum Multiple (C、java)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6298

题型:思维+暴力

1=12+13+16=13+13+13=12+14+14 1 = 1 2 + 1 3 + 1 6 = 1 3 + 1 3 + 1 3 = 1 2 + 1 4 + 1 4 .
其实找找规律就知道只有3的倍数或4的倍数才能分解

比赛的时候java卡了,就写了个C的,结束了再补了java的

C版本

#include<stdio.h>
#include<math.h>
#include<string.h>
int t;
unsigned long long int a;
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%lld",&a);
            if(a%3==0){
                printf("%lld\n",(a/3)*(a/3)*(a/3));
                continue;
            }else if(a%4==0){
                printf("%lld\n",(a/4)*(a/4)*(a/2));
                continue;
            }
            printf("-1\n");
    }
    return 0;
}

java版本


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Main {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    public static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }
    public static long nextLong() throws IOException {
        in.nextToken();
        return (long) in.nval;
    }
    public static String next() throws IOException {
        in.nextToken();
        return (String) in.sval;
    }
    public static void main(String[] args) throws IOException {

        int n = nextInt();
        long a;
        while (n-- > 0) {
             a= nextLong();
            if (a % 3 == 0) {
                out.println((a / 3) * (a / 3) * (a / 3));
                continue;
            } else if (a % 4 == 0) {
                out.println((a / 2) * (a / 4) * (a / 4));
                continue;
            }
            out.println(-1);
        }
        out.flush();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值