我正在编写一个带有一些素数相关方法的小库.因为我已经完成了基础工作(也就是工作方法),现在我正在寻找一些优化.当然,互联网是一个很好的地方.然而,我偶然发现了一个四舍五入的问题,我想知道如何解决这个问题.
在循环中,我用它来测试一个数字,因为它的搜索效率更高,搜索直到sqrt(n)而不是n/2甚至n - 1.但由于舍入问题,一些数字会被跳过,因此会跳过一些素数!例如,第10000个素数应为:104729,但"优化"版本最终为:103811.
一些代码(我知道,它可以进行更多优化,但我一次只能处理一件事):
/// <summary>
/// Method for testing the primality of a number e.g.: return IsPrime(29);
/// History:
/// 1. Initial version, most basic form of testing: m smaller then n -1
/// 2. Implemented m smaller then sqrt(n), optimization due to prime factoring
/// </summary>
/// <param name="test">Number to be tested on primality</param>
/// <returns>True if the number is prime, false otherwise</returns>
public static bool IsPrime(int test)
{
// 0 and 1 are not prime numbers …Run Code Online (Sandbox Code Playgroud) 我在我正在使用的小应用程序中使用CheckedListBox控件.这是一个很好的控制,但有一件事困扰我; 我无法设置属性,以便在我实际选中复选框时仅检查项目.克服这个问题的最佳方法是什么?我一直在考虑从复选框的左侧获取鼠标点击的位置.这部分工作,但如果我点击一个空的空格,左边足够靠近,仍然会检查当前所选项目.关于这个的任何想法?
作为重构动作的结果,我有这个类库.我添加了一个App.config文件并添加了这样的内容:
<configuration>
<connectionStrings>
<add name="MyDatabase" connectionString="Data Source=server;Initial Catalog=database;User ID=userid;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
但是当我运行应用程序时,调试学习我完全被忽略了.即时窗口告诉我:
ConfigurationManager.ConnectionStrings[0]
{data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
base {System.Configuration.ConfigurationElement}: {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
ConnectionString: "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
Key: "LocalSqlServer"
Name: "LocalSqlServer"
Properties: {System.Configuration.ConfigurationPropertyCollection}
ProviderName: "System.Data.SqlClient"
Run Code Online (Sandbox Code Playgroud)
我已经在bin目录中检查了生成的配置文件,其内容与App.config相同.
我尝试使用以下方法读取App.config:
ConfigurationManager.ConnectionStrings[Constants.Connections.DevConnection].ConnectionString
Run Code Online (Sandbox Code Playgroud)
没有什么不寻常的我会说,但出了什么问题?
是的,在我们关于合并细胞之前,我也讨厌它们,但无论如何我都要处理它们.我现在或将来都无法改变这一点.我想尽可能多;)
假设我有一些合并的细胞,我需要确定它跨越的细胞数量.说A1:A4合并,然后我需要返回合并单元格的数量4.有没有办法实现这个目标?
在我正在开发的应用程序中,我正在使用datagridview来显示数据.为了填充它,我要按一个按钮,后台工作人员将开始运行,它将填充一个数据表,当它完成运行时,它将使用数据表作为数据网格的数据源.这很好用,用户界面保持响应等等.但是现在我已经对行进行了着色,这取决于它们的值(我还在玩它,所以欢迎任何建议):
private void ApplyColoring()
{
if (dataGridView1.DataSource != null)
{
foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
{
// hardmap a color to a column
IDictionary<Int32, Color> colorDictionary = new Dictionary<Int32, Color>();
colorDictionary.Add( 7, Color.FromArgb(194, 235, 211));
colorDictionary.Add( 8, Color.Salmon);
colorDictionary.Add( 9, Color.LightBlue);
colorDictionary.Add(10, Color.LightYellow);
colorDictionary.Add(11, Color.LightGreen);
colorDictionary.Add(12, Color.LightCoral);
colorDictionary.Add(13, Color.Blue);
colorDictionary.Add(14, Color.Yellow);
colorDictionary.Add(15, Color.Green);
colorDictionary.Add(16, Color.White);
foreach (DataGridViewRow gridRow in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in gridRow.Cells)
{
if (colorDictionary.Keys.Contains(cell.ColumnIndex))
{
// standard background
cell.Style.BackColor = Color.FromArgb(194, 235, 211);
}
} …Run Code Online (Sandbox Code Playgroud) 我正在学习一些Prolog教程(没什么好做的,本周早些时候我发现我非常喜欢编程,所以我正在研究一些范例)并且参加了一个练习,要求我编写一个谓词delete_from_list/3,它将删除所有从列表中出现的事件.
我已经解决了这个问题如下:
delete_from_list([], _, []).
delete_from_list([Ah|At], X, [Ah|Bt]) :- Ah \= X, !, delete_from_list(At, X, Bt).
delete_from_list([_|Ct], X, Bt) :- delete_from_list(Ct, X, Bt).
Run Code Online (Sandbox Code Playgroud)
我想知道的是,这可能更美观而不是实际目的.你们会以另一种方式做到这一点?为什么?这主要是为了更广泛地理解prolog中解决问题的方法:)例如,这可以在1规则中完成吗?
我正试图解决着名的天际线问题(见gif):
输入
(1,11,5),(2,6,7),(3,13,9),(12,7,16),(14,3,25),(19,18,22),(23 ,13,29),(24,4,28)
应该返回,其他建筑物后面的点应该消失,Y轴的变化坐标应该在返回的天际线中:
(1,11),(3,13),(9,0),(12,7),(16,3),(19,18),(22,3),(23,13),(29 ,0)
我试图通过对算法使用分而治之的方法来实现,以实现O(n lg n)的运行时间,但是我被困在合并部分.
每当我想到它,我都会感到困惑.例如,我检查哪个天际线是第一个,例如哪个具有较低的x坐标,然后我将其+高度添加到我的新天际线.但后来我遇到了另外两个天际线后面的天际线,我怎么才能发现这个"碰撞"?
我是否首先通过检查left.x2 <right.x1来检查天际是否有任何重叠?但是我想我应该先检查一下?在x轴上重叠优先级,一切都变成了一个鸡蛋大蛋.
也许我觉得太复杂了?我需要的是每个交叉点的最高y坐标集,我应该像这样接近吗?
问题陈述
假设我有一个搜索人名的查询:
var result = (from person in container.people select person)
.Where(p => p.Name.Contains(some_criterion)
Run Code Online (Sandbox Code Playgroud)
这将被转换为包含以下like子句的SQL查询:
WHERE NAME LIKE '%some_criterion%'
Run Code Online (Sandbox Code Playgroud)
这有一些性能影响,因为数据库无法有效地使用名称列上的索引(索引扫描与索引搜索,如果我没有记错).
为了解决这个问题,我可以决定只使用StartsWith(),使用like子句生成查询,如:
WHERE NAME LIKE 'some_criterion%'
Run Code Online (Sandbox Code Playgroud)
这使SQL服务器能够以某些功能为代价使用索引查找和交付性能.
我希望能够为用户提供一个选择:默认行为使用StartsWith,但是如果用户希望使用Contains()进行搜索的"增加的灵活性",那么应该使用.
我试过了什么
我认为这是微不足道的,并继续在字符串上实现了一个扩展方法.但是当然,LINQ不接受这个并抛出异常.
现在,我当然可以使用if或switch语句并为每个案例创建一个查询,但我更倾向于在"更高级别"或更一般地解决这个问题.简而言之:由于现实应用程序的复杂性,使用if语句来区分用例是不可行的.这会导致很多重复和混乱.我真的希望能够以某种方式封装变化的行为(Contains,StartsWith,EndsWith).
题
我应该在哪里看,或者我应该寻找什么?这是与IQueryables的可组合性的案例吗?我很困惑!
说我有3个Dictionary<string, string>物体.所有3都有相同的键,如下所示:
Dic1 Dic2 Dic3 K V K V K V A s A z A i B d B e B u C a C r C o D w D t D p
现在,我想将这些词典合并到一个DataTable中,DataTable应该如下所示:
A s z i B d e u C a r o D w t p
关于如何从单独的词典到DataTable中的组合词典的任何指示或想法?
当我学习C++时,我开始实现一些常见的数据结构作为一种实践形式.第一个是Stack(这是第一个想到的).我已经做了一些编程并且它正在工作,但是现在我需要一些输入,否则我应该做什么.喜欢删除某些东西或其他专业提示.我该怎么办?为什么?
template <class T>
class Stack
{
private:
int* values;
int capacity;
int itemsOnStack;
public:
///////////////////
Stack()
{
Stack(32);
}
///////////////////
Stack(const int sz)
{
values = new T[sz];
capacity = sz;
itemsOnStack = 0;
}
~Stack()
{
values = 0;
// delete?
}
////////////////////
void Push(const T& item)
{
*(values + itemsOnStack) = item;
itemsOnStack++;
if(itemsOnStack > capacity)
{
capacity *= 2;
T* temp = new T[capacity];
temp = values;
values = new T[capacity];
values = temp;
}
} …Run Code Online (Sandbox Code Playgroud) 我希望这个描述就足够了,用文字表达的最好方法就是给出一个现在的例子以及我想要它的例子.开始.
当我执行某个方法时,将生成一个结构.其中可能包含以下内容:
> Key: FaceBook, Value: Dinges
> Key: SocialMedia, Value: FaceBook
> Key: Medium, Value: SocialMedia
Run Code Online (Sandbox Code Playgroud)
这些是协会.据此,Dinges与FaceBook相关联,Facebook与SocialMedia相关联,而SocialMedia则与Medium相关联.
现在,我真正需要的是以下布局:
> Key: FaceBook, Value: Dinges
> Key: SocialMedia, Value: FaceBook
> Key: SocialMedia, Value: Dinges
> Key: Medium, Value: SocialMedia
> Key: Medium, Value: Facebook
> Key: Medium, Value: Dinges
Run Code Online (Sandbox Code Playgroud)
我实际上不知道如何处理这个问题.任何帮助将不胜感激
为了与线程,代表和后台工作者一起玩,我正在整理一些小应用程序,我对其中一个有点麻烦.我有一个Windows表单,带有文本框,按钮和richttext.当我按下按钮时,文本框中的文本被用作实例化类的参数,如下所示:
public partial class Form1 : Form
{
private BackgroundWorker backgroundWorker;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += new DoWorkEventHandler(worker_DoWork);
backgroundWorker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
new Thread((ThreadStart)delegate()
{
this.BeginInvoke((ThreadStart)delegate()
{
foreach (string line in textBox1.Lines)
{
Dig digger = new Dig(line, textBox1.Text);
digger.DomainChecked += new Dig.DomainCheckedHandler(OnUpdateTicker);
string response = digger.GetAllInfo();
richTextBox1.AppendText(response);
Application.DoEvents();
}
});
}).Start();
}
void OnUpdateTicker(string msg)
{
new Thread((ThreadStart)delegate()
{
this.BeginInvoke((ThreadStart)delegate()
{
label4.Text …Run Code Online (Sandbox Code Playgroud) c# ×9
recursion ×2
winforms ×2
algorithm ×1
app-config ×1
c++ ×1
datagrid ×1
datatable ×1
dictionary ×1
excel ×1
flicker ×1
linq ×1
math ×1
office-2003 ×1
performance ×1
primes ×1
prolog ×1
stack ×1
vba ×1