Advertisement
andruhovski

Untitled

Sep 21st, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Prog20180921
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //Дано масив цілих чисел. Подвоїти парні і потроїти непарні значення.
  14.             int[] arr1 = new[] { 1, 2, 3, 4, 5 };
  15.             int[] res1 = OneThread(arr1);
  16.             int[] res2 = ParallelThread(arr1);
  17.         }
  18.  
  19.         private static int[] ParallelThread(int[] arr)
  20.         {
  21.             int[] res = new int[arr.Length];
  22.             Parallel.For(0, arr.Length, (i) =>
  23.             {
  24.                 if (arr[i] % 2 == 0)
  25.                 {
  26.                     res[i] = arr[i] * 2;
  27.                 }
  28.                 else
  29.                 {
  30.                     res[i] = arr[i] * 3;
  31.                 }
  32.             });
  33.             return res;
  34.         }
  35.  
  36.         private static int[] OneThread(int[] arr)
  37.         {
  38.             int[] res = new int[arr.Length];
  39.             for (int i = 0; i < arr.Length; i++)
  40.             {
  41.                 if (arr[i] % 2 == 0)
  42.                 {
  43.                     res[i] = arr[i] * 2;
  44.                 }
  45.                 else
  46.                 {
  47.                     res[i] = arr[i] * 3;
  48.                 }
  49.             }
  50.             return res;
  51.         }
  52.  
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement