|
| 1 | +/* this is program written in C# by Alvareto */ |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace hotelRoom |
| 10 | +{ |
| 11 | + class Program |
| 12 | + { |
| 13 | + static int[] getDivisors (int number) |
| 14 | + { |
| 15 | + List<int> divisors = new List<int>(); |
| 16 | + for (int i = number; --i > 0; ) |
| 17 | + if (number % i == 0) |
| 18 | + divisors.Add(i); |
| 19 | + return divisors.ToArray(); |
| 20 | + } |
| 21 | + |
| 22 | + //get the smallest integer m greater than n, |
| 23 | + //both n and m have same number of '1' in their binary string |
| 24 | + static int getNextN(int n){ |
| 25 | + int temp1 = n & (-n);//point out the first '1' in n's binary string form right to left |
| 26 | + int temp2 = n + temp1; |
| 27 | + int ret = temp2 | ((n ^ temp2) / temp1) >> 2; |
| 28 | + return ret; |
| 29 | + } |
| 30 | + static List<List<int>> getSubsets(int[] divisors) |
| 31 | + { |
| 32 | + List<List<int>> subsets = new List<List<int>>(); |
| 33 | + int num = divisors.Length; |
| 34 | + for (int i = 1; i <= num; i++) { //get divisors's i element subsets |
| 35 | + int beginIdx = (1 << i) - 1; |
| 36 | + int endIdx = (1 << num) - (1 << (num - i)); |
| 37 | + for (int j = beginIdx; j <= endIdx; ) { //the index of '1' in j's binary string from rigth to left indicates divisors[index] is in the subset |
| 38 | + //TODO:output subset |
| 39 | + List<int> subset = new List<int>(); |
| 40 | + int index = 0; |
| 41 | + int k = j; |
| 42 | + while (k>0) { |
| 43 | + if (k % 2 == 1) subset.Add(divisors[index]); |
| 44 | + k /= 2; |
| 45 | + index++; |
| 46 | + } |
| 47 | + subsets.Add(subset); |
| 48 | + j = getNextN(j); |
| 49 | + } |
| 50 | + } |
| 51 | + return subsets; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + static bool isRoom(List<List<int>> subsets, int room) |
| 56 | + { |
| 57 | + foreach (List<int> subset in subsets) |
| 58 | + { |
| 59 | + if (subset.Sum() == room) |
| 60 | + return false; |
| 61 | + } |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + static void findRoom(int totalRooms) |
| 66 | + { |
| 67 | + for (int i = 0; ++i < 101; ) |
| 68 | + { |
| 69 | + int[] divisors = getDivisors(i); |
| 70 | + int sum = divisors.Sum(); |
| 71 | + |
| 72 | + if (sum <= i) |
| 73 | + continue; |
| 74 | + |
| 75 | + if (isRoom(getSubsets(divisors), i)) |
| 76 | + { |
| 77 | + Console.WriteLine(i); |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + static void Main(string[] args) |
| 84 | + { |
| 85 | + int input = Convert.ToInt32(Console.ReadLine()); |
| 86 | + findRoom(input); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments