USACO 1.1 Broken Necklace (模拟)
2015-09-09 19:51
295 查看
<pre name="code" class="cpp">#include <stdio.h> #define DEBUG 0 #define TESTCASES 8 #define LEN_NECKLACE 350 #define MOD(x) ( (x) < 0 ? (x) + numOfBeads : (x) % numOfBeads )//用取模操作来模拟环的技巧真赞 int numOfBeads; char necklace[LEN_NECKLACE + 1]; int collect(int brokenBead, int direction){ int color = 'w'; int bead = MOD(brokenBead); int beadsCollected; for (beadsCollected = 0; beadsCollected < numOfBeads; beadsCollected++){ //colory用来对比的颜色,初始化为白色,遇到非白的珠子便更新为相应的颜色 if (color == 'w' && necklace[bead] != 'w') color = necklace[bead]; //如果color和要收集的珠子的颜色都非白而且不同颜色,停止收集 if (color != 'w' && necklace[bead] != 'w' && necklace[bead] != color) break; bead = MOD(bead + direction); } return beadsCollected; } int main(){ #if DEBUG int testCase; for (testCase = 1; testCase <= TESTCASES; testCase++){ char inputFileName[20] = "necklace.inX"; inputFileName[11] = '1' + (testCase - 1); freopen(inputFileName, "r", stdin); printf("#%d\n", testCase); #endif scanf("%d %s", &numOfBeads, necklace); int maxBeadsCollected = 0; int beadsCollected = 0; int lastbead = numOfBeads - 1; int bead; for (bead = 0; bead < lastbead ; bead++){ //主循环:往两个方向收集珠子,断点在bead和bead+1之间 beadsCollected = collect(bead, -1) + collect(bead + 1, 1); if (beadsCollected > maxBeadsCollected) maxBeadsCollected = beadsCollected; } if (maxBeadsCollected > numOfBeads) maxBeadsCollected = numOfBeads; printf("%d\n", maxBeadsCollected); #if DEBUG } #endif return 0; }
相关文章推荐
- Transformation 能将 Windows XP/Server 2003 操作系统,完美地模拟成 Windows Vista 的软件
- 用javascript和css模拟select的脚本
- PHP模拟asp.net的StringBuilder类实现方法
- php提示Failed to write session data错误的解决方法
- PHP5.5和之前的版本empty函数的不同之处
- javascript用层模拟可移动的小窗口
- PHP has encountered a Stack overflow问题解决方法
- 自编jQuery插件实现模拟alert和confirm
- PHP模拟asp中response类实现方法
- php 生成WML页面方法详解
- javascript 模拟点击广告
- JQuery中模拟image的ajaxPrefilter与ajaxTransport处理
- PHP过滤★等特殊符号的正则
- php中文字母数字验证码实现代码
- php实现模拟post请求用法实例
- JavaScript实现MIPS乘法模拟的方法
- 模拟xcopy的函数
- php模拟服务器实现autoindex效果的方法
- C# SendInput 模拟鼠标操作的实现方法
- PHP模拟登陆163邮箱发邮件及获取通讯录列表的方法