Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Base64__Контест_
- {
- class Program
- {
- static void Main(string[] args)
- {
- StringBuilder base64 = new StringBuilder();
- List<string> alfavit = new List<string> { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"};
- string binaryNumber = null;
- using(var sr = new StreamReader("base64.in"))
- {
- long n = long.Parse(sr.ReadLine());
- string[] str = sr.ReadLine().Split(' ');
- for(int i = 0; i < n; i++)
- {
- for(int j = 0; j < str[i].Length; j++)
- {
- binaryNumber += System16In2(str[i][j].ToString());
- }
- }
- long flag = 0;
- if(binaryNumber.Length % 6 == 2)
- {
- flag = 2;
- }
- else if(binaryNumber.Length % 6 == 4)
- {
- flag = 1;
- }
- if(flag == 0)
- {
- long m = binaryNumber.Length / 6;
- for (int i = 0; i < m; i++)
- {
- string temp = binaryNumber.Substring(i * 6, 6);
- double index = System2In10(temp);
- base64.Append(alfavit[(int)index]);
- }
- }
- else if(flag == 2)
- {
- long m = binaryNumber.Length / 6;
- for (int i = 0; i < m; i++)
- {
- string temp = binaryNumber.Substring(i * 6, 6);
- double index = System2In10(temp);
- base64.Append(alfavit[(int)index]);
- }
- string lastString = binaryNumber.Substring(binaryNumber.Length - 2, 2);
- lastString += "0000";
- base64.Append(alfavit[(int)System2In10(lastString)]);
- base64.Append("==");
- }
- else
- {
- int m = binaryNumber.Length / 6;
- for (int i = 0; i < m; i++)
- {
- string temp = binaryNumber.Substring(i * 6, 6);
- double index = System2In10(temp);
- base64.Append(alfavit[(int)index]);
- }
- string lastString = binaryNumber.Substring(binaryNumber.Length - 4, 4);
- lastString += "00";
- base64.Append(alfavit[(int)System2In10(lastString)]);
- base64.Append("=");
- }
- }
- using (var sw = new StreamWriter("base64.out"))
- {
- sw.Write(base64);
- }
- }
- public static double System2In10(string str6)
- {
- double result = 0;
- for (int i = 0; i < 6; i++)
- {
- int temp = str6[i] % 48;
- result += temp * Math.Pow(2, 5 - i);
- }
- return result;
- }
- public static string System16In2(string str)
- {
- string result = "";
- switch (str)
- {
- case "0": result += "0000"; break;
- case "1": result += "0001"; break;
- case "2": result += "0010"; break;
- case "3": result += "0011"; break;
- case "4": result += "0100"; break;
- case "5": result += "0101"; break;
- case "6": result += "0110"; break;
- case "7": result += "0111"; break;
- case "8": result += "1000"; break;
- case "9": result += "1001"; break;
- case "A": result += "1010"; break;
- case "B": result += "1011"; break;
- case "C": result += "1100"; break;
- case "D": result += "1101"; break;
- case "E": result += "1110"; break;
- case "F": result += "1111"; break;
- }
- return result;
- }
- }
- }
Add Comment
Please, Sign In to add comment