POJ2602《Superlong sums》方法:高精度 模拟
2013-02-25 10:15
281 查看
题目比较拗口的大数相加,两个数居然分开一个数字一个数字读入,注意要用scanf()读入,cin因为重载,没有指定格式,输入比较耗时。同时输出也要先转换为字符串形式,不然循环输出整数数组会超时。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int n; // 输入长度 int main() { //freopen("temp.txt", "r", stdin); cin >> n; int *a = new int ; int *b = new int ; int *ans = new int ; char *ansc = new char[n+1]; for (int i = n-1; i >= 0; --i) scanf("%d %d", &a[i], &b[i]); memset(ans, 0, sizeof(ans) * n); //当ans为int ans 时,用sizeof是不同的 int carry = 0; for (int i = 0; i < n; ++i) { //这题居然结果与输入结果长度一致 ans[i] += a[i] + b[i] + carry; carry = ans[i] / 10; ans[i] = ans[i] % 10; } for (int i = 0; i < n; ++i) // 这里不能直接循环输出ans,会超时 ansc[i] = ans[n-1-i] + '0'; ansc = '\0'; cout << ansc << endl; return 0; }
相关文章推荐
- POJ 2602 Superlong sums (高精度,模拟,水题)
- poj2602 Superlong sums(高精度)
- (高精度运算4.7.24)UVA 10013 Super long sums(大数加法——某一位的数字可能大于10)
- POJ 2602|URAL 1048|Superlong Sums|高精度加法
- (高精度运算4.7.24)UVA 10013 Super long sums(大数加法——某一位的数字可能大于10)
- POJ 2602 Superlong sums(高精度)
- POJ 2602 Superlong sums(模拟大数加法)
- ural 1048. Superlong Sums 模拟
- UVa 10013 Super long sums (简单高精度)
- POJ1503《Integer Inquiry》方法:模拟 高精度
- POJ 2602 Superlong sums G++
- POJ 2602 Superlong sums
- UVa 10013 - Super long sums
- POJ1001《Exponentiation》方法:模拟 高精度
- POJ 2602 Superlong sums
- poj 2602 Superlong sums
- poj 大数 - 2602 Superlong sums
- poj-2602-Superlong sums
- ACM篇:POJ2602--Superlong Sums
- Poj2602 Superlong sums