序列中缺少数字(数字按降序排列) [英] Missing number in sequence (numbers are in decending order)

查看:174
本文介绍了序列中缺少数字(数字按降序排列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按顺序排列的一组数字按字符串s的顺序排列。但是b / b
字符串s之间缺少其中一个数字。字符串s传递为输入到该程序。该程序必须识别丢失的数字m并将其打印为输出。



a set of numbers which are in sequence are arranged in decending order as string s.but
one of the number in sequence is missing in between in the string s.this string s is passed as input to the program.this program must identify the missing number m and print it as output.

sample input and output:

input:

600599598596

output:

597





我尝试过:



帮助我解决这个问题。



What I have tried:

help me to solve this program.

推荐答案

查看输入字符串:

Look at the input string:
600599598596

要做的第一件事就是识别序列。

首先假设最高数字有1位数:

The first thing to do is to identify the sequence.
Start by assuming the highest number has 1 digit:

6,0,0,5,9,9,5,9,8,5,9,6

这有用吗?不 - 因为你有两个零。

所以试试2位数:

Does that work? No - because you have two zeros together.
So try with 2 digits:

60,05,99,59,85,96

这有用吗?否 - 99大于05

下三位数:

Does that work? No - 99 is larger than 05
Next three digits:

600,599,598,596

Dos有效吗?是。每个值都小于前一个值,并且大多数值都有一个。

缺少什么号码? 597



您可以手动执行此操作,并且您的任务是自动执行该过程 - 但这与手动执行相同,但在代码中。您需要做的就是通过提取每组字符并将其转换为数字来找到正确的数字长度。然后你可以查看数字序列,看看你有什么,以及它是否是一个降序。



从你标记的5个非常不同的语言中选择一种语言,试一试:当你手动开始计算方法时,它并不复杂。

Dos that work? Yes. Each of the values is less than the previous one, and most of them have a difference of one.
What number is missing? 597

You can to that manually just like that, and your task is to automate the process - but that's just the same as doing it manually, but in code. All you need to do is find the right number length by extracting each set of characters and converting that to a number. You can then look at the number sequences and see what you have, and if it's a descending sequence.

Pick a language from the 5 very different ones you tagged, and give it a go: it's not that complex when you start by doing it manually first to work out how.


除了OriginalGriff [ ^ ]的解决方案,我想提供一些改进的示例代码。

我的解决方案基于Linq:

In addition to OriginalGriff[^]'s solution, i'd like to provide sample code with bit of improvement.
My solution is Linq based:
string sentence = "600599598596";
//string sentence = "60595856";
int len = sentence.Length;
int half = len/2;
//here is a magic!
    var dividers =  Enumerable.Range(1, half).Where(i=>len%i==0).ToList();
foreach(int d in dividers)
{
    //create list of chars for later use
    List<int> numbers = new List<int>();
    //loop through the chars in sentence
    //[i] - counter
    int i = 0;
    do
    {
        //get number of chars accordingly  to the value stored in [d]
        string tmp = string.Join("", sentence.Skip(i).Take(d));
        //try parse string into integer
        //if possible, then add it to the list
        int number = 0;
        if (Int32.TryParse(tmp, out number)) numbers.Add(number);
        i+=d;
    } while (i<len);
    //find missing value
    var misVal = numbers.Zip(numbers.Skip(1), (first, second) => Tuple.Create(first, second, first-second-1))
        .Where(x=>x.Item3==1)
        .Select(x=> x.Item2+1)
        .FirstOrDefault();
    Console.WriteLine("For list: [{0}] missing value is: {1}", string.Join(";", numbers), misVal);
}





结果:



Result:

For list: [6;0;0;5;9;9;5;9;8;5;9;6] missing value is: 0
For list: [60;5;99;59;85;96] missing value is: 0
For list: [600;599;598;596] missing value is: 597
For list: [600599;598596] missing value is: 0





如果您想要返回缺失值列表,请将最后一个查询更改为以下格式:



In case you'd like to return list of missing values, change last query into this form:

var misVal = numbers.Zip(numbers.Skip(1), (first, second) => Tuple.Create(first, second, first-second-1))
	.Where(x=>x.Item3==1)
	.Select(x=>x.Item2+1)
	.ToList();





随意根据需要更改代码!



Feel free to change the code to your needs!


这篇关于序列中缺少数字(数字按降序排列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆