帮我改进代码...... [英] Help me improve my code...

查看:102
本文介绍了帮我改进代码......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Prime_digit_replacements
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter number of members of the family: ");
            int fn = int.Parse(Console.ReadLine());
            int change = 1;
            if(fn > 6)
            {
                change = fn - 5;
            }
            int number = Convert.ToInt32(Math.Pow(10, change));

            List<int> tprime = new List<int>();
            int[] family = new int[fn];
            int i = 0;
            int leng = number;
            int temp = 0;
            bool a = true;
            while (a)
            {
                number = MyMath.Here.NextPrime(number);
                i = 0;
                temp = number;
                family[i] = number;
                tprime.Clear();
                while (temp.ToString().Length == number.ToString().Length)
                {
                    tprime.Add(temp);
                    temp = MyMath.Here.NextPrime(temp);
                }
                i++;
                int dis = 0;
                int temporary = number;
                foreach (int mun in tprime)
                {
                    dis = number.ToString().Distinct().Count();
                    i = 1;
                    temporary = number;
                    family[0] = number;
                    foreach (int tr in tprime)
                    {
                        if(number > tr)
                        {
                            continue;
                        }
                        if (tr.ToString().Distinct().Count() <= dis)
                        {
                            if (!family.Contains(tr) && number % 10 == tr %10)
                            {
                                if (Repeat(temporary, tr, change))
                                {
                                    family[i] = tr;
                                    i++;
                                    if (i == fn)
                                    {
                                        a = false;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (!a)
                    {
                        break;
                    }
                    number = mun;
                }
            }

            foreach(int v in family)
            {
                Console.WriteLine(v);
            }
        }

        public static bool Repeat(int a, int b, int num)
        {
            int leng = Convert.ToInt32(Math.Floor(Math.Log10(a))) + 1;
            int[] aa = new int[leng];
            int[] bb = new int[leng];
            for(int i = 0; i < leng; i++)
            {
                aa[i] = a % 10; a /= 10;
                bb[i] = b % 10; b /= 10;
            }
            int j = 0;
            int[] repa = new int[num];
            int[] repb = new int[num];
            int y = 0;
            while (j < leng)
            {
                if(aa[j] != bb[j])
                {
                    if (y >= num) { return false; }
                    repa[y] = aa[j];
                    repb[y] = bb[j];
                    y++;
                }
                j++;
            }
            if(y != num)
            {
                return false;
            }
            int tempa = repa[0];
            int tempb = repb[0];
            foreach(int tr in repa)
            {
                if(tr != tempa)
                {
                    return false;
                }
            }
            foreach (int tr in repb)
            {
                if (tr != tempb)
                {
                    return false;
                }
            }
            return true;
        }
    }
}







这是< br $>
MyMath.Here.NextPrime






Here is the
MyMath.Here.NextPrime

public static int NextPrime(int lastprime)
        {
            int a = 0;
            if (IsPrime(lastprime))
            {
                a = 2;
            }
            else
            {
                a = 1;
            }

            if(lastprime < 2)
            {
                return 2;
            }
            else if(lastprime == 2)
            {
                return 3;
            }
            lastprime += a;
            while (!IsPrime(lastprime))
            {
                lastprime += a;
            }
            return lastprime;
            
        }







如何让它更快?



我尝试过:



这很慢..

请帮我改进它。




How to make it Faster??

What I have tried:

It is slow..
Please help me improve it.

推荐答案

为了提高速度,需要避免字符串转换。仅使用数字,并避免任何数据推荐。否则使用一些结构数据来缓存结果。

For higher speed the string conversions need to be avoided. Work only with numbers, and avoid any recomuting of data. Else use some struct data to cache results.
///int leng = Convert.ToInt32(Math.Floor(Math.Log10(a))) + 1// slow;
int leng = (int) Math.Log10(a)) + 1;//should be faster

还要避免控制台输出也会减慢速度。或仅用于调试使用。



如果要反转一个数字:print as string,string reverse and parse to number。但请参阅提示2.



提示:要获得数字的长度,您可以使用 log10



提示2:为子功能编写测试代码并循环它们一百万轮以获得感觉其中代码很慢。

Also avoid console output which also slows down. Or only for debugging use.

If you want to reverse a number: print as string, string reverse and parse to number. But see Tip 2.

Tip: to get the length of a number you can use the log10.

Tip 2: write test code for subfunction and loop them a million rounds to get a feeling where the code is slow.


引用:

如何让它更快?



首先要做的是避免对什么是快速和什么不做假设。


First thing to do is to avoid making assumption on what is fast and what is not.

引用:

下一个素数很快



唯一要做的就是使用为此制作的工具:使用程序分析器。

程序分析器将测量程序每个部分所花费的时间以及每个部分执行的时间。有了这些信息,您就会知道在哪里寻找优化。



分析(计算机编程) - 维基百科 [ ^ ]

性能分析工具列表 - 维基百科 [ ^ ]


这篇关于帮我改进代码......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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