hdu_1013_A + B Problem II_(模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
解题思路:利用数组进行大数的相加。内置数据类型不能满足整数位数的要求。
样例:
<span style="white-space:pre">	</span>1 999
<span style="white-space:pre">	</span>999 1
#include <iostream>
#include <cstring>
using namespace std;
int num1[1010],num2[1010];
int t[1010];
int result[1010];
char str1[1010],str2[1010];

void add(int len1,int len2)
{
	int i,j;
	//字符转换成数字,再反转
	for(i = 0;i < len1;i++)
		t[i] = str1[i] - 48;
	for(i = len1 - 1,j = 0;i >= 0;i--,j++)
		num1[j] = t[i];

	for(i = 0;i < len2;i++)
		t[i] = str2[i] - 48;
	for(i = len2 - 1,j = 0;i >= 0;i--,j++)
		num2[j] = t[i];

	int k = 0,temp,cnt = 0;
	i = 0;
	while(i < len1 || i < len2)
	{
		temp = num1[i] + num2[i] + cnt;//cnt表示进位
		if(temp >= 10)
		{
			cnt = 1;
			result[i] = temp % 10;
		}
		else
		{
			result[k] = temp;
			cnt = 0;
		}
		i++;
		k++;
	}
	result[k] = cnt;//假如是99+99,则最后的进位1要记录下来 

	for(i = 0;i < len1;i++)
		cout << str1[i];
	cout << " + ";
	for(i = 0;i < len2;i++)
		cout << str2[i];
	cout << " = ";	

	if(result[k] != 0)
	   	   cout << result[k];
	int ii;
	for(ii = k-1;ii >= 0;ii--)
	{
	   	cout << result[ii];
	}
	cout << endl;
}
int main(int argc, char *argv[])
{
	int s,cnt = 1;
	cin >> s;
	int a = s;
	while(s--)
	{
		memset(num1,0,sizeof(num1));
		memset(num2,0,sizeof(num2));//每次都要初始化num1和num2 的数组,没有则会WA
		memset(result,0,sizeof(result));
		cin >> str1 >> str2;
		cout << "Case " << cnt << ":" << endl;
		add(strlen(str1),strlen(str2));
		if(cnt < a) //最后的样例没有空行
			cout << endl;
		cnt++;
	}
	return 0;
}

时间: 2024-08-25 04:35:18

hdu_1013_A + B Problem II_(模拟)的相关文章

HDU 1022 Train Problem I 模拟栈题解

火车进站,模拟一个栈的操作,额外的栈操作,查看是否能按照规定顺序出栈. 数据量很少,故此题目很容易AC. 直接使用数组模拟就好. #include <stdio.h> const int MAX_N = 10; char inOrder[MAX_N], outOrder[MAX_N], stk[MAX_N]; bool rs[MAX_N<<2]; int n; int main() { while (scanf("%d", &n) != EOF) { s

HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 312    Accepted Submission(s): 219 Problem Description One day, you, a clever boy, feel bored in your math class, and then fall

G - Train Problem I(模拟题)

Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a proble

HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))

Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special Judge Problem Description Everyone hates ugly problems. You are given a positive integer. You m

uva 101 The Blocks Problem (模拟)

uva 101  The Blocks Problem Background Many areas of Computer Science use simple, abstract domains for both analytical and empirical studies. For example, an early AI study of planning and robotics (STRIPS) used a block world in which a robot arm per

csu 1549: Navigition Problem(几何,模拟)

1549: Navigition Problem Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 305  Solved: 90[Submit][Status][Web Board] Description Navigation is a field of study that focuses on the process of monitoring and controlling the movement of a craft or vehicle

HDU 4716 A Computer Graphics Problem(模拟啊 )

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4716 Problem Description In this problem we talk about the study of Computer Graphics. Of course, this is very, very hard. We have designed a new mobile phone, your task is to write a interface to displa

HDU 1032 [The 3n + 1 problem] 暴力模拟

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 题目大意:给出i,j,要求输出i j之间"3N+1"的循环次数的最大值. 关键思想:暴力,模拟.可以优化,因为某些大数在进行操作时,会变为已经求过的小数,之后的循环次数已求过. 代码如下: //between i,j 模拟,暴力 #include <iostream> #include <algorithm> using namespace std; int

hdoj 2522 A simple problem 【模拟】

题意:算出1/n的结果,循环小数只输出第一个循环节 策略:模拟1除去n即可. 判断是否是循环节只需要找到%n之后的模是否出现就好了. 代码: #include <stdio.h> #include <string.h> #define M 100005 bool vis[M]; int main(){ int t, n; scanf("%d", &t); while(t --){ scanf("%d", &n); if(n =