如何在伪代码中创建一个dowhile循环? [英] How do I make a dowhile loop in pseudocode?

查看:1535
本文介绍了如何在伪代码中创建一个dowhile循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序将从输入中获取素数,然后对所有已检查的素数求和。



我的尝试:



The program will get the prime numbers from input and then sum all the checked prime numbers.

What I have tried:

Prompt for input
X = 0
prime_amount = 0
prime_sum = 0
DOWHILE X < input
Prompt for prime_number
Y = 2 
Prime = TRUE
IF prime_number = 1 THEN 
                                 Prima = FALSE
           		ENDIF
DOWHILE Y <= prime_number AND Prime = TRUE
IF prime_number Mod Y = 0 THEN
Prime = FALSE
                        ENDIF
Y = Y + 1
ENDDO
IF Prime = TRUE THEN 
prime_amount = prime_amount + 1
prime_sum = prime_sum + prime_number
ENDIF
ENDDO
Output "Amount of prime numbers" + prime_amount
Output "Total sum of prime numbers" + prime_sum
END

推荐答案

目前还不清楚你需要什么,但有一个<您的伪代码中的code> DOWHILE ... ENDDO ,并将C转换为:

It's not exactly clear what you need, but there is a DOWHILE...ENDDO in your pseudocode, and that translates to C as:
while(Y <= prime_number && Prime)
   {
   ... body of the loop
   }

但是那是微不足道的!



如果那不是无论你在寻找什么,那么你需要更详细地解释你想要达到的目标。

But that's trivial!

If that isn't what you are looking for, then you need to explain in much, much better detail what you are trying to achieve.


我会用头,身,脚这样写:< br $>
I would write it with head, body and foot this way:
DO
  ///instructions
WHILE condition = TRUE

但是你最好阅读所需伪代码样式的文档,或者问你的老师。

But you better read the documentation of the needed pseudo code style, or ask your teacher.


学会正确缩进你的代码和伪代码,它显示了它的结构,它有助于阅读和理解。它还有助于发现结构错误。

Learn to indent properly your code and pseudocode, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Prompt for input
X = 0
prime_amount = 0
prime_sum = 0
DOWHILE X < input
	Prompt for prime_number
	Y = 2
	Prime = TRUE
	IF prime_number = 1 THEN
		Prima = FALSE
	ENDIF
	DOWHILE Y <= prime_number AND Prime = TRUE
		IF prime_number Mod Y = 0 THEN
			Prime = FALSE
		ENDIF
		Y = Y + 1
	ENDDO
	IF Prime = TRUE THEN
		prime_amount = prime_amount + 1
		prime_sum = prime_sum + prime_number
	ENDIF
ENDDO
Output "Amount of prime numbers" + prime_amount
Output "Total sum of prime numbers" + prime_sum
END



专业程序员的编辑器具有此功能以及其他功能,例如括号匹配和语法高亮。

Notepad ++ Home [ ^ ]

ultraedit [ ^ ]

-----

建议:

- 初始化循环计数器尽可能接近循环,它有助于读取


Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
-----
Advices:
- initialize a loop counter as close as possible of the loop, it help reading

Prompt for input
X = 0
prime_amount = 0
prime_sum = 0
DOWHILE X < input
	Prompt for prime_number
	Y = 2
	Prime = TRUE
	IF prime_number = 1 THEN
		Prima = FALSE
	ENDIF
	Y = 2
	DOWHILE Y <= prime_number AND Prime = TRUE
		IF prime_number Mod Y = 0 THEN
			Prime = FALSE
		ENDIF
		Y = Y + 1
	ENDDO
	IF Prime = TRUE THEN
		prime_amount = prime_amount + 1
		prime_sum = prime_sum + prime_number
	ENDIF
ENDDO
Output "Amount of prime numbers" + prime_amount
Output "Total sum of prime numbers" + prime_sum
END



- 您的算法将因为'='而无法工作


- Your algorithm will not work because of the '=' here

DOWHILE Y <= prime_number AND Prime = TRUE



这里的'='将使 y = prime_number 在最后一个循环中,因此 y 将是 prime_number 的除数。 />
- 你的算法非常蛮力,矫枉过正。想一想,你需要测试哪些除数才能知道97是否是素数?


This '=' here will make y=prime_number on last loop, and thus y will be a divisor of prime_number.
- Your algorithm is very brute force, overkill. Think about it, which divisors do you need to test to know if 97 is a prime or not?


这篇关于如何在伪代码中创建一个dowhile循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆