Advertisement
vovanhoangtuan

TH - Đếm trùng

May 3rd, 2020
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.15 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. using System.IO;
  7.  
  8. namespace func
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             const string fileInput = "../../input.txt";
  15.             const string fileOutput = "../../output.txt";
  16.  
  17.             int[] inp = readFile(fileInput);
  18.  
  19.             int dapan = task(inp);
  20.  
  21.             writeFile(fileOutput, dapan.ToString());
  22.  
  23.         }
  24.  
  25.         public static int task(int[] a)
  26.         {
  27.             int total = 0;
  28.             int[] temp = new int[findMax(a) + 1];
  29.             foreach(int value in a)
  30.             {
  31.                 temp[value]++;
  32.             }
  33.             foreach (int value in temp)
  34.             {
  35.                 if (value >= 2) total += value;
  36.             }
  37.  
  38.             return total;
  39.  
  40.         }
  41.  
  42.         public static int findMax(int[] a)
  43.         {
  44.             int max = a[0];
  45.             for (int i = 1; i < a.Length; i++)
  46.             {
  47.                 max = Math.Max(a[i], max);
  48.             }
  49.             return max;
  50.         }
  51.  
  52.         public static int[] readFile(string path)
  53.         {
  54.            
  55.             FileStream fs = new FileStream(path, FileMode.Open);
  56.             StreamReader sReader = new StreamReader(fs, Encoding.UTF8);
  57.             int n = int.Parse(sReader.ReadLine());
  58.             int[] res = new int[n];
  59.             string[] temp = sReader.ReadLine().Split(' ');
  60.             for (int i = 0; i < n; i++)
  61.             {
  62.                 res[i] = int.Parse(temp[i]);
  63.             }
  64.             sReader.Close();
  65.             fs.Close();
  66.             Console.WriteLine("Doc file thanh cong !");
  67.             return res;
  68.  
  69.         }
  70.  
  71.         public static void writeFile(string path, string data)
  72.         {
  73.  
  74.             FileStream fsW = new FileStream(path, FileMode.Create);
  75.             StreamWriter sWriter = new StreamWriter(fsW, Encoding.Unicode);
  76.             sWriter.Write(data);
  77.             sWriter.Flush();
  78.             fsW.Close();
  79.             fsW.Close();
  80.             Console.WriteLine("Ghi file thanh cong !");
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement