using System;
class Program
{
static void Main(string[] args)
{
int pass = 792531;
Console.WriteLine("password = {0}", pass);
int[] arr = IntToArray(pass);
int[] encoded = Encode(arr);
int encodePass = ArrayToInt(encoded);
Console.WriteLine("encode_pass = {0}", encodePass);
int[] decoded = Decode(encodePass);
int decodePass = ArrayToInt(decoded);
Console.WriteLine("decode_pass = {0}", decodePass);
}
// ---------------------------------------------------------
// Encode: swap digit 1 with second‑last, and digit 2 with third‑last
// ---------------------------------------------------------
public static int[] Encode(int[] arr) {
int[] copy = (int[])arr.Clone();
Swap(copy, 1, copy.Length - 2);
Swap(copy, 2, copy.Length - 3);
return copy;
}
// ---------------------------------------------------------
// Decode: reverse the same swaps
// ---------------------------------------------------------
public static int[] Decode(int encoded) {
int[] arr = IntToArray(encoded);
Swap(arr, arr.Length - 2, 1);
Swap(arr, arr.Length - 3, 2);
return arr;
}
// ---------------------------------------------------------
// Convert integer → array of digits
// ---------------------------------------------------------
public static int[] IntToArray(int num) {
char[] chars = num.ToString().ToCharArray();
int[] arr = new int[chars.Length];
for (int i = 0; i < chars.Length; i++)
arr[i] = chars[i] - '0';
return arr;
}
// ---------------------------------------------------------
// Convert array of digits → integer
// ---------------------------------------------------------
public static int ArrayToInt(int[] arr) {
int value = 0;
foreach (int d in arr)
value = value * 10 + d;
return value;
}
// ---------------------------------------------------------
// Swap helper
// ---------------------------------------------------------
public static void Swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
/*
run:
password = 792531
encode_pass = 735291
decode_pass = 792531
*/