文章目录
Kickdown UVA - 1588
问题描述
代码&注释
/*
Dreams never shine!
It's you that shine while chasing your dreams :)
JAYO!!
*/
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int MAXN = 100 + 1;
int main()
{
string sec1, sec2;
int H[MAXN*3], b1 = MAXN, b2; // 100 100 100 总计300的可能范围,
// 以sec1为参照, sec2可以在sec1前100的区间,或者在sec1后100的区间,两者可能区间最大长度为300
while(cin>>sec1>>sec2){
memset(H, 0, sizeof(H));
int len1 = sec1.size(), len2 = sec2.size();
for(int i = 0; i < len1; i++)
H[b1+i] = sec1[i] - '0'; // sec1起始点
int ans = len1 + len2; // 下面的b2为sec2的起始点
for(b2 = b1 - len2; b2 <= b1 + len1; b2++) { // b1 = MAXN, sec1可能在sec2前, 或者后, 或者两者重合
bool ok = true;
for(int i = 0; i < len2; i++) {
if(sec2[i] - '0' + H[b2+i] > 3) { ok = false; break; }
}
if(ok) ans = min(ans, max(b2+len2, b1+len1) - min(b1,b2)); // b2+len2为 sec2终点,
// b1+len1为sec1终点, min(b1,b2)为两者起点最小值,相当于取二者并集
}
cout<<ans<<endl;
}
return 0;
}
UVA11809 Floating-Point Numbers
问题描述
代码&注释
// P25 算法竞赛入门经典 习题与解答
/*
Dreams never shine!
It's you that shine while chasing your dreams :)
JAYO!!
*/
#include <cstdio>
#include <cmath>
#include <cstring>
// #define _rep(i,a,b) for( int i=(a); i<=(b); ++i)
#define _rep(i, a, b) for(int i = (a); i <= (b); ++i)
using namespace std;
const double EPS = 1e-6;
int main() {
char line[256];
double lg2 = log10(2), A, v;
int B; // A * 10^B, A 为浮点数,根据公式遍历出M, E
while (scanf("%s", line) == 1 && strcmp(line, "0e0") != 0) {
*strchr(line, 'e') = ' '; // 找到e并替换为空格
sscanf(line, "%lf%d", &A, &B);
v = log10(A) + B;
_rep(M, 1, 10) {
int E = round(log10((v + M * lg2 - log10(pow(2, M) - 1)) / lg2 + 1) / lg2);
if (fabs(((1 << E) - 1) * lg2 + log10(pow(2, M) - 1) - M * lg2 - v) <= EPS) {
printf("%d %d\n", M - 1, E);
break;
}
}
}
return 0;
}
知识点 *strchr
C 库函数 char *strchr(const char *str, int c)
在参数 str 所指向的字符串中搜索第一次出现
字符 c(一个无符号字符)的位置。
sscanf函数
C 库函数 int sscanf(const char *str, const char *format, ...)
从字符串读取格式化输入。
round()
round() function is a library function of cmath header,
it is used to round the given value
that is nearest to the number with halfway cases
rounded away from zero, it accepts a number
and returns rounded value.
round()函数是cmath标头的库函数,
用于对最接近该数字的给定值进行四舍五入,
一半的情况下舍入为零,它接受一个数字并返回四舍五入的值。
总结
1588要考虑sec1和sec2多种相对位置(要考虑全面)
考虑全面全靠踩坑练习!
做学问要仔细!!!
注意=和==的区别!!!