为了更好的阅读体检,可以查看我的算法学习网
在线评测链接:P1012
题目大意
“哈哈数”定义为: 233 ∗ 1 0 i 233 * 10^i 233∗10i. 例如: 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<=1e14,1<=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网站注册用户名: 塔子哥学算法) 进行删除。