【算法渣渣的逆袭之路】summer training warmming up,暑假的集训要开始啦,先来几道题热热身
来源:互联网 发布:南朝 知乎 编辑:程序博客网 时间:2024/05/29 11:05
这次的热身赛一共有11道题,作为一个算法新手我还是机智的A了三道题,先来鼓励一下自己,么么哒!
下面贴出这三道题的题解和我的想法:
Problem 1: FZU-2146-Easy Game
问题描述
Fat brother and Maze are playing a kind of special (hentai) game on a string S. Now they would like to count the length of this string. But as both Fat brother and Maze are programmers, they can recognize only two numbers 0 and 1. So instead of judging the length of this string, they decide to judge weather this number is even.
输入
The first line of the date is an integer T, which is the number of the text cases.
Then T cases follow, each case contains a line describe the string S in the treasure map. Not that S only contains lower case letters.
1 <= T <= 100, the length of the string is less than 10086
输出
For each case, output the case number first, and then output “Odd” if the length of S is odd, otherwise just output “Even”.
样例输入
4
well
thisisthesimplest
problem
inthiscontest
样例输出
Case 1: Even
Case 2: Odd
Case 3: Odd
Case 4: Odd
下面贴出代码,这道题呢就是判断输入的字符串是奇数还是偶数,我在之前犯了一个错误就是用了gets()函数,导致了输出的问题,现在觉得在涉及字符串的输入时用string比用字符数组要好。
#include <iostream> #include <string.h>const int N = 10100;const char sign[2][N] = {"Even", "Odd"};int main () { char str[N]; int cas; scanf("%d", &cas); for (int i = 1; i <= cas; i++) { scanf("%s", str); int len = strlen(str); printf("Case %d: %s\n", i, sign[len % 2]); } return 0;}
Problem 2:FZU-2147 -A-B Game
问题描述
Fat brother and Maze are playing a kind of special (hentai) game by two integers A and B. First Fat brother write an integer A on a white paper and then Maze start to change this integer. Every time Maze can select an integer x between 1 and A-1 then change A into A-(A%x). The game ends when this integer is less than or equals to B. Here is the problem, at least how many times Maze needs to perform to end this special (hentai) game.
输入
The first line of the date is an integer T, which is the number of the text cases.
Then T cases follow, each case contains two integers A and B described above.
1 <= T <=100, 2 <= B < A < 100861008610086
输出
For each case, output the case number first, and then output an integer describes the number of times Maze needs to perform. See the sample input and output for more details.
样例输入
2
5 3
10086 110
样例输出
Case 1: 1
Case 2: 7
#include <iostream>using namespace std;int main(int argc, char *argv[]){ int t; long long a,b; cin>>t; int c=t; while(t--) { int i=0; cin>>a>>b; while(a>b) { a=a-a%(a/2+1); i++; } printf("Case %d: %d\n",c-t,i); } return 0;}
这道题呢就是让A-(A%x)尽可能的小,所以可以用最少的次数使它小于B,于是问题就变成A%X要尽可能的大,这里我们会发现如果是X取A/2+1的话那么A%X会取得最大值,同时我们也可以写一个小程序,改变对A的赋值,观察A%X的结果。在每次循环时加一个计数器,我们就可以轻松A了。
Problem 3: FZU-2151-OOXX Game
问题描述
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, there are N*M coins in this board with two symbol “O” or “X”. Then they take turns to choose a grid with symbol “O” and change it into “X”. The game ends when all the symbols in the board are “X”, and the one who cannot play in his (her) turns loses the game. Fat brother and Maze like this kind of OOXX game very much and play it day and night. They don’t even need a little rest after each game!
Here’s the problem: Who will win the game if both use the best strategy? You can assume that Maze always goes first.
输入
The first line of the date is an integer T, which is the number of the text cases.
Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the state of the board.
1 <= T <=100, 1 <= n <=100, 1 <= m <=100
输出
For each case, output the case number first, and then output the winner’s name, either Fat brother or Maze. See the sample input and output for more details.这里写代码片
样例输入
3
1 4
OXXX
2 4
OOXX
OOXX
1 2
XX
样例输出
Case 1: Maze
Case 2: Fat brother
Case 3: Fat brother
先来贴一段错误代码,错误代码的原因是我套了dfs的模板,提交之后发现是WA
#include <stdio.h>#define N 100char map[N][N];int t,m,n;int dfs(int x,int y){ int d=0; if(x<0||x>=n||y<0||y>=m||map[x][y]=='X') return 0; if(map[x][y]='O') { d=1; map[x][y]='X'; } return dfs(x+1,y)+dfs(x,y+1)+dfs(x-1,y)+dfs(x,y-1)+d; } int main(int argc, char *argv[]){ scanf("%d",&t); int a=t; while(t--) { scanf("%d%d",&n,&m); int ans=0; for(int i=0;i<n;i++) scanf("%s",map[i]); for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(map[i][j]=='O') ans=dfs(i,j); if(ans%2==1) printf("Case %d: Maze\n",a-t); else printf("Case %d: Fat brother\n",a-t); } return 0;}
苦苦思索半天之后才发现这个模板只适用连通的节点,如果输入为
OOOO
XXXX
OOOO
那么程序就无法给出正确结果了,因为中间是断路,这个时候才发现直接用暴力解题就行了,下面贴出正确代码:
#include <iostream> #include <string.h>const int N = 105;const char sign[2][N] = {"Fat brother", "Maze"};int main () { int cas, l, r; char str[N]; scanf("%d", &cas); for (int i = 1; i <= cas; i++) { int ans = 0; scanf("%d%d", &r, &l); for (int j = 0; j < r; j++) { scanf("%s", str); for (int k = 0; k < l; k++) if (str[k] == 'O') ans++; } printf("Case %d: %s\n", i, sign[ans % 2]); } return 0;}
这样就能顺利A了这道题了!这是我在CSDN的第一篇文章,以后也会陆续的分享我的ACM生涯,虽然不知道这篇文章会有几个人看到,但是我会坚持更新的!
- 【算法渣渣的逆袭之路】summer training warmming up,暑假的集训要开始啦,先来几道题热热身
- 【算法渣渣的逆袭之路】summer training warmming up,暑假的集训要开始啦,先来几道题热热身
- 愉快的暑假集训开始了!!!
- 暑假集训之开始学习
- 我的网站之路开始啦!
- 暑假集训-WHUST 2015 Summer Contest #0.1
- 暑假集训-WHUST 2015 Summer Contest #0.2
- 暑假开始啦
- 暑假项目开始啦!!!
- 暑假正式开始啦!
- 暑假集训第1天链表-数据结构实验之链表三:链表的逆置
- 现在要开始我的ACM之旅啦!
- 我的android学习之路从今天开始啦!
- 我的编程之路从现在开始啦~~~
- 文章标题 我的coding之路开始啦
- 开始我的博客之旅啦
- hdu-暑假集训-Time To Get Up
- 缤纷的暑假-----华信教育集训
- iOS学习(一)@property 修饰符
- Atom下 Emmet 插件使用的简单指南
- DFS poj2488 A Knight's Journey
- GRE写作必备句型
- 记录下今天调试USB读取FIFO函数遇到的问题
- 【算法渣渣的逆袭之路】summer training warmming up,暑假的集训要开始啦,先来几道题热热身
- 根据值 设置spinner默认选中
- HTML5学习笔记简明版(2):新元素之section,article,aside
- 数据结构概述
- 实现背景透明,文字不透明,兼容所有浏览器
- Java关键字abstract使用总结
- iOS开发 偷懒小技巧---自定义XCode代码片段
- java中Date,String,timestamp转换问题
- OC_类和对象