今天上午没什么事,布置了三道输入输出水题,一个月的复习之后成功对字符串读入陌生。
第三题老是TLE,暴力之后图简单想减枝过,然而GG,后推导公式一层循环过了。如下。
HDU-2058
Problem Description
Given a sequence 1,2,3,……N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M.Input
Input contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0.Output
For each test case, print all the possible sub-sequence that its sum is M.The format is show in the sample below.print a blank line after each test case.Sample Input
20 10
50 30
0 0Sample Output
[1,4]
[10,10][4,8]
[6,9]
[9,11]
[30,30]
暴力不过就推公式,另所求区间为[ i, i+len ],则可用m和len来推出i,判断i是否为整。
满足输出,len的枚举从大开始。
#include<cstdio>
#include<cmath>
int main()
{
int n, m;
while(scanf("%d%d",&n, &m)!=EOF)
{
if(n+m == 0)
break;
int len = sqrt(2*m)+5;
if(len>n+1)
len = n+1;
for(; len>=0; len--)
{
double temp =double(m)/(len+1)-double(len)/2;
if(temp>0 && temp+len<=n && temp == floor(temp))
{
int ans = floor(temp);
printf("[%d,%d]\n",ans, ans+len);
}
}
printf("\n");
}
return 0;
}
接下来定时赛,两道较为复杂的模拟,黑白棋和象棋(Uva220,Uva201)均没写出,其中黑白棋源码L和M有问题,代码冗长不贴了。
接下来练习一些水题。
Uva1399
有关字母表映射,题目提取出来后知,只需要重复次数相同的字母数目也相同即可,也即分别用cnt1,cnt2 记录string的各字母出现次数,将cnt数组排序比较即可。如下。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
char a[105], b[105];
while(scanf("%s%s",a,b)!=EOF)
{
int cnt1[26], cnt2[26];
memset(cnt1,0,sizeof(cnt1));
memset(cnt2,0,sizeof(cnt2));
int len = strlen(a);
for(char i = 'A'; i<='Z'; i++)
{
for(int j = 0; j<len; j++)
{
if(i == a[j])
cnt1[i-'A']++;
}
}
for(char i = 'A'; i<='Z'; i++)
{
for(int j = 0; j<len; j++)
{
if(i == b[j])
cnt2[i-'A']++;
}
}
sort(cnt1, cnt1+26);
sort(cnt2, cnt2+26);
int flag = 1;
for(int i = 0; i<26; i++)
{
if(cnt1[i] == cnt2[i])
continue;
else
{
flag = 0;
break;
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
以及吊死鬼题目
Uva489
思路:用table记录答案各字母出现次数,将猜的内容进行比对即可,此题有坑:重复猜不算错,即猜过a之后再猜a不算加一笔。如下。并不需要那些复杂的状态记录。
#include<cstdio>
#include<cstring>
int main()
{
int table[27], rnd;
char a[26], b[26];
while(scanf("%d",&rnd) == 1 && rnd != -1)
{
scanf("%s%s",a,b);
memset(table, 0, sizeof(table));
int len = strlen(a);
for(int i = 0; i<len; i++)
{
table[a[i]-'a']++;
}
int cnt = 0;
for(int i = 0; i<strlen(b); i++)
{
if(table[b[i]-'a'] == 0)
{
cnt++;
table[b[i]-'a'] = -1;
}
if(table[b[i]-'a'] > 0)
{
len -= table[b[i]-'a'];
table[b[i]-'a'] = -1;
}
if(cnt >= 7)
{
printf("Round %d\nYou lose.\n",rnd);
goto bkpt;
}
if(len == 0)
{
printf("Round %d\nYou win.\n",rnd);
goto bkpt;
}
}
printf("Round %d\nYou chickened out.\n",rnd);
bkpt:;
}
return 0;
}
今天并不顺,因为有些生疏了,几天的通宵之后智商也有所衰减。
PS 写了黑白棋200+line,发现智商真的捉急。
以上。