坚持螺旋三角形(Stuck with spiral triangle)

编程入门 行业动态 更新时间:2024-10-22 18:44:43
坚持螺旋三角形(Stuck with spiral triangle)

我正在寻找一种算法,它编号并输出一个边长为n (或正方形的一半)的三角形,其中n是程序的输入。 但是编号从三角形的顶部开始,沿着对角线向下,沿着底行向上,向上沿着左边缘。 如果剩余内部,则从最高数字斜向下并继续。

这是一个例子:

1 9 2 8 10 3 7 6 5 4

这是我的代码,它的结果是:

1 10 2 9 8 3 7 6 5 4

有没有任何算法的程序,如果有任何请向我解释。 上述程序适用于小于3行大小,但不适用于大于3大小。

#include<iostream.h> #include<conio.h> void main() { int n,i,j,v=0; static int k; clrscr(); cout<<"Enter the number of rows : "; cin>>n; for(i=0;i<n;i++) { for(j=0;j<=i;j++) { v++; } } for(i=0;i<n;i++) { for(j=0;j<i;j++) { cout<<v; cout<<"\t"; v--; } while(k==i) { k++; cout<<k; cout<<"\t"; } cout<<"\n"; } getch(); }

I am looking for an algorithm that numbers and outputs a triangle with sides n (or half of a square) where n is an input of the program. But the numbering starts at the top of the triangle, goes down the diagonal, back along the bottom row and up the left edge. If there is an interior remaining it goes diagonally down from the highest number and continues.

Here is an example:

1 9 2 8 10 3 7 6 5 4

Here is my code and it results in:

1 10 2 9 8 3 7 6 5 4

Is there any algorithm for this of program if there any please explain to me. The above program works well with row size less than 3 but doesn't for size above 3.

#include<iostream.h> #include<conio.h> void main() { int n,i,j,v=0; static int k; clrscr(); cout<<"Enter the number of rows : "; cin>>n; for(i=0;i<n;i++) { for(j=0;j<=i;j++) { v++; } } for(i=0;i<n;i++) { for(j=0;j<i;j++) { cout<<v; cout<<"\t"; v--; } while(k==i) { k++; cout<<k; cout<<"\t"; } cout<<"\n"; } getch(); }

最满意答案

这是一个不使用任何阵列存储的解决方案。 螺旋可以被认为是彼此内部的一组直角三角形。 该函数迭代所有行和列,并且对于每个位置,它通过找到到外三角形边缘的最近距离来计算元素所在的三角形,然后计算其相对于顶部的调整位置(x,y) - 该内三角形的左角,内三角形的行数(r)和内三角形的起始编号(开始+ 1)。 然后它根据它是否位于对角线,水平或垂直方向输出一个数字。

#include <iostream> #include <iomanip> using namespace std; int main(void) { int rows; cout << "Enter the number of rows : "; cin >> rows; int i, j; for(i = 0; i < rows; i++) { for(j = 0; j <= i; j++) { // find the closest side: int distance = j; // distance to vertical side if(i-j < distance) distance = i-j; // distance to diagonal side if((rows-1)-i < distance) distance = (rows-1)-i; // distance to horizontal side int r = rows - distance * 3; // compute position on inner triangle: int x = j - distance; int y = i - distance * 2; // compute start number for inner triangle: int start = (((rows+1)*rows)/2) - (((r+1)*r)/2); // output number based on side: if(x==y) // diagonal side cout << setw(2) << (start+y+1) << " "; else if(y==(r-1)) // horizontal side cout << setw(2) << (start+(r*2)-(x+1)) << " "; else // vertical side cout << setw(2) << (start+(r*3)-(y+2)) << " "; } cout << endl; } return 0; }

演示

以行等于7为例。在这种情况下,每个元素的distance值将为:

(0) 0 0 0 (1) 0 0 1 1 0 0 1 (2) 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0

所有具有相同距离值的元素形成一个三角形。 外三角形的行数是7,下一个较小的行数是4,然后是1.所以r = rows - (距离* 3)。

外三角形的左上角位于第0行第0列。第一个内部三角形是第2行,第1列,下一个在第4行,第2列。因此给定行/列位置的位置它所在的内三角是通过从行中减去距离* 2和从列中减去距离得到的,因此y = i - (距离* 2)和x = j - 距离。

内三角形列存储在x中。 内三角形行存储在y中。 在上面的示例中,括号中的值是每个三角形的左上角,其中x = 0且y = 0.例如,对于距离= 1的三​​角形的左上角,i = 2且j = 1,所以x = 1 - 1 = 0,y = 2 - (1 * 2)= 0。

通过计算整个大三角形((行+ 1)*行)/ 2中的元素数量然后减去剩余元素的数量(内部三角形中的元素数量)来找到起始值。

对于具有n行的三角形,元素总数为((n + 1)* n)/ 2,如下所示,对于行= 5:

1 X 0 0 0 0 0 2 X X 0 0 0 0 3 X X X 0 0 0 4 X X X X 0 0 5 X X X X X 0 1 2 3 4 5 6

为了计算X的数量,我们可以看到它是(5 + 1)* 5矩形中元素数量的一半,所以30的一半是15。

如果在另一个内部有两个三角形,如下所示:

X X X X O X X O O X X X X X X

并且我们想要计算X的数量,然后我们可以使用上面的公式计算整个三角形的大小得到15,然后计算内部三角形的大小,其中有2行为((2 + 1)* 2 )/ 2 = 3,并从较大的减去15-3 = 12.所以如果有12个X,那么第一个O必须是数字13.这就是我们如何计算左上角输出的数字内三角的一角。

一旦你计算了所有这一切,只需要弄清楚元素所在的内三角形的哪一面并将其打印出来。

Here's a solution that doesn't use any array storage. The spiral can be thought of as a set of right angled triangles inside each other. The function iterates over all the rows and columns and for each position, it calculates what triangle the element is on by finding the closest distance to the edge of the outer triangle, then computes its adjusted position (x, y) relative to the top-left corner of that inner triangle, the number of rows of the inner triangle (r), and the start number of the inner triangle (start+1). Then it outputs a number based on whether it lies on the diagonal, horizontal or vertical side.

#include <iostream> #include <iomanip> using namespace std; int main(void) { int rows; cout << "Enter the number of rows : "; cin >> rows; int i, j; for(i = 0; i < rows; i++) { for(j = 0; j <= i; j++) { // find the closest side: int distance = j; // distance to vertical side if(i-j < distance) distance = i-j; // distance to diagonal side if((rows-1)-i < distance) distance = (rows-1)-i; // distance to horizontal side int r = rows - distance * 3; // compute position on inner triangle: int x = j - distance; int y = i - distance * 2; // compute start number for inner triangle: int start = (((rows+1)*rows)/2) - (((r+1)*r)/2); // output number based on side: if(x==y) // diagonal side cout << setw(2) << (start+y+1) << " "; else if(y==(r-1)) // horizontal side cout << setw(2) << (start+(r*2)-(x+1)) << " "; else // vertical side cout << setw(2) << (start+(r*3)-(y+2)) << " "; } cout << endl; } return 0; }

Demo

Take the example where rows equals 7. In that case the distance value for each element will be:

(0) 0 0 0 (1) 0 0 1 1 0 0 1 (2) 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0

All elements with the same distance value form a triangle. The number of rows of the outer triangle is 7, the next smaller one has 4, then 1. So r = rows - (distance * 3).

The top-left corner of the outer triangle is at row 0, column 0. The first inner triangle is row 2, column 1, the next one is at row 4, column 2. So the position of a given row/column position on the inner triangle that it lies on is found by subtracting distance * 2 from the row and distance from the column, so y = i - (distance *2) and x = j - distance.

The inner triangle column is stored in x. The inner triangle row is stored in y. In the example above, the values in brackets are the top-left corners of each triangle, where x = 0 and y = 0. For example for the top-left corner of the triangle with distance = 1, i = 2 and j = 1, so x = 1 - 1 = 0, and y = 2 - (1 * 2) = 0.

The start value is found by calculating the number of elements in the entire large triangle ((row+1)*row)/2 and then subtracting the number of elements remaining, which is the number of elements in the inner triangle.

For a triangle with n rows, the total number of elements is ((n+1)*n)/2, as shown below for rows = 5:

1 X 0 0 0 0 0 2 X X 0 0 0 0 3 X X X 0 0 0 4 X X X X 0 0 5 X X X X X 0 1 2 3 4 5 6

To count the number of X's, we can see that it's half the number of elements in a rectangle of (5+1)*5, so half of 30, which is 15.

If there are 2 triangles one inside the other, like this:

X X X X O X X O O X X X X X X

and we want to count the number of X's, then we can calculate the size of the entire triangle using the above formula to get 15, and then calculate the size of the inner triangle which has 2 rows as ((2+1)*2)/2 = 3, and subtracting the smaller from the larger gives 15 - 3 = 12. So if there are 12 X's, then the first O must be number 13. That's how we can calculate the number to output for the top-left corner of the inner triangle.

Once you've calculated all that it's just a matter of working out which side of the inner triangle the element is on and printing it out.

更多推荐

本文发布于:2023-07-14 20:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:角形   螺旋   Stuck   spiral   triangle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!