zoukankan      html  css  js  c++  java
  • BitArray编写埃拉托斯特尼筛法(原书错误,学习更正)

    刚开始代码无法运行,修改后原书代码可以运行了,可是书本的思想还是错的。

    虽然接下来的都是讲错误的思想下的“错误”的修改。

    原书缺了窗体控件的代码,虽然在VS下不需要手动写窗体的代码,但是刚开始确实也不会怎么弄窗体

    记录窗体拖拽的方法:

      首要的一步是新建一个Windows窗体应用程序:文件 --> 新建 --> 项目 --> 选择Windows窗体应用程序;

      此时VS界面左侧应当要有“工具箱”,有的话这里面的控件就可以直接拖了,没有的话需要设置

          在第一排的菜单栏 -->点“重置窗口布局” -->左侧显示“工具箱” -->单击“工具箱” --> 选点击“公共控件” -->可拖拽各控件(Button/Label/ListBox/ListView/TextBox......)

    书本窗体复原:

    书本代码可运行版:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication3
    {
        public partial class Form1: Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                BitArray bitSet = new BitArray(1024);
                int value = Int32.Parse(txtValue.Text);
                BuildSieve(bitSet);
              //  Console.WriteLine("bitSet.Get(value)" + bitSet.Get(value));
                if(bitSet.Get(value))
                    lblPrime.Text = (value + "is a prime number." + bitSet.Get(value));
                else
                    lblPrime.Text = (value + "is not a prime number." + bitSet.Get(value));
            }
            private void BuildSieve(BitArray bits)
            {
                string primes="";//这里要赋值,否则出错
                for (int i = 0; i <= bits.Count - 1; i++)
                    bits.Set(i, true);
                int lastBit = Int32.Parse(Convert.ToString(Math.Sqrt(bits.Count)));//注意:Int32.Parse(string);
                for (int i = 2; i <= lastBit - 1; i++)
                {
                    if (bits.Get(i))
                        for (int j = 2 * i; j <= bits.Count - 1; j++)
                            bits.Set(j, false);
                    int counter = 0;
    
                    for(int k=1;k<=bits.Count-1;k++)
                    {
                        if(bits.Get(k))
                        {
                            primes +=k.ToString();
                            counter++;
                            if((counter%7)==0)
                                primes+="	";     
                            else
                                primes+="
    ";
                        }
                    }
                    txtPrimes.Text = primes;
    
                }
    
            }
    
            private void SeiveofEratosthenes_Load(object sender, EventArgs e)
            {
    
            }
        }
    }

    但遗憾最后运行的结果与思想是不对的:



  • 相关阅读:
    jQuery实现鼠标点击Div区域外隐藏Div
    JS判断输入值为正整数
    trim()不兼容ie的问题及解决方法
    傻问题就用傻办法:解决问题有时候不需要探究根源,依据表象就能直接解决
    /vendor/lib64/libOpenCL.so在安卓应用中无访问权限的解决办法
    复数域上的人工神经网络与量子计算
    中国移动CMCC家庭路由器的默认登陆账号
    717. 1-bit and 2-bit Characters
    219. Contains Duplicate II
    1346. Check If N and Its Double Exist
  • 原文地址:https://www.cnblogs.com/Jesuslovesme/p/8448482.html
Copyright © 2011-2022 走看看