我有一个任务,我必须输入一个数字,并找出所有素数,但不超过该数字.例如,如果我在程序中输入9,它应该打印3,5和7.
我确定数字是否为素数的计划是将其除以2并检查余数是否为0.如果余数为0,则程序从被除数中减去1,然后循环回到顶部再次除以.如果余数!= 0则将其打印到屏幕上,并再次递减红利.这种情况一直发生,直到被除数为0.只有这不是正在发生的事情,无论出于什么原因,每当我使用DIV指令时,我总是得到浮点异常,我似乎无法弄清楚为什么或如何解决它.任何人对我如何解决这个问题都有任何想法?
Code: %INCLUDE "csci224.inc"
SEGMENT .data
prompt: DD "Please enter a number: ",0 ; prompt string
message: DD " is prime.", 0 ; displays when n is prime
invalid: DD "Invalid entry.", 0
i: DD 2
SEGMENT .bss
input: RESD 100 ; not really necessary, ignore this
SEGMENT .text
main:
mov edx, prompt
call WriteString
call ReadInt
mov esi, eax ; move eax into esi to use as index for loop
myloop:
xor edx, edx ; … 我需要创建一个程序,它接受由空格分隔的整数输入,例如:
4 4 5 8 8 9
Run Code Online (Sandbox Code Playgroud)
程序然后获取这些数字并计算每个数字的出现次数,因此上述输入的输出将是:
The number 4 has 2 occurrence(s)
The number 5 has 1 occurrence(s)
The number 8 has 2 occurrence(s)
The number 9 has 1 occurrence(s)
Run Code Online (Sandbox Code Playgroud)
我几乎弄明白这一点,当我为数字没有空格分隔的输入(假设它们是1位整数,而不是我为最终版本做出的假设)时它工作得很好但是只要输入在它不再有效的数字之间有空格.
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
vector<int> parseString(string &s);
void parseVector(vector<int> &v);
int checkRepeats(vector<int> &v, int n);
void printVector(vector<int> &v);
int main()
{
vector<int> parsed;
vector<int> numbers;
string input;
bool keepGoing = true;
int nRepeats; // stores the number of …Run Code Online (Sandbox Code Playgroud)