注册 登录
编程论坛 C++教室

[全民编程]76道高难度C++练习题.含NOI竞赛题.欢迎挑战

野比 发布于 2007-06-16 11:57, 48947 次点击

偶然从朋友那里搞到的, 名字是<<C++入门题>>
题目都相当的经典. 还有不少是NOI的考试题.
1 - 20题作为热身(见2楼), 越往后越难, 完整版(76道)请到1楼楼底下载.

请解决问题后, 不要吝惜你的才华, 把解题方法跟贴, 大家共同学习提高.

统计时间: 2007-10-10 19:29[/quote]

目前已完成题目(按发帖时间先后排序)

题号 完成人 楼层
---------------------------------------------
2 百年不亮 3
5 HCL 4
20 aipb2007 5,6
17 游乐园/aipb2007 18/20
6 HCL 26,27
16 jiaju111 33 讨论完成, 未编程
11 aipb2007 46
75 HJin 48 不符题干, 参考
49 HJin 49
48 HJin 61
10 HJin 62 * 218楼(blueboy82006),236楼(远去的列车)修改
13 HJin 63
17 HJin 65
1 HJin 67
3 tianma8778 69 有缺陷, 供参考
24 HJin 71
1 kai 77 另一种解法
2 kai 79 数字电路分析法
4 HJin 80
3 kai 81 # With analysis
19 HJin 83
3 laigaoat2005 69
37 HJin 103 162楼补充说明(smartwind)
67 HJin 104
74 HJin 105
4 wfpb 107 * + (kai指出, 见117楼)
3 smartwind 108 *
9 smartwind 109 *
8 wfpb 110 *
3 zkkpkk 111 *
12 wfpb 112 *
75 smartwind 113 *
37 weishj 114 *
41 zkkpkk 115 * 166楼改进(smartwind)
6 zkkpkk 118 * 130楼补充蛇形填数
65 weishj 124 *
14 huozoo 126 @ 正确
7 zkkpkk 137 *
76 HJin 138 *
4 天空の城 140 *
58 HJin 141 *
9 zkkpkk 142 *
13 zkkpkk 144 *
57 HJin 147 *
53 HJin 148 *
22 HJin 149 *
43 zkkpkk 150 *
21 HJin 152 *
31 smartwind 160 * 164楼补充(HJin) 165楼补充(smartwind)
34 smartwind 161 # 分析
45 smartwind 172 *
2 zhchchqihu 194,193 * 2种方法
3 freshman42 196 *
? blueboy82006 208 *
2 卧龙孔明 209,210 * 2种方法
1 hkice 211 *
5 hkice 212 *
3 miaomiao0403 214 *
10 ml232528 215 * 指出62楼10题错误
55 远去的列车 220 *
48 远去的列车 222 *
14 huozoo 223 * 解答者本人不确定
2,3 xjlsgcjdtc 228,229 *
3 qq598369 230 *
3 远去的列车 232 *
3 曦木 233 *
6 远去的列车 234 *
7 海子星竹 239 *
3 且行且珍惜 240 *


注:
* 未测试 (Not tested)
@ 已测试 (Tested)
+ Bug
# 无源代码 (No code)

完整版(76道)下载[attach]22592[/attach]


[此贴子已经被作者于2007-10-10 19:29:50编辑过]

288 回复
#202
野比2007-08-02 00:03
答案不太全..
#203
happyfly2007-08-03 17:20
#204
happyfly2007-08-03 17:23

用的着堆栈什么的吗?是不是要有数据结构的知识啊?
不会呢还

#205
qimeiwushen2007-08-03 17:37
  我打算在三个月之内把这些题目搞定~~~~~~
#206
野比2007-08-04 10:44
加油吧...
#207
diablo02822007-08-05 23:55
大部分用c编的,C++还得改。。。。
#208
blueboy820062007-08-09 15:01
回复:(aipb2007) 11. 巧排数字。将1、2、......

to aipb2007:
我也搞了一个,就是效率不怎么高。
呵呵!

//将1--8排成一排,使相邻两数和为素数,且首尾两数的和也为素数
//编程列出所有排列。

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const N=8;

bool prime(int n)
{
if(n>=2)
{
int flag=1;
int j=2;
while(j<=sqrt(n))
{
if(n%j==0)
{flag=0;
break;}
j++;
}
if(flag)
return true;
else
return false;
}
else
return false;
}

bool sumisprime(int Array[N])
{
if(prime(Array[0]+Array[N-1]))
{
int x(1);
for(int i(0);i<N-1;i++)
if(! prime(Array[i]+Array[i+1]))
x=0;
if(x)
return true;
else
return false;
}
else
return false;
}

void main()
{
int a[N]={1,2,3,4,5,6,7,8};

while(next_permutation(a,a+N))
{
if(sumisprime(a))
{
for(int i(0);i<N;i++)
cout<<a[i]<<" ";
cout<<endl;
}
}
cout<<endl;
}

多多指点……

#209
卧龙孔明2007-08-17 10:21
第一题:
方法:最垃圾的枚举(时间效率较低,不过一秒内可以出解

#include<iostream>
int main(void)
{
char A,B,C,D,E,F,G,X,Y,Z;
for(A=0;A<10;A++)
for(B=0;B<10;B++)
if(A!=B)
for(C=0;C<10;C++)
if(B!=C && A!=C)
for(D=0;D<10;D++)
if(A!=D && B!=D && C!=D)
for(E=0;E<10;E++)
if(A!=E && B!=E && C!=E && D!=E)
for(F=0;F<10;F++)
if(A!=F && B!=F && C!=F && D!=F && E!=F)
for(G=0;G<10;G++)
if(A!=G && B!=G && C!=G && D!=G && E!=G && F!=G)
for(X=0;X<10;X++)
if(A!=X && B!=X && C!=X && D!=X && E!=X && F!=X && G!=X)
for(Y=0;Y<10;Y++)
if(A!=Y && B!=Y && C!=Y && D!=Y && E!=Y && F!=Y && G!=Y && X!=Y)
for(Z=0;Z<10;Z++)
if(A!=Z && B!=Z && C!=Z && D!=Z && E!=Z && F!=Z && G!=Z && X!=Z && Y!=Z)
if(A*10000+B*1000+(C+D+D)*100+(D+F+F)*10+E+G+G==X*10000+Y*1000+Z*100+D*10+E)
printf(" %d %d %d %d %d\n %d %d %d\n+ %d %d %d\n____________\n %d %d %d %d %d",A,B,C,D,E,D,F,G,D,F,G,X,Y,Z,D,E);
return 0;
}

#210
卧龙孔明2007-08-17 10:41
第2题:
方法:还是枚举,因为只有5个判断,所以可以瞬间出解
#include<stdio.h>
int main(void)
{
char a,b,c,d,e;
for(a=0;a<2;a++)
for(b=0;b<2;b++)
for(c=0;c<2;c++)
for(d=0;d<2;d++)
for(e=0;e<2;e++)
if(b+c==1 && c==d && d+e>0)
if(a)
{ if(b)
if(e)
{
if(a && d)
printf("A:%c B:%c C:%c D:%c E:%c\n",a ? 'Y' : 'N',b ? 'Y' : 'N',c ? 'Y' : 'N',d ? 'Y' : 'N',e ? 'Y' : 'N');
}
else
{ printf("A:%c B:%c C:%c D:%c E:%c\n",a ? 'Y' : 'N',b ? 'Y' : 'N',c ? 'Y' : 'N',d ? 'Y' : 'N',e ? 'Y' : 'N'); }
}
else
if(e)
{
if(a && d)
printf("A:%c B:%c C:%c D:%c E:%c\n",a ? 'Y' : 'N',b ? 'Y' : 'N',c ? 'Y' : 'N',d ? 'Y' : 'N',e ? 'Y' : 'N');
}
else
{ printf("A:%c B:%c C:%c D:%c E:%c\n",a ? 'Y' : 'N',b ? 'Y' : 'N',c ? 'Y' : 'N',d ? 'Y' : 'N',e ? 'Y' : 'N'); }
return 0;
}
#211
hkice2007-08-17 15:03
回复:(野比)[全民编程]76道高难度C++练习题.含NOI竞...

第一次发贴,做一做第1题。附件下载后改名为1.cpp

解体算法最原始的方法:穷举 (循环次数绝对不是10!)
耗时大概1S左右吧, 没有做时间测试。

虽然,B F G 可以推算出 为 9, 5 ,0 ,但还是最原始的方法让他们参加循环。
{
ABCDE
DFG
DFG
---------
XYZDE
推算B, 因为 X = A + 1 , 则 B 必须为 9。
(2 * FG ) % 100 必须为0 且F != G ,则 F = 5 G = 0;
}

下面给出算法:

//1.cpp
// hkice August 17 2007
// I use bcc5.5 free command line compiler
// c:> bcc32 1.cpp
// c:> 1.exe
//Result:abcdefgxyz
// 2978650314
//=======================
// 29786
// 850
// + 850
//-----------------------
// 31486

#include <iostream.h>

int main()
{
for (int z = 1; z < 10; z++)
{
for (int y = 0; y < 10; y++)
{
if (y == z)
continue;
for (int x = 1; x < 10; x++)
{
if (x == y || x == z)
continue;
for (int g = 0; g < 10; g++)
{
if (g == x || g == y || g == z)
continue;
for (int f = 0; f < 10; f++)
{
if (f == g || f == x || f == y || f == z)
continue;
for (int e = 0; e < 10; e++)
{
if (e == f || e == g || e == x || e == y || e == z)
continue;
for (int d = 1; d < 10; d++)
{
if (d == e || d == f || d == g || d == x || d == y || d == z)
continue;
for (int c = 0; c < 10; c++)
{
if (c == d || c == e || c == f || c == g || c == x || c == y || c == z)
continue;
for (int b = 0; b < 10; b++)
{
if (b == c || b == d || b == e || b == f || b == g || b == x || b == y || b == z)
continue;
for (int a = 1; a < 10; a++)
{
if (a == b || a== c || a == d || a == e || a == f || a == g || a == x || a == y || a == z)
continue;
//cout <<"Test" << a << b << c << d << e << f << g << x << y << z << endl;

int abcde = a * 10000 + b * 1000 + c * 100 + d * 10 + e;
int dfg = d * 100 + f * 10 + g;
int xyzde = x* 10000 + y * 1000 + z* 100 + d * 10 + e;
if ( abcde + 2 * dfg - xyzde == 0)
{
cout << "Result:" << 'a' << 'b' << 'c' << 'd' << 'e' << 'f' << 'g'
<< 'x' << 'y' << 'z' << endl;
cout << " " << a << b << c << d << e << f << g << x << y << z << endl;
cout << "=======================" <<endl;
cout << " " << abcde <<endl;
cout << " " << dfg <<endl;
cout << " + " << dfg <<endl;
cout << "-----------------------" << endl;
cout << " " << xyzde << endl;
}

}
}
}
}

}
}
}
}
}
}
return 0;
}







只有本站会员才能查看附件,请 登录

[此贴子已经被作者于2007-8-17 15:30:55编辑过]

#212
hkice2007-08-17 17:19
回复:(野比)[全民编程]76道高难度C++练习题.含NOI竞...

第五题,有位同志给出了堆栈算法,这里给出的是递归算法:

//5.cpp
// hkice August 17 2007
// I use bcc5.5 free command line compiler
// c:> bcc32 5.cpp
// c:> 5.exe
//
//c:>bcc32 5.cpp
//Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
//5.cpp:
//Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

//c:>5.exe
//Intput number:10
//Input ruler:3
//10(10) has converted to 101(3)

//c:>5.exe
//Intput number:3
//Input ruler:2
//3(10) has converted to 11(2)

//c:>5.exe
//Intput number:255
//Input ruler:2
//255(10) has converted to 11111111(2)

#include <iostream.h>

int convert(int number, int ruler);

int main()
{
int number, ruler;
cout << "Intput number:";
cin >> number;
cout << "Input ruler:";
cin >> ruler;
cout << number <<"(10) has converted to ";
convert(number, ruler);
cout << "(" << ruler << ")" << endl;

return 0;
}

int convert(int number, int ruler)
{
if (number >= ruler)
convert(number/ruler , ruler);
cout << number%ruler;
return number%ruler;
}

只有本站会员才能查看附件,请 登录

[此贴子已经被作者于2007-8-17 17:20:21编辑过]

#213
thintear2007-08-17 17:33

不会啊``

#214
miaomiao04032007-08-17 19:25
第三题 交流一下
分析:可以看到方阵是中心对称的,先考虑左上角,发现a[i][j]所在层数即i,j中较小的那一个(层数从零算起)这样就可以为左上角赋值,后边就好解决了
#include <stdio.h>
#define N 15
main()
{
char a[N][N]={0};
int i,j,M,storey,tag=N%2;
storey=(N+1)/2-1;
for(i=0;i<=storey;i++)
for(j=0;j<=storey;j++)
{
M=(i<=j)?i:j;
if(0==M) a[i][j]='T';
else if(1==M) a[i][j]='J';
else a[i][j]=M-1+'0';
}
for(i=0;i<=storey;i++)
for(j=storey+1;j<N;j++)
{
if(tag)
a[i][j]=a[i][2*M-j];
else
a[i][j]=a[i][2*M-j+1];
}
for(i=storey+1;i<N;i++)
for(j=0;j<N;j++)
{
if(tag)
a[i][j]=a[2*M-i][j];
else
a[i][j]=a[2*M-i+1][j];
}
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("%c ",a[i][j]);
printf("\n");
}
}
#215
ml2325282007-08-18 14:37
关于第10题62楼可能有错,个数对得上,就总面积不行
请高手指点

如图1所示,编写程序计算 ┎┰┰┰┰┰┰┰┰┰┒
大大小小正方形共有多少?当最小 ┠╂╂╂╂╂╂╂╂╂┨
正方行边长为1时,它们的总面积 ┠╂╂╂╂╂╂╂╂╂┨
共为多少? ┠╂╂╂╂╂╂╂╂╂┨
┠╂╂╂╂╂╂╂╂╂┨
┠╂╂╂╂╂╂╂╂╂┨
┠╂╂╂╂╂╂╂╂╂┨
┠╂╂╂╂╂╂╂╂╂┨
┠╂╂╂╂╂╂╂╂╂┨
┠╂╂╂╂╂╂╂╂╂┨
┖┸┸┸┸┸┸┸┸┸┚



#include<iostream>
using namespace std;
int main()
{
int a[11][11];
int q;
int n=0;
int s=0;
cout<<"请输入第一边有多少点=";
cin>>q;
for(int i=0;i<q;i++)
for(int j=0;j<q;j++)
for(int k=0;k<q;k++)
for(int m=0;m<q;m++)
if((i-k)&&(i-k)==(m-j)){n+=1;s+=(i-k)*(i-k);}
cout<<"正方行总个数n="<<n/2.0<<endl;
cout<<"总面积s="<<s/2.0<<endl;
cin>>q;
}


#216
blueboy820062007-08-21 20:20
?????????

[此贴子已经被作者于2007-8-21 20:41:51编辑过]

#217
blueboy820062007-08-21 20:22
回复:(ml232528) 关于第10题62楼可能有错,个数...

Great minds think alike!

#218
blueboy820062007-08-21 20:42
TO HJin:

以下是引用HJin在2007-6-19 6:55:37的发言:

total number of squares is 1^2 + 2^2 +... +n^2 = n*(n+1)*(2*n+1)/6
total area is 1*n^2 + 2*(n-1)^2 + ... + n*1^2 = n*(n+1)^2*(n+2)/12
//total area is 1^2*n^2 + 2^2*(n-1)^2 + ... + n^2*1^2

int totalArea(int n)

{
int area = 0;

for(int i=1; i<=n; ++i)
{
area += i*(n+1-i)*(n+1-i); //area += i*i*(n+1-i)*(n+1-i)
}

return area;
}

YOU made a mistake at the red part
I corrected it behind in green.



[此贴子已经被作者于2007-8-22 16:35:10编辑过]

#219
huozoo2007-08-22 09:39
回复:(野比)[全民编程]76道高难度C++练习题.含NOI竞...

嘿嘿```大家好,好久不见,闭关练武,才回来```

#220
远去的列车2007-08-23 15:39
新手做 55 题

55. (液晶显示) 下图是用液晶七笔阿拉数字表示的十个数字,我们把横和竖的一
个短划都称为一笔,即7有3笔,8有7笔等。请把这十个数字重新排列,要做到
两相邻数字都可以由另一个数字加上几笔或减去几笔组成,但不能又加又减。比如
7→3是允许的,7→2不允许。编程打印出所有可能的排列。
如:4107395682。

#include <iostream>
#define DIM(num) sizeof((num))/sizeof( (num)[0])

using namespace std;

const int code[10][7] = {
{1,1,1,1,1,1,0},{0,0,1,1,0,0,0},
{0,1,1,0,1,1,1},{0,1,1,1,1,0,1},
{1,0,1,1,0,0,1},{1,1,0,1,1,0,1},
{1,1,0,1,1,1,1},{0,1,1,1,0,0,0},
{1,1,1,1,1,1,1},{1,1,1,1,1,0,1}
}; //数字编码,1为亮,0为灭

bool isInclude(const int a,const int b); //显示 a的段包含着 b
bool isNext(const int a,const int b); //a,b两个数字可以相邻吗?
void print(int *a);

int main()
{
int num[10] = {0,1,2,3,4,5,6,7,8,9};
int x = 0;
while (next_permutation(num,num+DIM(num)))
{
int s = 0;
for (int i=0; i<=8; i++)
if (!isNext(num[i],num[i+1]))
s++; //此处效率低!用break跳不到 next_permutation,请指教
if (s == 0)
{
print(num);
x++;
}
}
cout << "共有 " << x << " 种方法." << endl;
return 0;
}

bool isInclude(const int a,const int b)
{
for (int i=0; i<=6; i++)
if ((code[b][i] == 1) && (code[a][i] == 0))
return false;
return true; //b编码中为1的位置,a编码相应位置也为1,true
}

bool isNext(const int a,const int b)
{
return (isInclude(a,b) || isInclude(b,a)); // 相互包含,即 “加上几笔或减去几笔”
}

void print(int *a)
{
for (int i=0; i<=9; i++)
cout << a[i] << " ";
cout << endl;
}

#221
远去的列车2007-08-23 15:51

上题分析编码规则: 
“8”(“日”) 拆成“口”和“凵”
其中 “口” 按笔划依次是 1,2,3,7号位;“凵”按笔划依次为 6,5,4号位

运行结果给出:

0 7 3 1 4 9 5 6 8 2
2 8 0 7 3 1 4 9 5 6
2 8 3 7 0 1 4 9 5 6
2 8 4 1 0 7 3 9 5 6
2 8 6 5 9 3 7 0 1 4
2 8 6 5 9 4 1 0 7 3
2 8 6 5 9 4 1 3 7 0
3 7 0 1 4 9 5 6 8 2
4 1 0 7 3 9 5 6 8 2
6 5 9 3 7 0 1 4 8 2
6 5 9 4 1 0 7 3 8 2
6 5 9 4 1 3 7 0 8 2
共有 12 种方法.


Terminated with return code 0
Press any key to continue ...

[此贴子已经被作者于2007-8-23 16:06:24编辑过]

#222
远去的列车2007-08-23 16:24
回复:(野比)[全民编程]76道高难度C++练习题.含NOI竞...

48. 将4个红球,3个白球与3个黄球排成一排,共有多少种排法?


#include <iostream>

#define DIM(x) sizeof((x))/sizeof((x)[0])
using namespace std;

int main()
{
int a[] = {0,0,0,0,1,1,1,2,2,2}; // 0 表示红球,1表示白球,2表示黄球
int s = 1; //数组a初始化也是一种
while (next_permutation(a,a+DIM(a)))
s++;
cout << "共有 " << s << " 种排法。" << endl;
}


输出结果:

共有 4200 种排法。


Terminated with return code 0
Press any key to continue ...

算不算投机

[此贴子已经被作者于2007-8-23 16:24:55编辑过]

#223
huozoo2007-08-23 20:22
回复:(野比)[全民编程]76道高难度C++练习题.含NOI竞...
原来写了在9页,上次在网吧写的,没编译,现在用编译器运行下,再发上来完整的.
14.(已确定正确):
#include<iostream>
using namespace std;
int main()
{
const int n=20;
int i,j;
char t,a[n];
for(i=0;i<n/2;i++)
{
a[i]='*';
}
for(i=n/2;i<n;i++)
{
a[i]='0';
}
cout<<"原排列为:"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
for(i=n/2;i<n;i++) //利用两个for,原理很简单,我不会把别人的水平看太低,所以不写了。
for(j=i;j>0&&a[j-1]!='0';j--)
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
cout<<"\n\n\n交换后排列变为:"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl<<endl;
return 0;
}

[此贴子已经被作者于2007-8-23 20:32:54编辑过]

#224
HJin2007-08-24 22:49
回复:(blueboy82006)TO HJin:以下是引用HJin在2007...

Thanks for pointing out my mistake.

I still remember a thing happened when I studied in Beijing. I pointed out a mistake in a woman professor's book, she got furious.

#225
HJin2007-08-24 22:50
回复:(远去的列车)回复:(野比)[全民编程]76道高...
Great algorithm.
#226
blueboy820062007-08-28 21:26
回复:(HJin)回复:(blueboy82006)TO HJin:以下是...
To HJin:

It's so great and lucky that you did not do that.

[此贴子已经被作者于2007-8-28 21:29:21编辑过]

#227
zptk7202007-09-02 12:09
题目真多,慢慢做来看看
#228
xjlsgcjdtc2007-09-05 21:08

路过,写个第二题:

#include<iostream>
using namespace std;

bool check(int a[])
{
if(a[0]==1&&a[1]==0) return false;
if(a[1]==1&&a[2]==1) return false;
if((a[2]==1&&a[3]==0) ||(a[2]==0&&a[3]==1)) return false;
if(a[3]==0&&a[4]==0) return false;
if(a[4]==1&&(a[1]==0||a[3]==0)) return false;
return true;
}
int main()
{
int a[5];
for(a[0]=0;a[0]<2;a[0]++)
{
if(a[1]=1) a[1]=0;
for(a[1]=0;a[1]<2;a[1]++)
{
if(a[2]=1) a[2]=0;
for(a[2]=0;a[2]<2;a[2]++)
{
if(a[3]=1) a[3]=0;
for(a[3]=0;a[3]<2;a[3]++)
{
if(a[4]=1) a[4]=0;
for(a[4]=0;a[4]<2;a[4]++)
{
if(check(a))
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<endl;
}
}
}
}
}
return 0;
}

#229
xjlsgcjdtc2007-09-05 22:53

第三题:


#include<iostream>
#define max 100
using namespace std;

void copy1(int a[],int k,int n)
{
for(int i=k+1;i<=(n-4)/2;i++)
a[i]=a[k];
}
void copy2(int a[],int k,int n)
{
for(int i=k-1;i>(n-4)/2;i--)
a[i]=a[k];
}
int main()
{
int n;
cout<<"输入n的值(n>=4):"<<endl;
cin>>n;
int a[100];
int i,j,k;
for(k=0;k<n;k++) cout<<"T"<<" ";
cout<<endl;
cout<<"T"<<" ";
for(k=0;k<n-2;k++) cout<<"J"<<" ";
cout<<"T"<<endl;
for(i=1;i<=(n-4)/2+1;i++)
{
for(j=1;j<=i;j++)
{
a[j-1]=j;
copy1(a,(j-1),n);
}
for(j=1;j<=i;j++)
{
a[n-4-j]=j;
copy2(a,(n-4-j),n);
}
cout<<"T"<<" "<<"J"<<" ";
for(k=0;k<n-4;k++) cout<<a[k]<<" ";
cout<<"J"<<" "<<"T"<<" ";
cout<<endl;
}
for(i=n-4;i>(n-4)/2+1;i--)
{
for(j=1;j<=i-((n-4)/2+1);j++)
{
a[j-1]=j;
copy1(a,(j-1),n);
}
for(j=1;j<=i-((n-4)/2+1);j++)
{
a[n-4-j]=j;
copy2(a,(n-4-j),n);
}
cout<<"T"<<" "<<"J"<<" ";
for(k=0;k<n-4;k++) cout<<a[k]<<" ";
cout<<"J"<<" "<<"T"<<" ";
cout<<endl;
}
cout<<"T"<<" ";
for(k=0;k<n-2;k++) cout<<"J"<<" ";
cout<<"T"<<endl;
for(k=0;k<n;k++) cout<<"T"<<" ";
cout<<endl;
return 0;
}

#230
qq5983692007-09-06 13:05

#include<iostream.h>
#define N 20
void main()
{ int i,j,k,q;
q=N/2;
char a[N][N];
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
a[i][j]='T';
}

for(i=1;i<N-1;i++)
for(j=1;j<N-1;j++)
{
a[i][j]='J';
}
char t,h=2;
t=N-2;

for(k=1;k<=q;k++)
{
for(i=h;i<t;i++)
for(j=h;j<t;j++)
{
a[i][j]=k+48;
}
t--;
h++;}
for(i=0;i<N;i++)
{ for(j=0;j<N;j++)
{cout<<a[i][j];
}
cout<<"\n";}

}

#231
yandongdido2007-09-06 21:53
5. 输入一个十进数,将其转换成 N 进制数(0<N<=16)。

#include<iostream.h>
void transfrom(int n,int base)
{
int num[20],i=0,j,m=n;
do
{
i++;
num[i]=m%base;
m=m/base;
}while(m!=0);
cout<<"**"<<n<<"转化为"<<base<<"进制的结果为:";
for(j=i;j>=1;j--)
if(num[j]<10) cout<<num[j];
else cout<<char(num[j]+'A'-10);
cout<<endl;
}
void main()
{
int n,choice;
cout<<"输入一个整数"; cin>>n;
do
{
cout<<"输入需要转化的进制:2-9,11-16";
cin>>choice;
switch(choice)
{
case 2:transfrom(n,2);
break;
case 3:transfrom(n,3);
break;
case 4:transfrom(n,4);
break;
case 5:transfrom(n,5);
break;
case 6:transfrom(n,6);
break;
case 7:transfrom(n,7);
break;
case 8:transfrom(n,8);
break;
case 9:transfrom(n,9);
break;
case 11:transfrom(n,11);
break;
case 12:transfrom(n,12);
break;
case 13:transfrom(n,13);
break;
case 14:transfrom(n,14);
break;
case 15:transfrom(n,15);
break;
case 16:transfrom(n,16);
break;
default:cout<<""<<endl;
}
} while(choice!=0);
return ;
}
#232
远去的列车2007-09-07 15:15
练习了用二维数组做参数传递

3. 打印一个 N*N 的方阵,N为每边 N=15 打印出下面图形
字符的个数(3<N<20), 要求最 TTTTTTTTTTTTTTT
外一层为"T", 第二层为"J", 从第三层 TJJJJJJJJJJJJJT
起每层依次打印数字 1,2,3,... TJ11111111111JT
(右图以N为15为例) TJ12222222221JT
TJ12333333321JT
TJ12344444321JT
TJ12345554321JT
TJ12345654321JT
TJ12345554321JT
TJ12344444321JT
TJ12333333321JT
TJ12222222221JT
TJ11111111111JT
TJJJJJJJJJJJJJT
TTTTTTTTTTTTTTT

#include <iostream>
#include <cassert>
using namespace std;

void setvalue(int (*a)[19], int n, int d, int value);
void show(int (*a)[19], int n);

int main()
{
int n, d = 0;
int a[19][19] = {0};
cout << "Please input N (3 < N < 20): ";
cin >> n;
assert((n>3) && (n<20));
cout << endl;

while (d <= (n+1)/2) // 1,2,3,……
{
setvalue(a, n, d, d-1); // 其中 'T' 用 -1 代替,'J' 用 0 代替
d++;
}

show(a, n);
}

void setvalue(int (*a)[19], int n, int d, int value)
{
for (int i=d; i<=n-1-d; i++)
{
a[d][i] = a[n-1-d][i] = value;
a[i][d] = a[i][n-1-d] = value;
}
}

void show(int (*a)[19], int n)
{
for (int i=0; i<=n-1; i++)
{
for (int j=0; j<=n-1; j++)
{
if (a[i][j] == -1)
cout << "T "; // 显示时变回来
else if (a[i][j] == 0)
cout << "J ";
else
cout << a[i][j] << " ";
}
cout << endl;
}
}

[此贴子已经被作者于2007-9-8 8:10:11编辑过]

#233
曦木2007-09-11 17:21

第三题:(
// 矩阵.cpp : 定义控制台应用程序的入口点。
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n;//矩阵的长和宽
char s[20][20];
char ss[]={'T','J','0','1','2','3','4','5','6','7'};
input: cout<<"请输入矩阵的长宽(长和宽不小于3,不大于20):";
cin>>n;
cout<<endl;
if(n<3||n>20)
{
cout<<"不符合要求的输入,请重新输入"<<endl;
goto input;
}
else
if(n%2!=0)
{
for(int i=0;i<=n/2;i++)
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)
{
if(j==i&&k>=j&&k<=n-j-1)
{
s[j][k]=ss[i];
s[n-j-1][k]=ss[i];
}
if(k==i&&j>=k&&j<=n-k-1)
{
s[j][k]=ss[i];
s[j][n-k-1]=ss[i];
}
}
}
if(n%2==0)
{
for(int i=0;i<n/2;i++)
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)
{
if(j==i&&k>=j&&k<=n-j-1)
{
s[j][k]=ss[i];
s[n-j-1][k]=ss[i];
}
if(k==i&&j>=k&&j<=n-k-1)
{
s[j][k]=ss[i];
s[j][n-k-1]=ss[i];
}
}
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(j==n-1)
cout<<s[i][j]<<endl;
else
cout<<s[i][j];
}
cout<<"继续请输如1,退出请输入其他:";
int a=0;
cin>>a;
if(a==1)
goto input;
system("pause");
}




#234
远去的列车2007-09-13 15:40
纯数学方法找规律

6. 矩阵中填数. 当给出 N*N 的矩阵,要求用程序填入下列形式的数:

① 倒填,例如N=5 ② 蛇形填数 ③ 回转填数

┌—┬─—┬─┬─—┬─—┐ ┌─┬─—┬—┬—─┬—┐ ┌─—┬─┬—─┬—┬─—┐
│25 │24 │23 │22 │21 │ │ 1 │ 3 │ 4 │10 │11 │ │ 1 │16 │15 │14 │13 │
├─┼─—┼─┼—─┼─—┤ ├─┼——┼─┼—─┼—┤ ├——┼—┼─—┼─┼——┤
│20 │19 │18 │17 │16 │ │ 2 │ 5 │ 9 │12 │19 │ │ 2 │17 │24 │23 │12 │
├—┼—─┼─┼─—┼─—┤ ├─┼——┼─┼——┼─┤ ├─—┼─┼─—┼─┼——┤
│15 │14 │13 │12 │11 │ │ 6 │ 8 │13 │18 │20 │ │ 3 │18 │25 │22 │11 │
├—┼—─┼——┼─┼─—┤ ├—┼─—┼—┼─—┼—┤ ├─—┼—┼——┼─┼——┤
│10 │ 9 │ 8 │ 7 │ 6 │ │ 7 │14 │17 │21 │24 │ │ 4 │19 │20 │21 │10 │
├─┼─—┼─—┼—┼—─┤ ├—┼─—┼—┼─—┼—┤ ├─—┼─┼─—┼—┼——┤
│ 5 │ 4 │ 3 │ 2 │ 1 │ │15 │16 │22 │23 │25 │ │ 5 │ 6 │ 7 │ 8 │ 9 │
└—─┴─┴─—┴─┴─—┘ └─┴─—┴─┴─—┴—┘ └─—┴─┴─—┴—┴─—┘

蛇形分析:
1.对称,a[0][0]+a[4][4]=26, a[i][j]+a[4-i][4-j]=26

2.数字金字塔如下:
1
2 3
4 5 6 第i行行尾数:tail = i*(i+1)/2
7 8 9 10 顺序排列还是逆序排列,得看i的奇偶性

#include <iostream>
#include <iomanip>
#include <cassert>
using namespace std;

void setvalue1(int (*a)[11], int n);
void setvalue2(int (*a)[11], int n);
void setvalue3(int (*a)[11], int circle, int value, int n);
void show(int (*a)[11], int n);

int main()
{
int n;
cout << "Input the class n (1~10): ";
cin >> n;
assert((n >= 1) && (n <= 10));
cout << endl;

int a[11][11];
setvalue1(a,n);
cout << setw(2) << n << "阶倒填矩阵:" << endl;
show(a,n);

setvalue2(a,n);
cout << setw(2) << n << "阶蛇形矩阵:" << endl;
show(a,n);

setvalue3(a,0,1,n);
cout << setw(2) << n << "阶回转矩阵:" << endl;
show(a,n);
}

void setvalue1(int (*a)[11], int n) //倒填
{
int max = n * n;
for (int i=0; i<=n-1; i++)
for (int j=0; j<=n-1; j++)
a[i][j] = max - n*i - j;
}

void setvalue2(int (*a)[11], int n) //蛇形填数
{
for (int i=0; i<=n-1; i++)
for (int j=0; j<=n-1-i; j++)
{
int s = i + j;
int tail = (s+1)*(s+2)/2; //数字金字塔行尾数计算, 金字塔的行 s 为数组行、列标和
a[i][j] = (s%2 == 0) ? (tail-j) : (tail-i); //蛇形排列与行数 s 有关
a[n-1-i][n-1-j] = n*n+1-a[i][j]; //关于 n*n+1 对称
}
}

void setvalue3(int (*a)[11], int circle, int value, int n) //回转填数
{
if (circle < n/2) //转第 circle+1 个圈, circle 从0开始
{
int i,j;
for (i=circle; i<n-1-circle; i++)
a[i][circle] = value++;
for (j=circle; j<n-1-circle; j++)
a[i][j] = value++;
for (i=n-1-circle; i>circle; i--)
a[i][j] = value++;
for (j=n-1-circle; j>circle; j--)
a[i][j] = value++;

setvalue3(a,circle+1,value,n); //递归填内圈
}

else if (n%2 != 0) //奇数阶数组中心的数
a[circle][circle] = value;
}

void show(int (*a)[11], int n)
{
for (int i=0; i<=n-1; i++)
{
for (int j=0; j<=n-1; j++)
cout << setw(4) << a[i][j];
cout << endl;
}
cout << endl;
}

#235
reedleaf2007-09-14 01:31
来晚了,
这么多,只有边看边学习了
#236
远去的列车2007-09-17 15:22
回复:(HJin)/*----------------------------------...

To HJin:

你在62楼关于第10题的解答:


Analysis:

i | # of squares with side length i | total area
----+------------------------------------------------
n | 1^2 | n*1^2 //面积计算公式错误 n^2 * 1^2
n-1 | 2^2 | (n-1)*2^2 // (n-1)^2 * 2^2
n-2 | 3^2 | (n-2)*3^2 //……
.....................................................
2 | (n-2)^2 | 2*(n-2)^2
1 | n^2 | 1*n^2

total number of squares is 1^2 + 2^2 +... +n^2 = n*(n+1)*(2*n+1)/6
total area is 1*n^2 + 2*(n-1)^2 + ... + n*1^2 = n*(n+1)^2*(n+2)/12 //


Sample output:

n | # of squares | total area | # of squares 2 | total area 2
-----+--------------+--------------+----------------+---------------
1 | 1 | 1 | 1 | 1
2 | 5 | 6 | 5 | 6 // total area = 8,以下...
3 | 14 | 20 | 14 | 20
4 | 30 | 50 | 30 | 50
5 | 55 | 105 | 55 | 105
6 | 91 | 196 | 91 | 196
……

#237
HJin2007-09-17 15:48
thanks, i think people already pointed out my mistakes.

but I did appreciated your time for finding my mistakes.
#238
远去的列车2007-09-17 15:51
我做这题跟你答案不一样,偶然发现,不是故意找茬
#239
海子星竹2007-09-19 15:09

7.读入一行文本,包含若干个单词(以空格间隔,%结尾)。将其中以 A 开头的
单词与以 N 结尾的单词,用头尾交换的办法予以置换。

#include<iostream>
#include<string>

using namespace std;

class Text
{
public:
Text(string p)
{
a = p;
}

void judge();

void show()
{
cout<<a<<endl<<endl;
}

private:
string a;
};

void Text::judge()
{
a = a.substr(0,a.find("%"));
if(a.at(0) == 'A' && a.at(a.length()-1) == 'N')
{
a.replace(0,1,"N");
a.replace(a.length()-1,1,"A");
}
}

int main ()
{
string b;
cout <<"输入:";
getline(cin,b);
Text a(b);
a.judge();
a.show();
return 0;

}

#240
且行且珍惜2007-09-30 07:46
第三题:
[CODE]public class print{
private static int N = 14;
public static void main(String args[] ){
int [][] a = new int[N][N];
int m;
if(N%2==0)
m=N/2-1;
else
m=N/2;
for(int i=0;i<=m;i++)
for(int j=i;j<=m;j++)
{
a[i][j]=i-1;


a[j][i]=a[i][j];
a[i][N-j-1]=a[i][j];
a[j][N-i-1]=a[j][i];
a[N-i-1][j]=a[i][j];
a[N-j-1][i]=a[j][i];
a[N-j-1][N-i-1]=a[j][i];
a[N-i-1][N-j-1]=a[i][j];
}
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(a[i][j]==-1)
System.out.print("T ");
else if(a[i][j]==0)
System.out.print("F ");
else
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}[/CODE]
#241
eagleboycn2007-10-01 01:59
好多高手啊
#242
上杉冰枫2007-10-05 02:30

那是相当的多好不~ 好多已经不能形容了啊

#243
snipered2007-10-10 17:23
第3题

#include<stdio.h>
#include<stdlib.h>
#define N 15
void main()
{
int i,j,c,k,l;
k=N-2;
l=3;
c=1;
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
{
if(i==N||j==N||i==1||j==1)
{
printf("T");
}
else if(i==(N-1) || j==(N-1) ||i==2||j==2)
{
printf("J");
}
else if(i==k||j==k||i==l||j==l)

{

printf("%d",1);
}
else if(i==(k-1)||j==(k-1)||i==(l+1)||j==(l+1))

{

printf("%d",2);
}
else if(i==(k-2)||j==(k-2)||i==(l+2)||j==(l+2))

{

printf("%d",3);
}
else if(i==(k-3)||j==(k-3)||i==(l+3)||j==(l+3))

{

printf("%d",4);
}
else if(i==(k-4)||j==(k-4)||i==(l+4)||j==(l+4))

{

printf("%d",5);
}
else
{
printf("%d",6);
}
}
printf("\n");
}

}


大家看看能不能精简一下!!
我水平不够啊只能做到这样!

[此贴子已经被野比于2007-10-10 19:03:21编辑过]

#244
china25qd2007-10-24 22:03

第2题如果这样考虑:
A、B、C、D、E五名学生有可能参加计算机竞赛,根据下列条件判断哪些
人参加了竞赛:

(1)A参加时,B也参加;A && B

(2)B和C只有一个人参加;(B && !C) || (!B && C)

(3)C和D或者都参加,或者都不参加;(C && D) || (!C && !D)

(4)D和E中至少有一个人参加;D || E

(5)如果E参加,那么A和D也都参加。IF E THEN (A && D)

该怎么写呢?

#245
solardo2007-10-25 22:00
回复:(kai)关于第一题给出另一种解法:ABCDE + (DF...

我觉得这样解 不妥;因为你是用自己的脑子分析后才让计算机帮你完成之后的工作;你代劳了;不是编程需要的;

#246
ytmGring2007-10-26 01:03
有意思~一定要来挑战一回
#247
solardo2007-10-27 08:44

太难了,打击信心!

#248
jonc2007-10-30 14:47

#include<iostream>
using namespace std;
int main()
{
int A,B,C,D,E;
for(A=0;A<2;A++)
for(B=0;B<2;B++)
for(C=0;C<2;C++)
for(D=0;D<2;D++)
for(E=0;E<2;E++)
{
if((B>=A)
&&((C+B)==1)
&&(D==C)
&&(D+E)
&&(!E||(E&&A&&D)))
{
if(A==1)
cout<<"A参加!"<<endl;
else
cout<<"A不参加" <<endl;

if(B==1)
cout<<"B参加!"<<endl;
else
cout<<"B不参加" <<endl;
if(C==1)
cout<<"C参加!"<<endl;
else
cout<<"C不参加" <<endl;
if(D==1)
cout<<"D参加!"<<endl;
else
cout<<"D不参加" <<endl;

if(E==1)
cout<<"E参加!"<<endl;
else
cout<<"E不参加" <<endl;
}
}

return 0;

}

#249
六道2007-10-30 21:01

第3题:

#include<iostream.h>
#include<iomanip.h>

void main()
{
char s[8]={'T','J','1','2','3','4','5','6'};
char d[20][20];
int i,j,n,k=7;
cout<<"please input a number(3~20):";
cin>>n;
if(n<3||n>20)
cout<<"input error!\n";
cout<<"方阵如下:\n";
for(i=n/2;i>=0;i--,k--)
for(j=n-1;j>=i;j--)
{
d[i][j]=d[j][i]=d[n-1-i][j]=d[j][n-1-i]=s[k];
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<setw(2)<<d[i][j];
cout<<endl;
}
}

#250
六道2007-10-30 22:20

第4题:
方法1:(向左移)
#include<iostream.h>
#include<iomanip.h>
void main()
{
int a[100];
int i,j,m,n,temp;
cout<<"please input a number:";
cin>>n;

for(i=0;i<n;i++)
a[i]=i;

for(i=1;i<n;i++)
{
temp=a[0];
for(j=0;j<n-1;j++)
a[j]=a[j+1];
a[n-1]=temp;
for(m=0;m<n;m++)
cout<<setw(2)<<a[m];
cout<<endl;
}
}

方法2:(向右移)
#include<iostream.h>
#include<iomanip.h>
void main()
{
int a[100];
int i,j,m,n,temp;
cout<<"please input a number:";
cin>>n;

for(i=0;i<n;i++)
a[i]=i;

for(i=n-1;i>=0;i--)
{
temp=a[n-1];
for(j=n-2;j>=0;j--)
a[j+1]=a[j];
a[0]=temp;
for(m=0;m<n;m++)
cout<<setw(2)<<a[m];
cout<<endl;
}
}

#251
travelling2007-10-31 00:13
     多谢供,以后有的研究了!
123456