Dart 中的程序可以使用 int 但无法使用 BigInt 解决
Program in Dart OK with int but unresolved with BigInt
嗨,
我写了一个程序来解决 Python 中的一个谜题。
为了在 Dart 中取得进步,我尝试用这种语言编写但不幸的是,我没有在那里解决:
Link的拼图: https://www.codingame.com/training/easy/happy-numbers
我的代码有效但不包括 BigInt(所以最后两个测试不包括)
import 'dart:io';
int step(int number)
{
int result = 0;
int ten = 10;
while( number != 0 )
{
result = result + (number % ten )*(number % ten );
number = (number ~/ ten );
}
return result;
}
bool solve(int out, int number)
{
int resultat = number;
while( true )
{
resultat = step(resultat);
if( resultat == 1 )
return true;
if( resultat == 4 )
return false;
}
}
void main() {
int N = int.parse(stdin.readLineSync());
for (int i = 0; i < N; i++) {
int x = int.parse(stdin.readLineSync());
bool res = solve(x,x);
print( res == true ? '$x :)' : '$x :(');
}
}
我已经用 BigInt 实现了这一点
import 'dart:io';
BigInt step(BigInt number)
{
BigInt result = BigInt.parse('0');
BigInt ten = BigInt.parse('10');
while( number != 0 )
{
result = result + (number % ten )*(number % ten );
number = (number ~/ ten );
}
return result;
}
bool solve(BigInt out, BigInt number)
{
BigInt resultat = number;
while( true )
{
resultat = step(resultat);
if( resultat == BigInt.parse('1') )
return true;
if( resultat == BigInt.parse('4') )
return false;
}
}
void main() {
int N = int.parse(stdin.readLineSync());
for (int i = 0; i < N; i++) {
BigInt x = BigInt.parse(stdin.readLineSync());
bool res = solve(x,x);
print( res == true ? '$x :)' : '$x :(');
}
}
我没有发现错误,也不知道如何解决。
:(
如果您将程序粘贴到 dartpad.dartlang.org,您会收到警告:
Equality operator `==` invocation with references of unrelated types - line 7
所以,也许将第 7 行从:
while( number != 0 )
到
while (number != BigInt.zero)
(也可以考虑使用 dart format
格式化您的代码,这使得习惯阅读 Dart 的人更具可读性)。
嗨,
我写了一个程序来解决 Python 中的一个谜题。 为了在 Dart 中取得进步,我尝试用这种语言编写但不幸的是,我没有在那里解决:
Link的拼图: https://www.codingame.com/training/easy/happy-numbers
我的代码有效但不包括 BigInt(所以最后两个测试不包括)
import 'dart:io';
int step(int number)
{
int result = 0;
int ten = 10;
while( number != 0 )
{
result = result + (number % ten )*(number % ten );
number = (number ~/ ten );
}
return result;
}
bool solve(int out, int number)
{
int resultat = number;
while( true )
{
resultat = step(resultat);
if( resultat == 1 )
return true;
if( resultat == 4 )
return false;
}
}
void main() {
int N = int.parse(stdin.readLineSync());
for (int i = 0; i < N; i++) {
int x = int.parse(stdin.readLineSync());
bool res = solve(x,x);
print( res == true ? '$x :)' : '$x :(');
}
}
我已经用 BigInt 实现了这一点
import 'dart:io';
BigInt step(BigInt number)
{
BigInt result = BigInt.parse('0');
BigInt ten = BigInt.parse('10');
while( number != 0 )
{
result = result + (number % ten )*(number % ten );
number = (number ~/ ten );
}
return result;
}
bool solve(BigInt out, BigInt number)
{
BigInt resultat = number;
while( true )
{
resultat = step(resultat);
if( resultat == BigInt.parse('1') )
return true;
if( resultat == BigInt.parse('4') )
return false;
}
}
void main() {
int N = int.parse(stdin.readLineSync());
for (int i = 0; i < N; i++) {
BigInt x = BigInt.parse(stdin.readLineSync());
bool res = solve(x,x);
print( res == true ? '$x :)' : '$x :(');
}
}
我没有发现错误,也不知道如何解决。
:(
如果您将程序粘贴到 dartpad.dartlang.org,您会收到警告:
Equality operator `==` invocation with references of unrelated types - line 7
所以,也许将第 7 行从:
while( number != 0 )
到
while (number != BigInt.zero)
(也可以考虑使用 dart format
格式化您的代码,这使得习惯阅读 Dart 的人更具可读性)。