简体   繁体   English

我不知道如何修复我的代码,它不能正常工作

[英]I don't know how to fix my code , it doesn't work properly

I have a question about c++ and a problem which I have to solve.我有一个关于 c++ 的问题和一个我必须解决的问题。 Actually, I wrote a code but the online judge gives me 70 from 100 and says that my code is not working correctly for 3 test cases.实际上,我写了一段代码,但在线评委给了我 100 分中的 70 分,并说我的代码在 3 个测试用例中无法正常工作。 I can't find the bug to correct it, I would appreciate if anyone can help me.我找不到错误来纠正它,如果有人可以帮助我,我将不胜感激。 Thanks谢谢

I want to print the output for every 2 natural numbers a, b which make a/b fraction.我想为每 2 个自然数 a, b 打印输出,它们构成 a/b 分数。 for example for the number (2) it should print 1 1 in the output because we don't consider (0,0) and the second point which we reach in the coordinate on the spiral path is (1,1).例如,对于数字 (2),它应该在输出中打印 1 1,因为我们不考虑 (0,0) 并且我们在螺旋路径上的坐标中到达的第二个点是 (1,1)。 the input should be a natural number and the output should be vertical and horizontal coordinate.输入应该是自然数,输出应该是纵横坐标。

for example, the input is 12 and its output is 2 2. more information is here in these photos:例如,输入是 12,其输出是 2 2。更多信息在这些照片中:

在此处输入图片说明

#include<iostream>

using namespace std;

int main()
{
    int i,j,x,y,count;
    unsigned int n;
    x=0;
    y=0;
    count=0;
    cin>>n;
    for(i=1;i<=30000;i++)
    {
        if(i%2!=0)
        {
            for(j=0;j<i;j++)
            {
                x++;
                count++;
                if(count==n)
                {
                    cout<<x<<" "<<y;
                }
            }
            for(j=0;j<i;j++)
            {
                y++;
                count++;
                if(count==n)
                {
                    cout<<x<<" "<<y;
                }
            }
        }
        else
        if(i%2==0)
        {
            for(j=0;j<i;j++)
            {
                x--;
                count++;
                if(count==n)
                {
                    cout<<x<<" "<<y;
                }
            }
            for(j=0;j<i;j++)
            {
                y--;
                count++;
                if(count==n)
                {
                    cout<<x<<" "<<y;
                }
            }
        }
    }
}

Your algorithm maybe a little bit too complicated.你的算法可能有点太复杂了。 Especially running loops always 30000 times , even while not necessary, is wasting time somehow.特别是运行循环总是 30000 次,即使没有必要,也会以某种方式浪费时间。

And, if you want to request numbers for input > 30000, it will also not work.而且,如果您想请求输入大于 30000 的数字,它也将不起作用。

The loop should be rewritten as: for (i = 1; count <= n; i++) .循环应该重写为: for (i = 1; count <= n; i++) Then is should be better.然后是应该更好。


If you just want to have xycoordinates for a spiral pattern, you may do the following design and then implement it.如果你只是想有一个螺旋图案的xy坐标,你可以做下面的设计然后实现它。 Looking at the picture, we see the follwoing:查看图片,我们看到以下内容:

  • We will have 2 times (for 2 directions) the same distance or length for a line我们将有 2 次(对于 2 个方向)相同距离或长度的线
  • Then, if we work on one line with a given distance or length, we always add the same delta value of wither 1 or -1 to a coordinate.然后,如果我们在具有给定距离或长度的一条线上工作,我们总是将相同的 delta 值添加到一个坐标上,即凋零 1 或 -1。
  • After we have worked one 1 line, we will add delta values to teh other coordinate.在我们完成 1 行之后,我们将向其他坐标添加增量值。 If it was x before than it will be y now and vice versa如果之前是 x,那么现在是 y,反之亦然
  • To do that, we can store the coordinates in an array with length 2. Index 0 is for x and index 1 is for y.为此,我们可以将坐标存储在长度为 2 的数组中。索引 0 表示 x,索引 1 表示 y。
  • So, if we want to address either x or y, we can use a selector, a index, that will be either 0 or 1.所以,如果我们想寻址 x 或 y,我们可以使用一个选择器,一个索引,它要么是 0,要么是 1。
  • We can toggle this selector after one distance or one line has been done我们可以在完成一段距离或一条线后切换此选择器
  • After we have worked on 2 distances or 2 lines,we needto change the delta values, becuase from then on coordinated need to be decremented now.在我们处理了 2 个距离或 2 条线之后,我们需要更改 delta 值,因为从那时起协调现在需要递减。 The same for vice versa.反之亦然。
  • Additionally, after 2 distances or lines have been done, we will increase the distance or length to be used next time by 1.此外,在完成 2 个距离或线后,我们将下次使用的距离或长度增加 1。

So, look at the pricture and implement accordingly.因此,请查看价格并相应地实施。

Please see one of many solution proposals:请参阅众多解决方案建议之一:

#include <iostream>

int main() {

    // Get user input. How many steps shall we go in our spiral
    unsigned int numberOfSteps{};
    std::cin >> numberOfSteps;

    int coordinate[2]{};            // Here we will store the xy coordinate. Index 0 = x, index 1 = y
    int xySelector{};               // This will select either index 0 or 1 , so, either x or y

    int delta{ 1 };                 // This will be added to a coordinate part, to get the next point of the line

    int distance{ 1 };              // Length of line. Will be used 2 times, then incremented
    int distanceCounter{ 1 };       // Down counter for distance
    int doSameDistanceCounter{ 2 }; // We will do always 2 lines with the same distance/length

    // Do the game for the requested number of steps
    for (int i{}; i < numberOfSteps; ++i) {

        coordinate[xySelector] += delta;    // Calculate next point of line

        --distanceCounter;                  // Keep track of length of line
        if (distanceCounter == 0) {         // Did we calculate one complete line?
            xySelector = 1-xySelector;      // Yes, then now use the other coordinate 
            --doSameDistanceCounter;        // Keep tract of number of same line length to use
            if (doSameDistanceCounter == 0) {   // If we did 2 lines with the same length
                doSameDistanceCounter = 2;      // Reset this counter
                ++distance;                     // Frome now one the length of line will be 1 more
                delta = -delta;                 // And the delta changes, fo increment or decrement operation changes
            }
            distanceCounter = distance;     // Reset distance/length counter to current needed distance
        }
    } // Show result
    std::cout << coordinate[0] << ' ' << coordinate[1] << '\n';
}

I tested it in release mode with 10M and the result was ultra fast.我在发布模式下用 10M 对其进行了测试,结果非常快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM