hdu 3925 一道复杂的简单题
2011-09-20 18:04
246 查看
题目意思简单,思路也挺简单的,可是打代码可不容易,我已经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; }
相关文章推荐
- 一道看似复杂但是简单的c#面试题
- hdu-1597一道简单的题目主要是注意Tle
- HDU--1195:Open the Lock (一道简单的BFS)
- HDU 5762 [简单复杂度分析]
- 一道简单的BFS题 HDU上 CCNU BFS练习的1003
- HDU 5301 Buildings(思维:一道简单的难题)
- HDU 1010题解这是一道简单的DFS加回溯题,看懂后就会对递归和回溯有较深刻的理解。
- hdu 2570 //这题想到贪心法有些曲折哈 是一道简单的贪心算法(最优装载问题)
- 面试遇到的既简单又复杂的一道算法题
- 第一次用链表。一道简单的题 hdu 2019 数列有序
- hdu 1176(一道简单的dp)
- 一道简单题目的复杂算法
- 简单三招,设计复杂ERP报表
- HDU 4642 Fliping game (简单博弈)
- DataGrid实例(简单易懂,无复杂功能,适合初学者)
- Hdu 1283 最简单的计算器
- HDU 3072 Intelligence System (强连通缩点+简单最小树形图,4级)
- android中解析复杂xml(XStream简单使用)
- HDU-2044-一只小蜜蜂...(简单DP)
- Dialog用法,简单+复杂