显示奇数有序数字的C问题 [英] A C problem showing oddly orderly numbers

查看:83
本文介绍了显示奇数有序数字的C问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果正整数(如183672)具有以下特征,则将其称为奇数有序数:-
1.数字的各个数字交替为奇数,然后是偶数,然后是奇数,依此类推.那是数字的数字,从左到右交替为奇偶.
2.各个奇数位从左到右按升序排列.
3.各个偶数从左到右按降序排列.

输出:所有正整数,其中数字奇数有序..

A positive integer like 183672 will be called an oddly orderly number if it follows following characteristics:-
1. The individual digits of the number are alternating as odd digit followed by an even digit,then followed by an odd digit and so on. That is the digits of the number from left to right alternate as odd-even.
2. Individual odd digits from left to right are in increasing order.
3. Individual even digits from left to right are in decreasing order.

OUTPUT: All positive integers in which the numbers are oddly orderly..

推荐答案

由于我的好奇心,CPallini在这里对我的代码的看法是:

C#4.0 .Net 4.0 IDE Sharp Develop 4.1

Because of my curiosity what CPallini thinks of my code here it is :

C# 4.0 .Net 4.0 IDE Sharp Develop 4.1

using System;
using System.IO;

namespace Oddly_orderly_numbers
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");


            const int start = 12; // min value of oddly number

            int end = 183654729; // max value of oddly number

            int i = 0;//
            int k = 0;//
            int j = 0;//

            int digit = 0;//

            string odd = "";//
            string even = "";//

            string number = "";// String representation of number

            bool is_oddly = false;//

            for(i=start;i<end+1;i++)
            {
                //
                // Test value relation
                // betveen odd and even digits inside of number
                //
                number = Convert.ToString(i);
                k = number.Length;
                is_oddly = false;
                if (k == 2)
                {
                    is_oddly = true;
                }
                else
                {
                    //
                    // test odd values
                    //
                    k = number.Length;
                    odd = "";
                    for (j=0;j<k;j+=2)
                    {
                        odd = odd + number.Substring(j,1);
                    }

                    k = odd.Length-1;
                    is_oddly = true;
                    for(j=0;j<k;j++)
                    {
                        if (odd[j]>=odd[j+1])
                        {
                            is_oddly=false;
                            j=k;
                        }
                    }

                    if (is_oddly)
                    {
                        //
                        // test even values
                        //
                        k = number.Length;
                        even = "";
                        for (j=1;j<k;j+=2)
                        {
                            even = even + number.Substring(j,1);
                        }

                        k = even.Length-1;
                        is_oddly = true;
                        for(j=0;j<k;j++)
                        {
                            if (even[j]<=even[j+1])
                            {
                                is_oddly=false;
                                j=k;
                            }
                        }
                    }
                }

                //
                // Test odd - even position of digits inside of number
                //
                if (is_oddly)
                {
                    number = Convert.ToString(i);
                    k = number.Length + 1;
                    is_oddly = false;
                    for(j=1;j<k;j++)
                    {
                        digit = Convert.ToInt32(number.Substring(j-1,1));
                        if (j%2!=0 && digit%2!=0)
                        {
                            is_oddly = true;
                        }
                        if (j%2==0 && digit%2==0)
                        {
                            is_oddly = true;
                        }
                        else if (j%2!=0 && digit%2==0)
                        {
                            is_oddly = false;
                            j = k;
                        }
                        else if (j%2==0 && digit%2!=0)
                        {
                            is_oddly = false;
                            j = k;
                        }

                    }
                }

                if(is_oddly)
                {
                    StreamWriter sr = new StreamWriter("output.txt",true);

                    Console.WriteLine(number);
                    sr.WriteLine(number);

                    sr.Close();
                }
            }



            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}



程序非常慢,但是我喜欢它,它给出了正确的结果.
我也想看看解决ODDLY问题的最快程序,
对于说最小= 12,最大= 9999999999的范围. (1836547290)
较高的代码将需要几个小时才能解决此任务.
也许这就是挑战!

祝一切顺利,
PerićŽeljko



Program is extremely slow but I like it and it gives correct result.
Also I would like to see fastest program for solving ODDLY problem,
for the range of let say min=12,max=9999999999. (1836547290)
Upper code would need several hours to solve this task.
So may this be the chalenge !

All the best,
Perić Željko


我们不做您的作业:这是有原因的.在这里,您可以考虑自己被告知的内容,并尝试理解它.也可以在那里帮助您的导师识别您的弱点,并将更多的注意力放在补救措施上.

自己尝试,您可能会发现它并不像您想的那么困难!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!


抱歉,我们没有为您编写完整的代码.
只是一个建议:递归.
Sorry we don''t write full code for you.
Just a suggestion: go recursive.


这篇关于显示奇数有序数字的C问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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