Challenge
Using the C# language, have the function
QuestionsMarks(str) take the
str string parameter, which will contain single digit numbers, letters, and question marks, and check if there are exactly 3 question marks between every pair of two numbers that add up to 10. If so, then your program should return the string
true, otherwise it should return the string
false. If there aren't any two numbers that add up to 10 in the string, then your program should return
false as well.
For example: if str is "arrb6???4xxbl5???eee5" then your program should return true because there are exactly 3 question marks between 6 and 4, and 3 question marks between 5 and 5 at the end of the string.
For example: if str is "arrb6???4xxbl5???eee5" then your program should return true because there are exactly 3 question marks between 6 and 4, and 3 question marks between 5 and 5 at the end of the string.
Sample Test Cases
Input:"aa6?9"
Output:"false"
Input:"acc?7??sss?3rr1??????5"
Output:"true"
Questions Marks算法
1、输入的字符串包含数字、字母和问号;
2、如果两个数字和为10暂且中间刚好有3个问号,返回true;否则返回false;
3、如果没有数字加起来为10,返回false
public static string QuestionsMarks(string str)
{
int Length = str.Length;
int startIndex = -1;
int questionMarks = 0;
List<int> NUMlist = new List<int>();
for (int i = 0; i < Length; i++)
{
if (Char.IsNumber(str[i]))
{
if (startIndex == -1)
{
startIndex = i;
}
else
{
if (Convert.ToInt32(str[startIndex].ToString()) + Convert.ToInt32(str[i].ToString()) == 10 && questionMarks != 3)
{
return "false";
}
else
{
questionMarks = 0;
startIndex = i;
}
}
NUMlist.Add(Convert.ToInt32(str[i].ToString()));
}
else if (str[i] == '?' && startIndex != -1)
{
questionMarks++;
}
}
bool rStatus = false;
for (int i = 0; i < NUMlist.Count; i++)
{
for (int j = i + 1; j < NUMlist.Count; j++)
{
if (NUMlist[i] + NUMlist[j] == 10)
{
rStatus = true;
break;
}
}
if (rStatus)
{
break;
}
}
if (!rStatus)
{
return "false";
}
return "true";
}