How do you multiply elements of?



  • Let's say there is. int A mass of 9 elements to be multiplied to calculate the reference amount for the following algorithm described:

    Указан СНИЛС 112-233-445 95
    Проверяем правильность контрольного числа:
    цифры номера        1 1 2 2 3 3 4 4 5
    номер позиции       9 8 7 6 5 4 3 2 1
    Сумма = 1×9 + 1×8 + 2×7 + 2×6 + 3×5 + 3×4 + 4×3 + 4×2 + 5×1 = 95
    95 ÷ 101 = 0, остаток 95.
    Контрольное число 95 — указано верно
    

    How can we do this carefully? The code is:

    Console.Write("snils_crc>"); str_snls = Console.ReadLine();
    

    //Генерируем исключение для проверки воодимх данных.
    try
    {
    str_snls_num = Convert.ToInt32(str_snls);

    char[] chr_snls = str_snls.ToCharArray();
    int[] int_snls = new int[chr_snls.Length];
    
    for (int i = 0; i < chr_snls.Length; i++)
    {
        int_snls[i] = int.Parse(chr_snls[i].ToString());
    }
    
    for (int i = 0; i < int_snls.Length; i++)
    {
        Console.Write(int_snls[i].ToString());
    }
    

    }
    catch
    {
    Console.WriteLine("Error");
    }

    This is where the user turns the numbers that are converted into char[]And then in int[] A mass. In the second cycle, it's just shy. Help me draw up an appropriate cycle.


  • QA Engineer

    Here.

    var numbers = Console.ReadLine().Split().Select(int.Parse).ToList();
    var positions = Console.ReadLine().Split().Select(int.Parse).ToList();
    var crc = numbers.Zip(positions, (x, y) => x * y).Sum() % 101;
    

    You don't need the numbers, leave them in the last millennium.


    Yeah, you've got another form of entry. No problem.

    var input = "112-233-445 95";
    var split = input.Split();
    var digits = split[0].Where(char.IsDigit).Select(c => c - '0').Reverse();
    var computedCrc = digits.Select((d, index) => d * (index + 1)).Sum() % 101;
    var crc = int.Parse(split[1]);
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2