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)
            {
    
            }
        }
    }

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



  • 相关阅读:
    Qt 实现遥感图像显示时的连动效果
    Qt 调试时的错误——Debug Assertion Failed!
    从零开始--系统深入学习IOS(使用Swift---带链接)
    在别人客户端上修改,来匹配测试自己的服务端
    趣拍SDK接入问题Android
    持久化数据
    实现编辑和删除
    实现导航
    创建一个Table View
    定义你的数据模型
  • 原文地址:https://www.cnblogs.com/Jesuslovesme/p/8448482.html
Copyright © 2011-2022 走看看