hdu 3925 一道复杂的简单题

题目意思简单,思路也挺简单的,可是打代码可不容易,我已经WA一下午了,至今没有AC,希望路过的大牛帮忙测测,将测试数据发给我,我将感激不尽……

/*
* hdu3925/win.cpp
* Created on: 2011-9-20
* Author : ben
*/
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<cmath>
using namespace std;

const int MAX = 100000000;

int ans;
char stra[200], strb[20];
bool carry[200];
int lenb, lena;

void work();
int main() {
#ifndef ONLINE_JUDGE
freopen(
"data2.in", "r", stdin);
#endif
work();
return 0;
}

void count(int s) {
int ret = 0, base;
int temp;
if (lena - lenb - s < 9) {
base = (int) pow(10.0, lena - lenb - s);
}
else {
base = MAX;
}
int e = lenb;
memset(carry,
0, sizeof(carry));
if (stra[s + e - 1] != strb[e - 1]) {
carry[e]
= true;
}
while (e > 0) {
e
--;
temp
= (strb[e] - stra[s + e] - carry[e + 1] + 10) % 10;
if (temp > 0) {
if (base >= MAX) {
return;
}
ret
+= base * temp;
if (ret > ans) {
return;
}
}
if (stra[s + e] + carry[e + 1] > strb[e]) {
carry[e]
= true;
}
if (base < MAX) {
base *= 10;
}
}
if (carry[lenb]) {
if (s + lenb == lena) {
ret
++;
}
else {
int i = lena - s;
if (i > lenb) {
i
--;
ret
+= 10 - stra[s + i] + '0';
base = 10;
}
while (i > lenb) {
i
--;
ret
+= ('9' - stra[s + i]) * base;
if (base < MAX) {
base *= 10;
}
if (ret > ans) {
return;
}
}
}
}
if (ret < ans) {
ans
= ret;
}
}

void work() {
int T;
scanf(
"%d", &T);
for (int t = 1; t <= T; t++) {
stra[
0] = '0';
scanf(
"%s %s", stra + 1, strb);
lenb
= strlen(strb);
lena
= strlen(stra);
if (lena <= lenb) {
int a, b;
sscanf(stra
+ 1, "%d", &a);
sscanf(strb,
"%d", &b);
ans
= b - a;
printf(
"Case #%d: %d\n", t, (int) ans);
continue;
}
char *p = strstr(stra + 1, strb);
if (p) {
ans
= 0;
}
else if (strcmp(strb, "0") == 0) {
ans
= 10 - stra[lena - 1] + '0';
}
else {
ans
= MAX;
for (int i = 0; i <= lena - lenb; i++) {
count(i);
}
}
printf(
"Case #%d: %d\n", t, ans);
}
}

另外,再附上一个别人过了的代码,其实思路跟我的差不太多了。不明白我的为什么还不过……

#include<cstdio>
#include
<string>
#include
<algorithm>
#include
<cmath>
using namespace std;
char stra[200], strb[20];
int main() {
int arr[101];
string str1, str2;
int T;
scanf(
"%d", &T);
for (int t = 1; t <= T; t++) {
int a = 0, b = 0;
scanf(
"%s %s", stra, strb);
str1
= string(stra);
str2
= string(strb);
int lena = str1.size();
int lenb = str2.size();
if (strstr(stra, strb)) {
printf(
"Case #%d: 0\n", t);
continue;
}
int min = 1000000000;
if (lena < lenb) {
sscanf(stra,
"%d", &a);
sscanf(strb,
"%d", &b);
printf(
"Case #%d: %d\n", t, b - a);
continue;
}
else {
str1
= "00" + str1;
while (str2.size() <= (unsigned int) (lena + 1)) {
string s = str2;
int k = str2.size() - 1, g = lena + 2;
for (int i = lena + 1; i > lena + 1 - (int)str2.size(); i--) {
--g;
if (str2[k] - str1[i] < 0) {
arr[g]
= (str2[k] - '0') + 10 - (str1[i] - '0');
str2[k
- 1]--;
}
else {
arr[g]
= str2[k] - '0' - (str1[i] - '0');
}
k
--;
if (k == -1) {
break;
}
}
while (arr[g] == 0) {
g
++;
}
if (lena + 1 - g + 1 < 9) {
int tmin = 0;
for (int i = g; i <= lena + 1; i++) {
tmin
= tmin * 10 + arr[i];
}
if (min > tmin) {
min
= tmin;
}
}
str2
= s;
str2
= str2 + '0';
}
}
printf(
"Case #%d: %d\n", t, min);
}
return 0;
}



posted @ 2011-09-20 18:04  moonbay  阅读(164)  评论(0编辑  收藏  举报