进程终止,状态为-107374167(Process terminated with status -107374167)
我试图打印给定数字的素数因子。 我的代码适用于某些输入,但对于其他输入,它会被终止,我无法理解为什么。
样本输入:
1 561473
输出:
2
样本输入:
1 10093
程序终止了。
在某些时候,我认为它可能是浮点异常,但我无法弄清楚原因。 我试着调试每一行代码,发现程序终止于这行代码:
while(ull(n%mem[j]) == 0) {
我仍然无法理解为什么此代码仅针对某些特定输入而中断。 我也试过没有类型铸造,但它仍然给我同样的问题。 我试图打印异常,但程序刚刚终止,它没有打印任何错误。
#include <iostream> #include <string> #include <climits> #include <stdexcept> #include <algorithm> #include <set> #include <cmath> using namespace std; typedef unsigned long long ull; ull RANGE = sqrt(10000), num; unsigned int i; set<ull> arr; ull mem [10000000]; bool isPrime(unsigned long long n) { if(n%2 == 0) return false; for( int i = 3; i <= sqrt(n); i+=2) { if(n%i == 0) return false; } return true; } void hungry() { while(true) { if(num > RANGE) break; if(isPrime(num)) { mem[i++] = num; } num += 2; } } int main() { ios_base::sync_with_stdio (false); cin.tie(NULL); num = 3; i = 1; mem [0] = 2; hungry(); ull ip, n, temp; cin >> ip; while(ip) { cin >> n; arr.clear(); temp = (ull) sqrt(n); if(temp > RANGE) { RANGE = temp; hungry(); } if(n == 1) cout << 0 << "\n"; else { while(n%2 == 0) { arr.insert(2); n = n/2; } for(int j = 1; mem[j] <= (ull)sqrt(n); j++){ while(ull(n%mem[j]) == 0) { arr.insert(mem[j]); n = (ull)n/mem[j]; } } if(n > 2) arr.insert(n); cout << arr.size() << "\n"; } ip--; } return 0; }
I am trying to print the number of prime factors of a given number. My code works fine for some inputs, but for other inputs it's getting terminated and I can't understand why.
Sample Input:
1 561473
Output:
2
Sample Input:
1 10093
And the program terminates.
At some point, I thought it might be floating point exceptions, but I couldn't figure out the reason. I tried to debug every line of code, and found that the program terminates on this line of code:
while(ull(n%mem[j]) == 0) {
I am still unable to understand why this code breaks only for some particular inputs. I have tried without type casting also, still it gives me the same problem. I tried to print the exception, but the program just terminated, it didn't print any error.
#include <iostream> #include <string> #include <climits> #include <stdexcept> #include <algorithm> #include <set> #include <cmath> using namespace std; typedef unsigned long long ull; ull RANGE = sqrt(10000), num; unsigned int i; set<ull> arr; ull mem [10000000]; bool isPrime(unsigned long long n) { if(n%2 == 0) return false; for( int i = 3; i <= sqrt(n); i+=2) { if(n%i == 0) return false; } return true; } void hungry() { while(true) { if(num > RANGE) break; if(isPrime(num)) { mem[i++] = num; } num += 2; } } int main() { ios_base::sync_with_stdio (false); cin.tie(NULL); num = 3; i = 1; mem [0] = 2; hungry(); ull ip, n, temp; cin >> ip; while(ip) { cin >> n; arr.clear(); temp = (ull) sqrt(n); if(temp > RANGE) { RANGE = temp; hungry(); } if(n == 1) cout << 0 << "\n"; else { while(n%2 == 0) { arr.insert(2); n = n/2; } for(int j = 1; mem[j] <= (ull)sqrt(n); j++){ while(ull(n%mem[j]) == 0) { arr.insert(mem[j]); n = (ull)n/mem[j]; } } if(n > 2) arr.insert(n); cout << arr.size() << "\n"; } ip--; } return 0; }
原文:https://stackoverflow.com/questions/39303820
最满意答案
WinForms控件具有名称,但这并不意味着您可以使用该名称作为C#标识符来访问它们。
你的PictureBox在
EventHandler()
只有一个命名的引用,即picture
,但是一旦控制离开那个引用超出范围的方法。您需要再次找到控件,或者找到另一种引用生成的控件的方式。
所以:
var allPictureBoxes = this.Controls.Find("PictureBoxLM"); foreach (var pictureBox in allPictureBoxes) { // ... }
或者把它放在你的表格上:
List<PictureBox> pictureBoxList = new List<PictureBox>();
然后在
EventHandler()
;this.Controls.Add(picture); pictureBoxList.Add(picture);
之后,您可以将其用于碰撞检测:
foreach (var pictureBox in pictureBoxList) { // ... }
WinForms controls have names, but that doesn't mean you can access them using that name as a C# identifier.
Your PictureBox only has a named reference within
EventHandler()
, namelypicture
, but once control leaves that method that reference goes out of scope.You need to find the controls again, or find another way to reference the generated controls.
So either:
var allPictureBoxes = this.Controls.Find("PictureBoxLM"); foreach (var pictureBox in allPictureBoxes) { // ... }
Or put this on your form:
List<PictureBox> pictureBoxList = new List<PictureBox>();
And then in the
EventHandler()
;this.Controls.Add(picture); pictureBoxList.Add(picture);
After which you can use this for your collision detection:
foreach (var pictureBox in pictureBoxList) { // ... }
相关问答
更多-
PictureBox显示和处理(PictureBox Show and Dispose)[2022-03-25]
pictureBox.Visible = true; pictureBox.Visible = false; pictureBox.Visible = true; pictureBox.Visible = false; -
如何刷新PictureBox(How to refresh PictureBox)[2022-03-18]
尝试方法PictureBox.Refresh() (从Control继承)。 Try the method PictureBox.Refresh() (inherited from Control). -
我真的不知道你想做什么,但你可以尝试将对象发送到你的Access类: private void pictureBox1_Click(object sender, EventArgs e) { Access ac = new Access(); ac.PictureClicked(sender); } public void PictureClicked(Object Sender) { picBox = (PictureBox)Sender; ...
-
在PictureBox上绘图(Drawing on PictureBox)[2023-10-13]
你从未分配过Image ,对吗? 如果你想在PictureBox的图像上绘图,你需要首先通过为它指定一个带有PictureBox尺寸的位图来创建这个图像: Graph.Image = new System.Drawing.Bitmap(Graph.Width, Graph.Height); 你只需要这样做一次 ,如果你想在那里重绘任何东西,那么可以重复使用图像。 然后,您可以随后使用此图像进行绘图。 有关更多信息, 请参阅文档 。 顺便说一句,这完全独立于在Paint事件处理程序中绘制PictureBox ... -
Label控件支持透明度。 只是设计师不会让您正确放置标签。 PictureBox控件不是容器控件,因此窗体成为标签的父项。 所以你看到表单的背景。 通过在窗体构造函数中添加一些代码来解决这个问题。 您需要更改标签的父属性并重新计算它的位置,因为它现在相对于图片框而不是窗体。 喜欢这个: public Form1() { InitializeComponent(); var pos = this.PointToScreen(label1.Location); ...
-
PictureBox没有出现(PictureBox doesn't appear)[2022-04-09]
你现在正在做的只是冻结用户界面。 请改用System.Windows.Forms.Timer 。 将计时器从工具箱中拖放到表单上。 然后创建一些Timer可以访问的字段,以存储您的照片和当前的pic位置: private Listpics = new List (); private int currentPic = 0; 最后,用你想要显示的图片加载它,然后启动Timer来完成它们: pics.Clear(); pics.AddRange(date.Where(x => ... -
WinForms控件具有名称,但这并不意味着您可以使用该名称作为C#标识符来访问它们。 你的PictureBox在EventHandler()只有一个命名的引用,即picture ,但是一旦控制离开那个引用超出范围的方法。 您需要再次找到控件,或者找到另一种引用生成的控件的方式。 所以: var allPictureBoxes = this.Controls.Find("PictureBoxLM"); foreach (var pictureBox in allPictureBoxes) { // ...
-
直接绘制到PictureBox(Draw directly to PictureBox)[2023-02-18]
Invalidate()你的PictureBox,所以它重绘自己: Bitmap frame = byteArrayToImage(buff) as Bitmap; using (var graphics = Graphics.FromImage(pictureBox1.Image)) { graphics.DrawImage(frame, left, top); } pictureBox1.Invalidate(); 如果您需要它是线程安全的,那么: pictureBox1.Invoke((Me ... -
在winforms中,您将不得不修改 PictureBox.Image 的alpha 。 要快速执行此操作,请使用ColorMatrix ! 这是一个例子: 跟踪栏代码: Image original = null; private void trackBar1_Scroll(object sender, EventArgs e) { if (original == null) original = (Bitmap) pictureBox1.Image.Clone(); pictureBo ...
-
PictureBox和Dispose(PictureBox and Dispose)[2022-08-21]
您可以将图像读入图片框而不锁定它,如下所示 Image img; string file = @"d:\a.jpg"; using (Bitmap bmp = new Bitmap(file)) { img = new Bitmap(bmp); current_pic.Image = img; } if (File.Exists(file)) { File.Delete(file); current_pic.Image.Save(file, ImageFormat.Jpeg); ...