我正在处理在线考试申请,在考试登录问题上显示的是4
或6
选项。让我们采用4
选项a b c d
,如果用户选择a
,则如果1
的值是b
,如果2
的值是c
如果4
,则值d
;如果8
,则值e
;如果16
,则值f
;然后,值32
:
a -> 1
b -> 2
c -> 4
d -> 8
e -> 16
f -> 32
用户可以选择多个选项,假设用户选择a
和b
,则答案是1 + 2 == 3
,三个是正确答案,或者用户选择{{1} },那么a d
是正确的答案。
什么是将1 + 8 == 9
分解为9
或1, 8
分解为11
或1, 2, 8
分解为{{1 }}?
答案 0 :(得分:2)
只需转换您的电话号码,例如11至二进制。对于11,您得到1011。现在,二进制数的每个数字都代表一个选择选项(但相反)。就像在这种情况下,此人选择了第一个,第二个和最后一个选项。
有帮助吗?
答案 1 :(得分:2)
按位操作似乎也是可行的方法。
既然您使用Angular进行了标记,那么我将使用Javascript解决方案:
function breakdown(input) {
var values = [1, 2, 4, 8, 16, 32];
var checked = [];
for (v of values) {
if (!!(input & v)) checked.push(v);
}
return checked;
}
测试用例:
breakdown(9)
> [1, 8]
breakdown(11)
> [1, 2, 8]
breakdown(35)
> [1, 2, 32]
答案 2 :(得分:2)
对我有用。
static void Main()
{
List<int> options = new List<int>() { 1, 2, 4, 8, 16, 32 };
List<int> selectedOptions = new List<int>();
int aggrigatedAnswer = 22;
BreakDown(aggrigatedAnswer, options, selectedOptions);
Console.Write(string.Join(",", selectedOptions));
}
public static int BreakDown(int value, List<int> options, List<int> selectedOptions)
{
if (value <= 0)
return 0;
else
{
int option = options.Where(a => a <= value).Max();
value -= option;
selectedOptions.Add(option);
BreakDown(value, options, selectedOptions);
return value;
}
}
输出
16,4,2
答案 3 :(得分:1)
似乎分解是基于二进制数:
9 => 0b1001 => 0b1000 + 0b0001 => 8 + 1
11 => 0b1011 => 0b1000 + 0b0010 + 0b0001 => 8 + 2 + 1
代码:
private static IEnumerable<int> BreakDown(int value) {
for (int power = 1; value > 0; value /= 2, power *= 2)
if (value % 2 != 0)
yield return power;
}
private static IEnumerable<char> BreakDownChars(int value) {
for (int i = 0; value > 0; value /= 2, ++i)
if (value % 2 != 0)
yield return (char)('a' + i);
}
演示:
int[] tests = new int[] {
9,
11,
35,
};
string result = string.Join(Environment.NewLine, tests
.Select(test => $"{test,2} into {string.Join(", ", BreakDown(test))}"));
Console.WriteLine(result);
结果:
9 into 1, 8
11 into 1, 2, 8
35 into 1, 2, 32
或者如果您想要字符(a..f
)
string result = string.Join(Environment.NewLine, tests
.Select(test => $"{test,2} into {string.Join(", ", BreakDownChars(test))}"));
Console.WriteLine(result);
结果:
9 into a, d
11 into a, b, d
35 into a, b, f
答案 4 :(得分:0)
解决方案1
代码:
static readonly Dictionary<char,int> dict = new Dictionary<char,int>
{
['a'] = 1,
['b'] = 2,
['c'] = 4,
['d'] = 8,
['e'] = 16,
['f'] = 32
};
public static void Main()
{
var i = 11;
var answers = GetCombination(i);
Console.WriteLine($"Letters: {string.Join(", ", answers.Keys)}");
Console.WriteLine($"Numbers: {string.Join(", ", answers.Values)}");
}
static Dictionary<char,int> GetCombination(int i) =>
dict.Where(p => (p.Value & i) == p.Value).ToDictionary(p => p.Key, p => p.Value);
输出:
Letters: a, b, d
Numbers: 1, 2, 8
解决方案2
代码:
enum Dummy { }
static readonly Dummy[] dummies = new[] { 1, 2, 4, 8, 16, 32 }.Select(d => (Dummy)d).ToArray();
public static void Main()
{
var i = 11;
var answers = GetCombination(i);
Console.WriteLine(string.Join(", ", answers));
}
static int[] GetCombination(int i)
{
var dummy = (Dummy)i;
return dummies.Where(d => dummy.HasFlag(d)).Cast<int>().ToArray();
}
输出
1, 2, 8