![](/img/trans.png)
[英]How or is it possible to store an array of integers into a dictionary in C#? i.e. is <Integer, int[]> possible?
[英]How to get integers to an array (from integer i to integer j). c#
我想存储整数(在数组或任何东西中)在 int "i" 和 int "j" 范围内。
例如:-想想,“int i = 1”和“int j = 10”。我想存储 1 和 10 的整数。所以 (1,2,3,4,5,6,7,8,9, 10)
因为我想回答HackerRank“电影中的美好时光”。 下方链接。
https://www.hackerrank.com/challenges/beautiful-days-at-the-movies/problem?isFullScreen=false
这是我的代码,它是垃圾。
static int beautifulDays(int i, int j, int k) {
var total = 0;
for(var a = i; a <= j; a++ )
{
if (a != 0)
{
int ri = Reverse(i);
int rj = Reverse(j);
var ra = Reverse(a);
if((ra/k) % 1 == 0)
{
total++;
}
if((rj/k) % 1 == 0)
{
total++;
}
if((ri/k) % 1 == 0)
{
total++;
}
}
return total;
}
return total;
}
public static int Reverse(int inval)
{
int result = 0;
do
{
result = (result * 10) + (inval % 10);
inval = inval / 10;
}
while(inval > 0);
return result;
}
简单,你能给我HackerRank“电影中的美好时光”的答案吗? 下方链接。
https://www.hackerrank.com/challenges/beautiful-days-at-the-movies/problem?isFullScreen=false
使用 Java,您可以轻松地使用 IntStream 流式传输一系列数字,然后为每个值映射反向函数,然后过滤满足条件和计数的那些。 使用不需要存储的流,您可以直接找到答案。
IntUnaryOperator reverse = (opperand) -> {
int reversed = 0;
int num = opperand;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return Math.abs(opperand - reversed);
};
return (int) IntStream.rangeClosed(i, j).map(reverse)
.filter(v -> v % k == 0).count();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.