VC++编程,求一道数学题目,求大神帮帮忙,满意加分……

2025-05-11 08:16:39
推荐回答(5个)
回答1:

#include "stdafx.h"
#include "stdlib.h"
#include "math.h"
#include "windows.h"
// 矩形层数 矩形变长 最大数
// 1 3 3*3-1=8
// 2 5 5*5-1=24
// 3 7 7*7-1=48
// i 2*i+1 (2*i+1)*(2*i+1)-1

//第i个矩形顶点坐标 (+/-i,+/-i)
//边长 nEdge=2*i+1;
//第i个矩形右下角坐标 (i,-i), 该坐标对应的数是该矩形中的最大数即nEdge*nEdge-1
// 左下角坐标 (-i,-i),该坐标对应的数是即(nEdge*nEdge-1)-(nEdge-1)
// 左上角坐标 (-i,i), 该坐标对应的数是即(nEdge*nEdge-1)-2*(nEdge-1)
// 右上角坐标 (i,i), 该坐标对应的数是即(nEdge*nEdge-1)-3*(nEdge-1)

//将四个顶点的坐标以及其对应的内容作为关键数
//首先确定输入的数位第j层数据 i=ceil((sqrt(m+1)-3)+1); 向上取整
//m<=右上角 则Y坐标=右上角数Y坐标-差值 X坐标不变
// 差值 cha=(i*i-1)-3*(i-1)-m; 坐标(i,i-cha)
//m<=左上角 则X坐标=左上角数X坐标-差值 Y坐标不变
// 差值 cha=(i*i-1)-2*(i-1)-m; 坐标(i+cha,i)
//m<=左下角 则Y坐标=左下角数Y坐标-差值 X坐标不变
// 差值 cha=(i*i-1)-(i-1)-m; 坐标(i,i+cha)
//m<=右下角 则X坐标=右下角数X坐标-差值 Y坐标不变
// 差值 cha=(i*i-1)-m; 坐标(i-cha,i)

void PrintfRect(int nIndex)
{ //可实现打印该图形
int nEdge=2*nIndex+1; //矩形边长
int nElemNum=nEdge*nEdge; //元素总数
}

POINT CalCoordByValue(int nValue)
{
POINT point;
point.x=point.y=0;
//首先确定输入的数位第j层数据 i=ceil((sqrt(m+1)-3)*0.5)+1; 向上取整
int nLayerNum=ceil((sqrt(nValue+1)-3)*0.5)+1;
POINT bottomRight;
bottomRight.x=nLayerNum;
bottomRight.y=-nLayerNum;
POINT bottomLeft;
bottomLeft.x=-nLayerNum;
bottomLeft.y=-nLayerNum;
POINT topLeft;
topLeft.x=-nLayerNum;
topLeft.y=nLayerNum;
POINT topRight;
topRight.x=nLayerNum;
topRight.y=nLayerNum;
int nEdge=2*nLayerNum+1;
int nBottomRight=nEdge*nEdge-1; //边长
int nBottomLeft=nBottomRight-(nEdge-1);
int nTopLeft=nBottomRight-2*(nEdge-1);
int nTopRight=nBottomRight-3*(nEdge-1);
int nOffset=0;
if(nValue<=nTopRight)
{
nOffset=nTopRight-nValue;
point.x=topRight.x;
point.y=topRight.y-nOffset;
}
else if(nValue<=nTopLeft)
{
nOffset=nTopLeft-nValue;
point.x=topLeft.x+nOffset;
point.y=topLeft.y;
}
else if(nValue<=nBottomLeft)
{
nOffset=nBottomLeft-nValue;
point.x=bottomLeft.x;
point.y=bottomLeft.y+nOffset;
}
else if(nValue<=nBottomRight)
{
nOffset=nBottomRight-nValue;
point.x=bottomRight.x-nOffset;
point.y=bottomRight.y;
}
return point;
}

int main()
{
POINT point;
int nValue=0;
printf("请输入需要求坐标的整数:\n");
scanf("%d",&nValue);
point=CalCoordByValue(nValue);
printf("%d的坐标为:(%d,%d)\n",nValue,point.x,point.y);
system("pause");
return 0;
}

回答2:

我有个很傻瓜的方式做这道题目..不知道行不行- -~
首先定义5个数组,分别给每行赋值..
然后..给这每个数字 都定义个坐标数值..
使用if语句来进行筛选,从而得出答案...- -

回答3:

#include
using namespace std;
#define N 1000
struct point
{
int x;
int y;
};
struct point pos[N]={0,0};
void position()
{
int i,k=1,m=1;
while(m {
for(i=0;i {
pos[m].x = pos[m-1].x + 1;
pos[m].y = pos[m-1].y;
m++;
}
for(i=0;i {
pos[m].x = pos[m-1].x;
pos[m].y = pos[m-1].y + 1;
m++;
}
k++;
for(i=0;i {
pos[m].x = pos[m-1].x - 1;
pos[m].y = pos[m-1].y;
m++;
}
for(i=0;i {
pos[m].x = pos[m-1].x;
pos[m].y = pos[m-1].y - 1;
m++;
}
k++;
}
}
void main()
{
int n;
position(); //筛选法,一次求出,多次输出
while(1)
{
cin>>n;
cout< }
}

回答4:

记录一下,有空再想

回答5:

没注释,希望LZ能看懂

#include
using namespace std;

int find(int &x ,int &y , int n)
{
int i = 1 , j = 1 , flag = 0;
while(flag != n)
{
for(j = 1 ; j <= i ; j++)
{
if(flag == n)
return 1;
x++;
flag++;
}

for(j = 1 ; j <= i ; j++)
{
if(flag == n)
return 1;
y++;
flag++;
}
i++;

for(j = 1 ; j <= i ; j++)
{
if(flag == n)
return 1;
x--;
flag++;
}

for(j = 1 ; j <= i ; j++)
{
if(flag == n)
return 1;
y--;
flag++;
}
i++;
}
return 0;

}

int main()
{
int number , x = 0 , y = 0;
cout<<"input a number :";
cin>>number;
find(x , y , number);
cout< return 0;
}