| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 48911 人关注过本帖, 10 人收藏
标题:[全民编程]76道高难度C++练习题.含NOI竞赛题.欢迎挑战
只看楼主 加入收藏
zkkpkk
Rank: 2
等 级:论坛游民
威 望:5
帖 子:489
专家分:28
注 册:2006-6-17
收藏
得分:0 
回复:(野比)[全民编程]76道高难度C++练习题.含NOI竞...

程序代码:

/*
3. 打印一个 N*N 的方阵,N为每边
字符的个数(3<N<20), 要求最
外一层为\"T\", 第二层为\"J\", 从第三层
起每层依次打印数字 1,2,3,...
*/
#include <iostream.h>
#define MAX 22

int main()
{
int i=0,j=0,m=1,n=3;
char charr[MAX][MAX];
char ch1='T';
char ch2='J';
//char tmp[2];

for(i=0;i<MAX-1;i++)
for(j=0;j<MAX-1;j++)
charr[i][j]='a';

cout<<\"输入N的值:\";
cin>>n;
int a=0,b=n-1,c=n-1,d=0;
bool bo=false;

while(m<=(n-4)/2+1)
{
for(i=0;i<n;i++)
{
if(a==0)
{
if(charr[a][i]=='a')
charr[a][i]=ch1;
continue;
}
if(a==1)
{
if(charr[a][i]=='a')
charr[a][i]=ch2;
continue;
}
if(charr[a][i]=='a')
charr[a][i]=(char)m+(char)48;
}

for(i=0;i<n;i++)
{
if(b==n-1)
{
if(charr[i][b]=='a')
charr[i][b]=ch1;
continue;
}
if(b==n-2)
{
if(charr[i][b]=='a')
charr[i][b]=ch2;
continue;
}
if(charr[i][b]=='a')
charr[i][b]=(char)m+(char)48;
}

for(i=n-1;i>=0;i--)
{
if(c==n-1)
{
if(charr[c][i]=='a')
charr[c][i]=ch1;
continue;
}
if(c==n-2)
{
if(charr[c][i]=='a')
charr[c][i]=ch2;
continue;
}
if(charr[c][i]=='a')
charr[c][i]=(char)m+(char)48;
}

for(i=n-1;i>=0;i--)
{
if(d==0)
{
if(charr[i][d]=='a')
charr[i][d]=ch1;
continue;
}
if(d==1)
{
if(charr[i][d]=='a')
charr[i][d]=ch2;
continue;
}
if(charr[i][d]=='a')
charr[i][d]=(char)m+(char)48;
bo=true;
}

a++;d++;c--;b--;
if(bo==true)
m++;
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<charr[i][j]<<' ';
cout<<endl;
}
return 0;
}

[此贴子已经被作者于2007-6-27 21:38:23编辑过]


Viva,espana!
2007-06-26 13:36
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 

第12题:不知道是不是这个意思.
打印出来的0,1,2,3分别表示:
0——向左
1——向右
2——向下
3——向上
不知道如何更高效率

程序代码:
class Coordinate 
{
//注意:坐标代表的数字与2维数组相反。
int abscissa; //横坐标
int ordinate; //纵坐标
int nWidth,nHeight;
int dir;
public:
Coordinate(int a=-1,int b=-1,int w=0,int h=0)
{
dir=-1;
abscissa=a;
ordinate=b;
nWidth=w;
nHeight=h;
}
bool ToLeft()
{
if(abscissa==0)
return false;
abscissa--;
dir=0;
return true;
}
bool ToRight()
{
if(abscissa==nWidth)
return false;
abscissa++;
dir=1;
return true;
}
bool ToDown()
{
if(ordinate==nHeight)
return false;
ordinate++;
dir=2;
return true;
}
bool ToUp()
{
if(ordinate==0)
return false;
ordinate--;
dir=3;
return true;
}
bool DirOperate(int i)
{
if(abs(dir-i)==1 && (dir+i)*2!=6)
return false;
switch(i)
{
case 0:return ToLeft();
case 1:return ToRight();
case 2:return ToDown();
case 3:return ToUp();
default:return false;
}
}
bool operator==(Coordinate cd)
{
return abscissa==cd.abscissa&&ordinate==cd.ordinate;
}
void SetRange(int w,int h)
{
nWidth=w;
nHeight=h;
}
int GetAbscissa()
{
return abscissa;
}
int GetOrdinate()
{
return ordinate;
}
};
#include <deque>
class Maze
{
Coordinate inner; //入口
Coordinate outer; //出口
int ** ppMap;
int nWidth,nHeight;
private:
bool MazeLine(Coordinate in,deque<int>&s)
{
Coordinate temp=in;
for(int i=0;i<4;i++)
{
in=temp;
if(in.DirOperate(i))
{
if(ppMap[in.GetOrdinate()][in.GetAbscissa()]==1)
{
if(in==outer||MazeLine(in,s))
{
s.push_front(i);
return true;
}
}
}
}
return false;
}
public:
Maze(Coordinate in,Coordinate out,int **map,int width,int height)
{
in.SetRange(width,height);
ppMap=new int*[height];
for(int i=0;i<height;i++)
ppMap[i]=new int[width];
for (int m=0;m<height;m++)
for (int n=0;n<width;n++)
ppMap[m][n]=map[m][n];
inner=in;
outer=out;
}
bool MazeLine(deque<int>&dq)
{
return MazeLine(inner,dq);
}
};

int arr[][10]=
{
{0,1,0,0,0,0,0,0,0,0},
{0,1,0,1,1,1,1,0,1,0},
{0,1,1,1,0,0,1,0,1,0},
{0,1,0,0,0,0,1,1,1,0},
{0,1,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,1,0,1,0,0},
{0,1,0,0,0,1,0,1,0,0},
{0,1,0,1,1,1,0,1,1,0},
{0,1,0,0,0,0,0,0,1,0}
};


void main()
{
int **maze=new int*[10];
for(int m=0;m<10;m++)
maze[m]=new int[10];

for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
maze[i][j]=arr[i][j];
Maze mz(Coordinate(1,0),Coordinate(8,9),maze,10,10);
deque<int>dq;
if(mz.MazeLine(dq))
cout<<\"\nsuccess\"<<endl;
else
cout<<\"\nfail\"<<endl;
copy(dq.begin(),dq.end(),ostream_iterator<int>(cout));
}


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-06-26 16:06
smartwind
Rank: 1
等 级:新手上路
威 望:1
帖 子:277
专家分:0
注 册:2006-11-13
收藏
得分:0 

75题:

程序代码:

#include <iostream>
using namespace std;

#define N 3

const int a[N]={31,15,7};
//{20,19,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
//{200,179,157,140,139,110,103,101,97,90,80,64,51,45,36,25,14,10,9,6};
int b[N];

bool fun(int n,int i)
{
if(n==0)
return true;
if(i>=N)
return false;
if(n<a[i])
return fun(n,i+1);
if(fun(n-a[i],i))
{
b[i]++;
return true;
}
else
return fun(n,i+1);
}

int main()
{
int n,i,k;
do
{
k=0;
cin>>n;
if(n>10000||n<0) //限制0<n<10000
goto CON;
if(fun(n,0)) //有解则输出结果
{
for(i=0;i<N;i++)
k+=b[i];
cout<<k<<endl;
for(i=0;i<N;i++)
{
if(b[i]>0)
{
cout<<a[i]<<\"*\"<<b[i]<<\" \";
b[i]=0;
}
}
cout<<endl;
}
else
CON: cout<<\"no answer\"<<endl;
}
while(n!=0); //输入0结束
return 0;
}


2007-06-27 10:47
weishj
Rank: 1
等 级:新手上路
威 望:2
帖 子:141
专家分:0
注 册:2007-4-22
收藏
得分:0 
第37题,不知题意理解的对不对
/*file name:37.cpp
37. 已知 N 个正整数满足 K1+K2+...+Kn=M。求一组最佳的分解,使得
K1*K2*....*Kn 为最大。
例如:N=2时,给定 K1+K2=6,当 K1=3,K2=3 时,K1*K2=9 为最大*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int N=0,M=0,sum=0,diff=0,product=1;
int* arr=NULL;
float ave=0.0;
cout<<"输入数字个数N:"<<endl;
cin>>N;
cout<<"输入这些数字的和M:"<<endl;
cin>>M;
arr=new int[N];
ave=float(M)/float(N);
sum=int(ave)*N;
diff=M-sum;//diff肯定小于N
for(int i=0;i<N;i++)
{
if(i<diff) arr[i]=1+int(ave);
else arr[i]=int(ave);
}
cout<<"一个最佳分解是:"<<endl;
for(int j=0;j<N;j++)
{
cout<<setw(5)<<arr[j];
product*=arr[j];
}
cout<<endl<<"最大乘积是:"<<product<<endl;
delete []arr;
return 0;
}

If you shed tears when you miss the sun, you also miss the stars.
2007-06-27 11:16
zkkpkk
Rank: 2
等 级:论坛游民
威 望:5
帖 子:489
专家分:28
注 册:2006-6-17
收藏
得分:0 

41题链表合并

程序代码:

/*
41. (合并链表) 已知两个链
表 AN={a1,a2,...an},
BN={b1,b2,...bm},
将其合并为一个链表
CN={a1,b1,a2,b2,...}
*/

#include <iostream.h>

/*节点结构体Node*/
struct Node
{
int data;
Node* next;
};
/*结构体头指针*/
Node* head1 = new Node;
Node* head2 = new Node;
Node* head3 = new Node;

/*添加一个新节点*/
void Push(Node* head,int data)
{
Node* node = new Node;
node->data = data;
node->next = head->next;
head->next = node;
}
/*输出链表*/
void Display(Node* head)
{
Node* temp=head->next;
while(temp!=NULL)
{
cout<<temp->data<<'\t';
temp=temp->next;
}
cout<<endl;
}
/*初始化一条链表*/
Node* Info(Node* head,int a)
{
for(int i=0;i<a;i++)
Push(head,i);
return head;
}
Node* Info_Input(Node* head,int a)
{
cout<<\"请输入数据:\"<<endl;
for(int i=0;i<a;i++)
{
int value;
cout<<\"第\"<<i+1<<\"个元素\"<<endl;
cin>>value;
Push(head,value);
}
return head;
}
/*链表归并算法*/
Node* UniteLink(Node* head1,Node* head2,Node* head3)
{
Node *p1=NULL,*p2=NULL;
Node* temp1=head1->next;
Node* temp2=head2->next;
Node* temp3=head3;
temp3->next=temp1;
while((temp1!=NULL) && (temp2!=NULL))
{
p1=temp1;
temp1=temp1->next;
p1->next=temp2;
p2=temp2;
temp2=temp2->next;
p2->next=temp1;
if((temp1==NULL) && (temp2!=NULL))
{
p2->next=temp2;
break;
}
else if((temp1!=NULL) && (temp2==NULL))
{
p2->next=temp1;
break;
}
}
return temp3;
}

int main()
{
head1->next=NULL;
head2->next=NULL;
head3->next=NULL;
int a;
cout<<\"请输入链表A的数据长度:\";
cin>>a;
Info_Input(head1,a);
cout<<\"构造链表A:\"<<'\t';
Display(head1);
cout<<\"请输入链表B的数据长度:\";
cin>>a;
Info_Input(head2,a);
cout<<\"构造链表B:\"<<'\t';
Display(head2);
UniteLink(head1,head2,head3);
cout<<\"合并后的链表CN:\"<<'\t';
Display(head3);
return 0;
}


Viva,espana!
2007-06-27 16:24
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
To 野比:

If you have time, you may want to do some work to

1) give people credits
2) let people know which problem is solved.

Thanks,

HJin

I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-27 17:50
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
wfpb,

你在107楼关于第四题的代码是不正确的,看来你没有完全理解题意。
题目的本意是要你给出所有的解,而不是其中的某一种。

这道题是很难的,并不是什么太简单了,大家都不做了。


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2007-06-27 20:03
zkkpkk
Rank: 2
等 级:论坛游民
威 望:5
帖 子:489
专家分:28
注 册:2006-6-17
收藏
得分:0 

程序代码:

/*回旋填数*/
#include <stdio.h>
#include <iostream.h>

int array[11][11];
int temp;
int ROW;

void godown(int &m,int &a)
{
for(temp=1;temp<=ROW;temp++)
if(array[temp][a]==0)
array[temp][a]=m++;
a++;
}
void goright(int &m,int &b)
{
for(temp=1;temp<=ROW;temp++)
if(array[b ][temp]==0)
array[b ][temp]=m++;
b--;
}
void goup(int &m,int &c)
{
for(temp=ROW;temp>0;temp--)
if(array[temp][c]==0)
array[temp][c]=m++;
c--;
}
void goleft(int &m,int &d)
{
for(temp=ROW;temp>0;temp--)
if(array[d][temp]==0)
array[d][temp]=m++;
d++;
}

int main()
{
int a,b,c,d,max,m;
cout<<\"请输入缧旋方阵的维数n(不能大于10):\";
cin>>ROW;
cout<<endl;
for(a=1;a<=ROW;a++)
for(b=1;b<=ROW;b++)
array[a][b ]=0;
m=1;
a=d=1;
b=c=ROW;
max=ROW*ROW;

while(m<=max)
{
godown(m,a);
goright(m,b);
goup(m,c);
goleft(m,d);
}
for(a=1;a<=ROW;a++)
{
for(b=1;b<=ROW;b++)
printf(\"%3d \",array[a][b ]);
cout<<endl;
}
return 0;
}



程序代码:

/*倒填*/
#include <iostream.h>
#define MAX 5

int main()
{
int i=0, j=0;
int array[MAX][MAX];
int t=MAX*MAX;

for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
array[i][j]=t--;

for(i=0;i<MAX;i++)
{
for(j=0;j<MAX;j++)
cout<<array[i][j]<<'\t';
cout<<endl;
}

return 0;
}

[此贴子已经被作者于2007-6-29 11:43:55编辑过]


Viva,espana!
2007-06-27 21:39
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 

To HJin,

I'm very sorry to tell you that I do not have much time recently...
My boss got me plenty of works to do...

You can edit my posts by yourself, since you are now the moderator of the cpp classroom...
I don't have as much time as you do...

Thanks,
Nobi


女侠,约吗?
2007-06-27 22:08
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
你也是斑竹,他也是斑竹,所以不得行。

还是你自己能来,等有空了再搞吧,呵呵~工作要紧!

Fight  to win  or  die...
2007-06-27 22:47
快速回复:[全民编程]76道高难度C++练习题.含NOI竞赛题.欢迎挑战
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017062 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved