将草稿转换为算法

发布于 2025-01-13 10:21:08 字数 428 浏览 3 评论 0原文

我第一次学习算法并尝试用 stratch 来计算。我正在关注 Stratch wiki 上的教程。我怎样才能将其转换为算法?(用流程图或正常步骤)。特别是循环。(我上传为图片)请点击此处查看图片

我开始:

步骤:1 开始
Step2: int: 删除所有数字、迭代器、金额、总和
第三步:你想要多少个数字?
Step4:初始化sum=0,amount=0,iterator=1
步骤5:输入元素值
步骤6:通过使用数组中的循环找到总和并更新总和值,其中循环必须继续直到(元素数-1)次
Step7:avg=总和/元素个数
步骤8:打印平均值

我不认为这是真的。我的意思是我感觉有错误?谢谢您抽出时间。

First time I am learning algorithms and trying to figure out with stratch. I am following tutorials on Stratch wiki. How can I convert this to algorithm?( with flow chart or normal steps). Especially the loop.( I uploaded as picture) Please click here to see picture

I Started:

Step:1 Start
Step2: İnt: delete all of numbers, iterator, amount,sum
Step3: How many numbers you want?
Step4:initialize sum=0,amount=0,iterator=1
Step5: Enter the elements values
Step6: found the sum by using loop in array and update sum value in which loop must be continue till (no of elements-1 ) times
Step7:avg=sum/no of elements
Step8: Print the values average

I don't think It's true. I mean I feel there are errors? Thank you for time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-01-20 10:21:08

Scratch

这里是 Scratch 中变体 2 的算法(参见下面的 Java 算法)。输出应该是相同的。
输入图片这里的描述

Java

这里是 Java 我在哪里评论了步骤它也应该为您提供有关如何在 Scratch 中执行此操作的分步指南。

我还实现了算法的两种变体,向您展示程序员在实现算法时经常需要考虑的一些注意事项,主要是时间(=算法完成所需的时间)和空间(=计算机上使用的内存) 。

请注意:以下算法不处理错误。例如,如果用户输入a而不是数字,程序就会崩溃。调整程序来处理这个问题很容易,但为了简单起见,我没有这样做。

变体 1:将所有元素存储在数组 numbers

此变体将所有数字存储在数组 numbers 中,并在最后使用这些数字计算 sum比变体 2 慢,因为算法将所有数字遍历两次。好处是您将保留用户输入的所有数字,如果需要,您可以稍后使用它,但您将需要存储空间来存储这些值。

public static void yourAlgorithm() {
        // needed in Java to get input from user
        var sc = new Scanner(System.in);
        // print to screen (equivalent to "say"/ "ask")
        System.out.print("How many numbers do you want? ");
        // get amount of numbers as answer from user
        var amount = sc.nextInt();
        // create array to store all elements
        var numbers = new int[amount];
        // set iterator to 1
        int iterator = 1;
        // as long as the iterator is smaller or equal to the number of required numbers, keep asking for new numbers
        // equivalent to "repeat amount" except that retries are possible if no number was entered
        while (iterator <= amount) {
            // ask for a number
            System.out.printf("%d. number: ", iterator);
            // insert the number at position iterator - 1 in the array
            numbers[iterator - 1] = sc.nextInt();
            // increase iterator by one
            iterator++;
        }
        // calulate the sum after all the numbers have been entered by the user
        int sum = 0;
        // go over all numbers again! (this is why it is slower) and calculate the sum
        for (int i = 0; i < amount; i++) {
            sum += numbers[i];
        }
        // print average to screen
        System.out.printf("Average: %s / %s = %s", sum, amount, (double)sum / (double)amount);
    }

变体 2:输入新数字时计算总和

该算法不存储用户输入的数字,而是立即使用输入来计算总和,因此速度更快,因为只需要一个循环,并且由于数字不需要,因此需要更少的内存被存储。
如果您不需要用户稍后输入的所有数字,这将是最佳解决方案(最快、所需空间/内存最少)。

// needed in Java to get input from user
        var sc = new Scanner(System.in);
        // print to screen (equivalent to "say"/ "ask")
        System.out.print("How many numbers do you want? ");
        // get amount of numbers as answer from user
        var amount = sc.nextInt();
        // set iterator to 1
        int iterator = 1;
        int sum = 0;
        // as long as the iterator is smaller or equal to the number of required numbers, keep asking for new numbers
        // equivalent to "repeat amount" except that retries are possible if no number was entered (e.g. character was entered instead)
        while (iterator <= amount) {
            // ask for a number
            System.out.printf("%d. number: ", iterator);
            // get number from user
            var newNumber = sc.nextInt();
            // add the new number to the sum
            sum += newNumber;
            // increase iterator by one
            iterator++;
        }
        // print average to screen
        System.out.printf("Average: %s / %s = %s", sum, amount, (double)sum / (double)amount);

变体 3:结合这两种方法

您还可以结合这两种方法,即在第一个循环内计算总和,并将值另外存储在数字数组中,以便以后需要时可以使用它。

预期输出

在此输入图像描述

Scratch

Here is the algorithm in variant 2 (see Java algorithm below) in Scratch. The output should be identical.
enter image description here

Java

Here is the algorithm in Java where I did comment the steps which should give you a step-by-step guide on how to do it in Scratch as well.

I have also implemented two variants of the algorithm to show you some considerations that a programmer often has to think of when implementing an algorithm which mainly is time (= time required for the algorithm to complete) and space (= memory used on your computer).

Please note: the following algorithms do not handle errors. E.g. if a user would enter a instead of a number the program would crash. It is easy to adjust the program to handle this but for simplicity I did not do that.

Variant 1: Storing all elements in array numbers

This variant stores all numbers in an array numbers and calculates the sum at the end using those numbers which is slower than variant 2 as the algorithm goes over all the numbers twice. The upside is that you will preserve all the numbers the user entered and you could use that later on if you need to but you will need storage to store those values.

public static void yourAlgorithm() {
        // needed in Java to get input from user
        var sc = new Scanner(System.in);
        // print to screen (equivalent to "say"/ "ask")
        System.out.print("How many numbers do you want? ");
        // get amount of numbers as answer from user
        var amount = sc.nextInt();
        // create array to store all elements
        var numbers = new int[amount];
        // set iterator to 1
        int iterator = 1;
        // as long as the iterator is smaller or equal to the number of required numbers, keep asking for new numbers
        // equivalent to "repeat amount" except that retries are possible if no number was entered
        while (iterator <= amount) {
            // ask for a number
            System.out.printf("%d. number: ", iterator);
            // insert the number at position iterator - 1 in the array
            numbers[iterator - 1] = sc.nextInt();
            // increase iterator by one
            iterator++;
        }
        // calulate the sum after all the numbers have been entered by the user
        int sum = 0;
        // go over all numbers again! (this is why it is slower) and calculate the sum
        for (int i = 0; i < amount; i++) {
            sum += numbers[i];
        }
        // print average to screen
        System.out.printf("Average: %s / %s = %s", sum, amount, (double)sum / (double)amount);
    }

Variant 2: Calculating sum when entering new number

This algorithm does not store the numbers the user enters but immediately uses the input to calculate the sum, hence it is faster as only one loop is required and it needs less memory as the numbers do not need to be stored.
This would be the best solution (fastest, least space/ memory needed) in case you do not need all the numbers the user entered later on.

// needed in Java to get input from user
        var sc = new Scanner(System.in);
        // print to screen (equivalent to "say"/ "ask")
        System.out.print("How many numbers do you want? ");
        // get amount of numbers as answer from user
        var amount = sc.nextInt();
        // set iterator to 1
        int iterator = 1;
        int sum = 0;
        // as long as the iterator is smaller or equal to the number of required numbers, keep asking for new numbers
        // equivalent to "repeat amount" except that retries are possible if no number was entered (e.g. character was entered instead)
        while (iterator <= amount) {
            // ask for a number
            System.out.printf("%d. number: ", iterator);
            // get number from user
            var newNumber = sc.nextInt();
            // add the new number to the sum
            sum += newNumber;
            // increase iterator by one
            iterator++;
        }
        // print average to screen
        System.out.printf("Average: %s / %s = %s", sum, amount, (double)sum / (double)amount);

Variant 3: Combining both approaches

You could also combine both approaches, i. e. calculating the sum within the first loop and additionally storing the values in a numbers array so you could use that later on if you need to.

Expected output

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文