在不使用数组的情况下如何解决此问题?我会感谢任何人的帮助 [英] How can I solve this without using arrays? I would appreciate anyone's help

查看:66
本文介绍了在不使用数组的情况下如何解决此问题?我会感谢任何人的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序,不断询问用户输入0到999之间的数字.使用您选择的标记来停止使用数字.获取所有数字后,您需要做的是识别并显示输入的最频繁的数字.例如,如果用户输入了3、17、81、24、354、49和60,则最常见的数字是4.一个非常简单的技巧,几乎可以立即产生结果,您甚至不需要将用户的输入转换为数字,所需要做的就是将所有输入连接到一个巨大的字符串中;然后计算零,一,二等的数量,这可以通过检查由用空字符串替换数字引起的字符串长度变化来完成.这个问题可以用10行代码解决!

:)


如果您确定不想使用数组,则可以尝试将数字简单地放在一个字符串中.然后,您可以遍历字符串中的每个字符,并通过循环跟踪计数.

伪代码:

 私有 字符串 allData;
公共 无效 GetData(字符串 theNumber)
{
    allData + = theNumber.ToString;
}
输入所有数据后,// :
公共 字符串 MostCounted()
{
    字符串 currChar;
     int  currCount;
    字符串 maximumCountedChar;
     int  maximumCharCount;
     for ( int  i =  0 ; i <  = allData.Length- 1 ; i ++){
        currChar = allData.Substring(i, 1 );
         for ( int  i =  0 ; i <  = allData.Length- 1 ; i ++){
            如果(currChar == allData.Substring(i, 1 )){
                currCount + =  1 ;
            }
        }
        如果(currCount >  maximumCharCount){
            maximumCountedChar = currChar;
            maximumCharCount = currCount;
        }
        currCount =  0 ;
    }
    返回 maximumCountedChar;
} 


Write a program that keeps asking the user to enter the numbers between 0 and 999. Use a sentinel of your choice to stop taking numbers. What you need to do after getting all the numbers is to identify and display the most frequent digit that was entered. For example, if the user entered 3, 17, 81, 24, 354, 49, and 60, then the most frequent digit is 4.

解决方案

there is a very simple hack that yields the results almost instantly, you don''t even need to convert the user''s input to numbers, all it takes is to concatenate all inputs in one huge string; then count the number of zeroes, ones, twos, etc. which can be done by checking string length change caused by replacing a digit by an empty string. The problem can be solved in 10 lines of code!

:)


If you are sure you don''t want to use arrays then you can try something like simply placing the numbers together in a string. You can then iterate through each character in the string and keep track of the count through a loop.

psudo code:

private string allData;
public void GetData(string theNumber)
{
    allData += theNumber.ToString;
}
//after all the data is entered:
public string MostCounted()
{
    string currChar;
    int currCount;
    string highestCountedChar;
    int highestCharCount;
    for (int i = 0; i <= allData.Length - 1; i++) {
        currChar = allData.Substring(i, 1);
        for (int i = 0; i <= allData.Length - 1; i++) {
            if (currChar == allData.Substring(i, 1)) {
                currCount += 1;
            }
        }
        if (currCount > highestCharCount) {
            highestCountedChar = currChar;
            highestCharCount = currCount;
        }
        currCount = 0;
    }
    return highestCountedChar;
}


这篇关于在不使用数组的情况下如何解决此问题?我会感谢任何人的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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