Questions Marks

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. 
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";
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值