找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 933|回复: 9

数组的一个问题

[复制链接]
发表于 2009-11-2 01:23:14 | 显示全部楼层 |阅读模式 IP:江苏扬州
一个数组有n个值,如n=3;数组数为{1,2,3,4,5,6,7,8,9}
输出为:1 2 3
  8 9 4
  7 6 5
n=4时 数组为{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
输出为:1  2  3  4
  12 13 14 5
  11 16 15 6
  10  9  8 7
发表于 2009-11-2 01:23:19 | 显示全部楼层 IP:江苏扬州
不知所云不知道你到底说的什么意思

#include "stdio.h"
#define n 3
void main () {
int iArr[n][n] = {{1,2,3},{8,9,4},{7,6,5}};
printf("%d %d %d\n",iArr[0][0],iArr[0][1],iArr[0][2]);
printf("%d %d %d\n",iArr[1][0],iArr[1][1],iArr[1][2]);
printf("%d %d %d\n",iArr[2][0],iArr[2][1],iArr[2][2]);
}
回复

使用道具 举报

发表于 2009-11-2 01:23:21 | 显示全部楼层 IP:江苏扬州
呵呵,有点意思,你等等
回复

使用道具 举报

发表于 2009-11-2 01:23:24 | 显示全部楼层 IP:江苏扬州
#include<iostream>
#include<iomanip>
#include<vector>
using namespace std;
int main(){
for(int n; cin>>n; cout<<endl){
vector<vector<int> > board(n,vector<int>(n,0));
for(int i=1,di=0,x=0,y=-1; i<=n*n; i++){
di%=4;
switch(di){
case 0:y++;break;
case 1:x++;break;
case 2:y--;break;
case 3:x--;break;
}
if(x==n || x<0 || y==n || y<0 || board[x][y]){
switch(di){
case 0:y--;break;
case 1:x--;break;
case 2:y++;break;
case 3:x++;break;
}
di++;
i--;
}
else
board[x][y]=i;
}
for(int i=0; i<n ;i++){
if(i) cout<<endl;
for(int j=0; j<n; j++)
cout<<setw(4)<<board[i][j];
}
}
}
回复

使用道具 举报

发表于 2009-11-2 01:23:28 | 显示全部楼层 IP:江苏扬州
用一维数组的话

#include <iostream>
#include<iomanip>
using namespace std;
int main(){
cout<<"input n: ";
int n;
while(!(cin>>n))
{
cout<<"input error\n";
cin.clear();
while(cin.get()!='\n'){}
cout<<"input n : ";
}
int *p=new int[n];
double half=(n-1)/2;
int circle=0;
for(int line=0; line<n; line++)
{
for(int list=0; list<n; list++)
{
int tmp1=(line > half ? n-line-1: line);
int tmp2=(list > half ? n-list-1: list);
circle=(tmp1 < tmp2 ? tmp1 : tmp2);

int circler_total=0;
int tempN=n;

if(list >= line)
{
for(int i=0; i<circle; i++)
{
circler_total+=4*(tempN-1);
tempN-=2;
}
p
=circler_total+(list-circle+1)+(line-circle);
}
else
{
for(int i=0; i<(circle+1); i++)
{
circler_total+=4*(tempN-1);
tempN-=2;
}
p
=circler_total-(list-circle)-(line-circle-1);
}
}
for(int i=0; i<n; i++)
cout<<setw(5)<<p[i];
cout<<"\n\n";
}
delete[]p;
system("pause");
}
回复

使用道具 举报

发表于 2009-11-2 01:23:32 | 显示全部楼层 IP:江苏扬州
/*
把矩阵分成几个框
1 2 3 4 1 2 3 4
12 13 14 5 => 12 5 和 13 14
11 16 15 6 11 6 16 15
10 9 8 7 10 9 8 7
方法可能比较笨,但是应该还算直观...
*/
#include <iostream.h>
int CalcStartVal( int startPos, int n ) //计算每个框的初始值(左上角)
{
int startVal = 1;
for ( int i = 0; i < startPos; i++ ){
startVal += ( n + ( n - 2 ) + n + ( n - 2 ) );
n -= 2;
}
return startVal;
}
int CalcStartPosNum( int n ) //计算框数
{
return n / 2 + n % 2;
}
//填充框的上边
void FillTop( int startPos, int startVal, int **theFrame, int n )
{
int end = n - 2 * startPos;
for ( int i =0; i < end; i++ )
theFrame[ startPos ][ startPos + i ] = startVal + i;
}
//填充框的右边
void Fillright( int startPos, int startVal, int **theFrame, int n )
{
int end = n - 2 * startPos - 2;
for ( int i = 0; i < end; i++ )
theFrame[ startPos + i + 1 ][ startPos + end + 1 ]
= startVal + ( end + 2 ) + i;
}
//填充框的下边
void FillBottom( int startPos, int startVal, int **theFrame, int n )
{
int end = n - 2 * startPos;
for ( int i = 0 ; i < end; i++ ){
theFrame[ startPos + end - 1 ][ startPos + end -1 - i ]
= startVal + 2 * end - 2 + i;
}
}
//填充框的左边
void FillLeft( int startPos, int startVal, int **theFrame, int n )
{
int end = n - 2 * startPos - 2;
for ( int i = 0; i < end; i++ )
theFrame[ startPos + end - i ][ startPos ]
= startVal + 3 * ( end + 2 ) -2 + i;
}
//填充一个框
void FillFrame( int startPos, int n, int **theFrame )
{
int startVal = CalcStartVal( startPos, n );
FillTop( startPos, startVal, theFrame, n );
Fillright( startPos, startVal, theFrame, n );
FillBottom( startPos, startVal, theFrame, n );
FillLeft( startPos, startVal, theFrame,n );
}
void main()
{
int n;
cout << "输入N值:" << endl;
cin >> n;
int **matrix = new int*[n];
for ( int i2 = 0; i2 < n; i2++ )
matrix[i2] =new int[n]; //根据输入分配矩阵存储
for ( int i1 = 0; i1 < CalcStartPosNum( n ); i1++ )
FillFrame( i1, n, matrix ); //填充数值
cout << endl ; //显示结果
for ( int i = 0; i < n; i++ ){
for ( int j = 0; j < n; j++ ){
cout << matrix[ i ][ j ];
if ( i == 0 )
cout << " ";
else
cout << " ";
}
cout << endl << endl;
}
for ( i2 = 0; i2 < n; i2++ )
delete [] matrix[i2];
delete [] matrix; //删除指针
}
//O(n^2);没有作输入检查...
回复

使用道具 举报

发表于 2009-11-2 01:23:34 | 显示全部楼层 IP:江苏扬州
问一下楼上的,10怎么办?
回复

使用道具 举报

发表于 2009-11-2 01:23:36 | 显示全部楼层 IP:江苏扬州
我就是按一圈一圈的思路做的,每一圈的个数是 (边长-1)*4个,每圈从左上角开始

先找出数所在的圈的层数,再根据它在圈中的位置,具体算出应该赋给的值,

这些都可以用所在的行列数得出.
回复

使用道具 举报

发表于 2009-11-2 01:23:39 | 显示全部楼层 IP:江苏扬州
我记得我好象写过一个,找了找,果然在老blog上有

#include <stdio.h>
#include <conio.h>
int a,b,c,d,e,f,t;main(_){!(_-1)&&(d=(e=(f=(c=b=((a=(t=1)+t)*a+t))*a)*a)-f-c+t);_<37&&(t==1?(a+=3,a==e&&(e-=3,t=2)):t==2?(b++,b==f&&(f--,t=3)):t==3?(a-=3,a==c&&(c+=3,t=0)):t==0?(b--,b==d&&(d++,t=1)):0,gotoxy(a,b),printf("%3d",_),_++)&&main(_);}
回复

使用道具 举报

发表于 2009-11-2 01:23:42 | 显示全部楼层 IP:江苏扬州
你写程序总是这种风格?

看不懂
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 21:28 , Processed in 0.198484 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表