2017/8/8学习小结
Score
1585:https://vjudge.net/problem/UVA-1585
analysis
用一个标记变量symbol判断是否连续即可
code
#include<iostream>
#include<cstring>
using namespace std;
#define maxn 85
char str[maxn];
int main()
{
int n,score;
cin >> n;
while(n--)
{
score = 0;
int j = 0;
bool symbol = true;
cin >> str;
int len = strlen(str);
for(int i=0; i<len; ++i)
{
if(str[i]=='O')
{
if(symbol)
{
j++;
}else
{
j = 0;
j++;
symbol = true;
}
score += j;
}else
{
symbol = false;
}
}
cout << score <<endl;
}
return 0;
}
Digit Counting
1225:https://vjudge.net/problem/UVA-1225
analysis
直接暴力即可
code
#include<iostream>
#include<cstring>
using namespace std;
#define maxn 11
int m[maxn];
int main()
{
int n;
cin >> n;
while(n--)
{
memset(m,0,sizeof(m));
int t;
cin >> t;
for(int i=1; i<=t; ++i)
{
int tem = i;
while(tem)
{
m[tem%10]++;
tem /= 10;
}
}
// m[0]++;
for(int i=0; i<10; ++i)
{
if(i == 9)
cout << m[i] <<endl;
else
cout << m[i] <<" " ;
}
}
return 0;
}
Hangman Judge
489:https://vjudge.net/problem/UVA-489
analysis
就是代码实现能力
code
#include<cstdio>
#include<cstring>
#define maxn 100
int left,chance;
//left还需要猜left个位置
//chance表示允许错误的剩余次数
char s1[maxn],s2[maxn];
//s1答案 s2猜的
int win,lose;
void guess(char ch)
{
int len = strlen(s1);
int bad = 1;
for(int i=0; i<len; ++i)
{
if(ch == s1[i])
{
s1[i] = ' '; //猜过的将其置为空,
//以至于猜错或者猜过的都将chance--
left--;
bad = 0;
}
}
if(bad)
chance--;
if(!chance)
lose = 1;
if(!left)
win = 1;
return;
}
int main()
{
int rnd;
while(scanf("%d%s%s",&rnd,s1,s2)==3 && rnd!=-1)
{
printf("Round %d\n",rnd);
win = lose = 0;
left = strlen(s1);
chance = 7;
int len2 = strlen(s2);
for(int i=0; i<len2; ++i)
{
guess(s2[i]);
if(win || lose) //不管是赢是输就退出
break;
}
if(win)
printf("You win.\n");
else if(lose)
printf("You lose.\n");
else
printf("You chickened out.\n");
}
return 0;
}