【备战秋招】每日一题:2022.11.11-携程机试-哈哈数

为了更好的阅读体检,可以查看我的算法学习网
在线评测链接:P1012

题目大意

“哈哈数”定义为: 233 ∗ 1 0 i 233 * 10^i 23310i. 例如: 233 , 2330 , 23300 233,2330,23300 233,2330,23300等.

现在塔子哥想玩一玩这种数。他想知道,你是否可以使用最少的 "233数"组成 x x x ,无法做到时输出-1,总共t组输入( 0 < = x < = 1 e 14 , 1 < = t < = 100 0<=x<=1e14,1<=t<=100 0<=x<=1e141<=t<=100)

样例

输入

4
15347
233
4660
6991

输出

-1
1
2
-1

题目思路

将大的哈哈数提出因子233剩余的每个数位就是最小的哈哈数组成的组成情况

代码

Python代码

t = int(input())
for i in range(t):
    x = int(input())
    if (x % 233 != 0):
        print(-1)
    else:
        x = x//233
        print(sum(int(j) for j in str(x)))

C++代码

#include <iostream>
using namespace std;

int main(){
    int t;
    cin>>t;
    while(t--) {
        long long x;
        cin>>x;
        if (x%233!=0)
            cout<<-1<<endl;
        else
        {
            x/=233;
            int ans=0;
            while(x)
            {
                ans+=x%10;
                x/=10;
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}

Java代码

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for (int i = 0; i < t; i++) {
            Long x = scanner.nextLong();
            int ans = 0;
            if (x % 233 != 0)
                System.out.println(-1);
            else {
                x /= 233;
                while (x != 0) {
                    ans += x % 10;
                    x /= 10;
                }
                System.out.println(ans);
            }
        }
    }
}

Js代码

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let input = '';

process.stdin.on('data', (data) => {
  input += data;
  return;
});
process.stdin.on('end', () => {
    input=input.split('\n');
    var t=Number(input[0]);
    for (let i = 0; i < t; i++) {
            var x = Number(input[i+1]);
            var ans = 0;
            if (x % 233 != 0)
                console.log(-1);
            else {
                x /= 233;
                while (x != 0) {
                    ans += x % 10;
                    x=parseInt(x /= 10);
                }
                console.log(ans);
            }
        }
});

题目内容均收集自互联网,如如若此项内容侵犯了原著者的合法权益,可联系我: (CSDN网站注册用户名: 塔子哥学算法) 进行删除。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

塔子哥学算法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值