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







sCGgtX9B.txt (2.01 KB) 回复:(野比)[全民编程]76道高难度C++练习题.含NOI竞...


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

2007-08-17 15:03
hkice
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-8-17
收藏
得分:0 
回复:(野比)[全民编程]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;
}

F9mW6tG5.txt (1001 Bytes) 回复:(野比)[全民编程]76道高难度C++练习题.含NOI竞...


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

2007-08-17 17:19
thintear
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-8-17
收藏
得分:0 

不会啊``

2007-08-17 17:33
miaomiao0403
Rank: 1
等 级:新手上路
帖 子:38
专家分:0
注 册:2006-8-22
收藏
得分:0 
第三题 交流一下
分析:可以看到方阵是中心对称的,先考虑左上角,发现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");
}
}
2007-08-17 19:25
ml232528
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:367
专家分:879
注 册:2007-7-23
收藏
得分:0 
关于第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;
}



-︻┻┳═一 ☆ 悲伤的代价就是让自己明白什么是最重要的和应该珍惜的
2007-08-18 14:37
blueboy82006
Rank: 5Rank: 5
来 自:幻想世界
等 级:贵宾
威 望:16
帖 子:1227
专家分:57
注 册:2007-7-23
收藏
得分:0 
?????????

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


2007-08-21 20:20
blueboy82006
Rank: 5Rank: 5
来 自:幻想世界
等 级:贵宾
威 望:16
帖 子:1227
专家分:57
注 册:2007-7-23
收藏
得分:0 
回复:(ml232528) 关于第10题62楼可能有错,个数...

Great minds think alike!


2007-08-21 20:22
blueboy82006
Rank: 5Rank: 5
来 自:幻想世界
等 级:贵宾
威 望:16
帖 子:1227
专家分:57
注 册:2007-7-23
收藏
得分:0 
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编辑过]


2007-08-21 20:42
huozoo
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-6-28
收藏
得分:0 
回复:(野比)[全民编程]76道高难度C++练习题.含NOI竞...

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

2007-08-22 09:39
远去的列车
Rank: 1
等 级:新手上路
威 望:2
帖 子:205
专家分:0
注 册:2007-8-7
收藏
得分:0 
新手做 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;
}


C++学习
2007-08-23 15:39
快速回复:[全民编程]76道高难度C++练习题.含NOI竞赛题.欢迎挑战
数据加载中...
 
   



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

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