| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付买域名,送MP3、MP4
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY买空间,免费送域名(厦门中资源)
共有 32031 人关注过本帖
标题:[全民编程]76道高难度C++练习题.含NOI竞赛题.欢迎挑战
收藏  订阅  推荐  打印 
aipb2007
Rank: 12Rank: 12Rank: 12
来自:CQU
等级:贵宾
威望:40
帖子:2881
积分:29414
注册:2007-3-18


也有事,空了研究。
UP

Fight  to win  or  die...
2007-6-25 15:53
laigaoat2005
Rank: 3Rank: 3
等级:中级会员
帖子:283
积分:2984
注册:2007-4-5

// 10.cpp author:laigaoat2005 data:2007.6.25
#include "iostream.h"
int main()
{
int input=0,count=0;
int i=0;
int ex=1;
while(ex)
{
cout<<"请输入每排正方形的个数:";
cin>>input;
for(i=input;i>0;i--)
{
count+=i*i;
}
cout<<"\n正方形个数是:"<<count<<"\n";
input=0;
cout <<"请输入小正方形的边长:";
cin>>input;
count=input*input*count;
cout<<"大正方形的面积是:"<<count<<"\n请选择你的操作:输入0退出,输入其它数值继续";
cin>>ex;
}
return 0;
}

没有签名
2007-6-25 15:55
HJin
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:27
帖子:401
积分:4134
注册:2007-6-9

/*---------------------------------------------------------------------------
File name: C76-37.cpp
Author: HJin
Created on: 6/25/2007 01:12:43
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762


Modification history:


经典 76 道编程题 之 37:

37. 已知 N 个正整数满足 K1+K2+...+Kn=M。求一组最佳的分解,使得
K1*K2*....*Kn 为最大。
例如:N=2时,给定 K1+K2=6,当 K1=3,K2=3 时,K1*K2=9 为最大


Analysis:

Do your math to establish the following recurrence formula

f(M, N) = M/N * f(M-M/N, N-1), if N>1
= M, if N=1.

My implementation is just a restating of this formula
in C++ language. You may want to develop an iterative soln.


Sample output:

M = 60, N = 17
max product 1719926784 = 3 * 3 * 3 * 3 * 3 * 3 * 3 * 3 * 4 * 4 * 4 * 4 * 4 * 4 *
4 * 4 * 4.
Press any key to continue . . .


Reference:

1. 野比, 相当经典的76道编程自虐题分享.无答案 at
http://bbs.bc-cn.net/viewthread.php?tid=147853

*/

#include <iostream>
#include <stdexcept>

using namespace std;


long f(int M, int N, int *k);


int main(int argc, char** argv)
{
int i;
int M=60;
int N=17;
int prod;
int* k = new int[N];

prod = f(M, N, k);

cout<<"M = "<<M<<", N = "<<N<<endl;

cout<<"max product "<<prod<<" = ";
for(i=0; i<N-1; ++i)
cout<<k[i]<< " * ";
cout<<k[N-1]<<"."<<endl;

delete [] k;

return 0;
}

long f(int M, int N, int* k)
{
static int index = 0;
if(M<N)
return 0;

if(N==1)
{
k[index] = M;
return M;
}
else
{
k[index] = M/N;
++index;
return M/N * f(M-M/N, N-1, k);
}
}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-6-25 16:51
HJin
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:27
帖子:401
积分:4134
注册:2007-6-9

/*---------------------------------------------------------------------------
File name: C76-67.cpp
Author: HJin
Created on: 6/25/2007 02:03:08
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762


Modification history:


经典 76 道编程题 之 67 :

67. ( NOI'94.1_3 ) 一个实数数列共有N项,已知a(i)=(a(i-1)-a(i+1))/2+d,
(1〈i〈N)(N<60) , 键盘输入N,d,a(1),a(n),m,输出 a(m)。


Analysis:

A littl bit algebra shows that we are solving a tridiagonal linear
system (refer to you linear algebra textbook for solving Ax = b).

The tridiagonal linear system solver is standard and has been know
for almost 200 years.


Sample output:

i | a[i] | (a[i-1] - a[i+1]) / 2 + d
-----+----------------------+------------------------------
1 | 0 |
2 | 0.585786 | 0.585786
3 | 0.828427 | 0.828427
4 | 0.928932 | 0.928932
5 | 0.970563 | 0.970563
6 | 0.987807 | 0.987807
7 | 0.994949 | 0.994949
8 | 0.997908 | 0.997908
9 | 0.999133 | 0.999133
10 | 0.999642 | 0.999642
11 | 0.99985 | 0.99985
12 | 0.999942 | 0.999942
13 | 0.999965 | 0.999965
14 | 1.00001 | 1.00001
15 | 0.999939 | 0.999939
16 | 1.00013 | 1.00013
17 | 0.999672 | 0.999672
18 | 1.00079 | 1.00079
19 | 0.998091 | 0.998091
20 | 1.00461 | 1.00461
21 | 0.988873 | 0.988873
22 | 1.02686 | 1.02686
23 | 0.935147 | 0.935147
24 | 1.15657 | 1.15657
25 | 0.622007 | 0.622007
26 | 1.91255 | 1.91255
27 | -1.2031 | -1.2031
28 | 6.31876 | 6.31876
29 | -11.8406 | -11.8406
30 | 32 |
-----+----------------------+------------------------------
Press any key to continue . . .

Reference:

1. 野比, 相当经典的76道编程自虐题分享.无答案 at
http://bbs.bc-cn.net/viewthread.php?tid=147853

*/

#include <iostream>
#include <iomanip>

using namespace std;

/**
n --- number of equations
sub --- subdiagonal entries
diag --- diagonal entries
sup --- supdiagonal entries
b --- original rhs vector
x --- solution vector
*/
void triDiagonal(int n,
double* sub,
double* diag,
double* sup,
double* b,
double* x);

int main(int argc, char** argv)
{
int i;
int N=30;
double *a = new double[N];
double d = 1;
int m=5;

a[0] = 0;
a[N-1] = 32;

double* sub = new double[N-2];
double* sup = new double[N-2];
double* diag = new double[N-2];
double* x = &a[1];
double* b = new double[N-2];

for(i=0; i<N-2; ++i)
{
sub[i] = 1.0;
diag[i] = -2.0;
sup[i] = -1.0;
b[i] = -2.0*d;
}
b[0] += -a[0];
b[N-3] += a[N-1];

triDiagonal(N-2, sub, diag, sup, b, x);

//cout<<x[m]<<endl;

cout<<std::left<<setw(4)<<"i"<<" | "<<std::right<<setw(20)<<"a[i]"<<" | "<<std::left<<"(a[i-1] - a[i+1]) / 2 + d"<<endl;
cout<<"-----+----------------------+------------------------------\n";
i=0;
cout<<std::left<<setw(4)<<i+1<<" | "<<std::right<<setw(20)<<a[0]<<" | "<<std::left<<" "<<endl;
for(i=1; i<N-1; ++i)
{
cout<<std::left<<setw(4)<<i+1<<" | "<<std::right<<setw(20)<<a[i]<<" | "<<std::left<<setw(20)<<( a[i-1] - a[i+1] )/2.0 + d<<endl;
}
cout<<std::left<<setw(4)<<i+1<<" | "<<std::right<<setw(20)<<a[N-1]<<" | "<<std::left<<" "<<endl;
cout<<"-----+----------------------+------------------------------\n";


delete [] a;
delete [] sub;
delete [] diag;
delete [] sup;
delete [] b;

return 0;
}

void triDiagonal(int n,
double* sub,
double* diag,
double* sup,
double* b,
double* x)
{
int i;
double factor;

// forward elimination
for (i = 1; i < n; ++i)
{
factor = -sub[i-1] / diag[i-1];
diag[i] += factor * sup[i-1];
b[i] += factor * b[i-1];
}

// backward substitution
x[n-1] = b[n-1]/ diag[n-1];
for(i = n-2; i >=0; --i)
x[i] = (b[i] - sup[i]*x[i+1]) / diag[i];
}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-6-25 19:21
HJin
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:27
帖子:401
积分:4134
注册:2007-6-9

/*---------------------------------------------------------------------------
File name: C76-74.cpp
Author: HJin
Created on: 6/25/2007 04:45:37
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762


Modification history:


经典 76 道编程题 之 74 :

74. (NOI'95.1_5) m、n为整数,且满足下列两个条件:
  ① m、n∈{1, 2, …, k}, (1≤k≤109)
② (n^2-m*n-m^2)^2=1
编一程序, 由键盘输入k, 求一组满足上述两个条件的 m、n, 并且使m^2+n^2
的值最大.
例如, 若 k=1995, 则 m=987, n=1597 时, 则 m、n 满足条件, 且可使
m^2+n^2的值最大.

Analysis:

Loop over m and n, O(k^2) algorithm.


Sample output:

3524578
m = 987, n = 1597, m^2 + n^2 = 3524578
Press any key to continue . . .


Reference:

1. 野比, 相当经典的76道编程自虐题分享.无答案 at
http://bbs.bc-cn.net/viewthread.php?tid=147853

*/

#include <iostream>
#include <iomanip>

using namespace std;


int main(int argc, char** argv)
{
int m;
int n;
int temp;
int tempSquare;
int max=2;
int k=1995;
int mKeeper=1;
int nKeeper=1;

/**
O(k^2) algorithm --- just a 2-loop.
*/
for(m=1; m<=k; ++m)
{
for(n=1; n<=k; ++n)
{
temp = n*(n-m)-m*m;
if(temp*temp == 1)
{
tempSquare=m*m+n*n;
if(max < tempSquare)
{
max = tempSquare;
mKeeper = m;
nKeeper = n;
}
}
}
}

cout<<max<<endl;
cout<<"m = "<<mKeeper<<", n = "<<nKeeper<<", m^2 + n^2 = "<<mKeeper*mKeeper + nKeeper*nKeeper<<endl;

return 0;
}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-6-25 19:56
HJin
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:27
帖子:401
积分:4134
注册:2007-6-9

To 野比:

HCL's #6 tested to be correct (at least for n = 5, 4).

To HCL:

If you could write some comments about your idea inside the source code, that would be better for a viewer to follow the algorithm (instead of just copying your souce code).

--------------------------------------------------
Enter a number : 5
25 24 23 22 21
20 19 18 17 16
15 14 13 12 11
10 9 8 7 6
5 4 3 2 1

1 3 4 10 11
2 5 9 12 19
6 8 13 18 20
7 14 17 21 24
15 16 22 23 25

1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9

Press any key to continue . . .

Enter a number : 4
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1

1 3 4 10
2 5 9 11
6 8 12 15
7 13 14 16

1 12 11 10
2 13 16 9
3 14 15 8
4 5 6 7

Press any key to continue . . .

[此贴子已经被作者于2007-6-26 3:20:39编辑过]


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-6-26 03:14
wfpb
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:29
帖子:2188
积分:22230
注册:2006-4-2

我看第4题这个简单的没人挑啊。。。

程序代码:

<BR><BR>int fun(int n)<BR>{<BR> for(int i=1;i&lt;=n;i++)<BR> {<BR> for(int j=i;j&lt;i+n;j++)<BR> cout&lt;&lt;(j-1)%n+1&lt;&lt;" ";<BR> cout&lt;&lt;endl;<BR> }<BR>}<BR>void main()<BR>{<BR> fun(5); <BR>}


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-6-26 11:16
smartwind
Rank: 3Rank: 3
等级:中级会员
威望:1
帖子:277
积分:2874
注册:2006-11-13

第三题,C++:

程序代码:

<BR>#include&lt;iostream&gt;<BR>using namespace std;</P> <P>void print(int,int,int);</P> <P>int n;</P> <P>int main()<BR>{<BR> cin&gt;&gt;n;<BR> if(n&gt;=20||n&lt;=3)<BR> return 0;<BR> int i,j;<BR> for(i=1;i&lt;=n;i++)<BR> {<BR> for(j=1;j&lt;=n;j++)<BR> {<BR> if(i==1||i==n||j==1||j==n)<BR> cout&lt;&lt;"T";<BR> else if(i==2||i==n-1||j==2||j==n-1)<BR> cout&lt;&lt;"J";<BR> else<BR> print(1,i,j);<BR> }<BR> cout&lt;&lt;endl;<BR> }<BR> system("pause");<BR>}</P> <P>void print(int a,int i,int j)<BR>{<BR> if(a&gt;n)<BR> return;<BR> if(i-2==a||i==n-a-1||j-2==a||j==n-a-1)<BR> cout&lt;&lt;a;<BR> else<BR> print(a+1,i,j);<BR> return;<BR>}<BR>


2007-6-26 11:36
smartwind
Rank: 3Rank: 3
等级:中级会员
威望:1
帖子:277
积分:2874
注册:2006-11-13

第九题:

程序代码:

<BR>#include&lt;iostream&gt;<BR>using namespace std;</P> <P>#define N 16</P> <P>#define f(n1,n2,n3,n4) {n1/=2;n2/=2;n3/=2;n4+=n1+n2+n3;}</P> <P>int main()<BR>{<BR> int a,b,c,d;<BR> a=b=c=d=N;<BR> f(a,b,c,d);<BR> f(b,c,d,a);<BR> f(c,d,a,b);<BR> f(d,a,b,c);<BR> cout&lt;&lt;a&lt;&lt;endl&lt;&lt;b&lt;&lt;endl&lt;&lt;c&lt;&lt;endl&lt;&lt;d&lt;&lt;endl;<BR> system("pause");<BR> return 0;<BR>}<BR>


2007-6-26 11:50
wfpb
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:29
帖子:2188
积分:22230
注册:2006-4-2

第8题:
不知道是不是下面的意思?

程序代码:

#include &lt;cmath&gt;<BR>template&lt;class T&gt;<BR>class BYTE<BR>{<BR> char *ptr;<BR> unsigned num;<BR>public:<BR> BYTE()<BR> {<BR> num=sizeof(T)*8;<BR> ptr=new char[num];<BR> if(!ptr)return;<BR> memset(ptr,0,num);<BR> }<BR> BYTE(T t)<BR> {<BR> num=sizeof(T)*8;<BR> ptr=new char[num];<BR> if(!ptr)return;<BR> memset(ptr,0,num);<BR> for(int i=0;i&lt;num;i++)<BR> ptr[i]=(t&amp;(T)pow(2,i))==0?0:1;<BR> }<BR> BYTE(const BYTE&lt;T&gt;&amp; byte)<BR> {<BR> num=byte.num;<BR> ptr=new char[num];<BR> if(!ptr)return;<BR> memset(ptr,0,num);<BR> for(int i=0;i&lt;num;i++)<BR> ptr[i]=byte.ptr[i];<BR> }<BR> ~BYTE()<BR> {<BR> num=0;<BR> delete[]ptr;<BR> }<BR> BYTE&lt;T&gt;&amp; operator=(BYTE&lt;T&gt;&amp; byte)<BR> {<BR> memset(ptr,0,num);<BR> for(int i=0;i&lt;num;i++)<BR> ptr[i]=byte.ptr[i];<BR> return *this;<BR> }<BR> BYTE&lt;T&gt;&amp; operator=(T t)<BR> {<BR> *this=BYTE&lt;T&gt; byte(t);<BR> return *this;<BR> }<BR> BYTE operator+(BYTE&lt;T&gt; bt)<BR> {<BR> BYTE&lt;T&gt; temp;<BR> char t=0;<BR> for(int i=0;i&lt;num;i++)<BR> {<BR> temp.ptr[i]=(t+ptr[i]+bt.ptr[i])%2;<BR> t=(t+ptr[i]+bt.ptr[i])/2;<BR> }<BR> return temp;<BR> }<BR> BYTE operator|(BYTE&lt;T&gt; bt)<BR> {<BR> BYTE&lt;T&gt; temp;<BR> char t=0;<BR> for(int i=0;i&lt;num;i++)<BR> {<BR> temp.ptr[i]=(t+ptr[i]+bt.ptr[i])%2;<BR> t=(t+ptr[i]+bt.ptr[i])/2;<BR> }<BR> return temp;<BR> }<BR> operator T()<BR> {<BR> return GetValue();<BR> }<BR> T GetValue()<BR> {<BR> T data;<BR> memset(&amp;data,0,sizeof(T));<BR> for(int i=0;i&lt;num;i++)<BR> {<BR> data|=T(int(ptr[i])*pow(2,i));<BR> }<BR> return data;<BR> }<BR> friend ostream&amp; operator&lt;&lt;(ostream &amp;os,BYTE&lt;T&gt;&amp; byte)<BR> {<BR> for(int i=byte.num-1;i&gt;=0;i--)<BR> os&lt;&lt;char(byte.ptr[i]+48);<BR> os&lt;&lt;endl;<BR> return os;<BR> }<BR>};


测试用例

程序代码:
void main()<BR>{<BR>// fun(5); <BR> BYTE&lt;int&gt; a=3;<BR> BYTE&lt;int&gt; b=3;<BR> cout&lt;&lt;b&lt;&lt;endl;<BR> cout&lt;&lt;a&lt;&lt;endl;<BR> cout&lt;&lt;b+a&lt;&lt;endl;<BR> cout&lt;&lt;(b|a)&lt;&lt;endl;<BR> cout&lt;&lt;b.GetValue()&lt;&lt;endl;<BR> int c=b;<BR> cout&lt;&lt;c&lt;&lt;endl;<BR>// string s1="1";<BR>}


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2007-6-26 12:47
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.065833 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved