抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)
数字的反转:
就是将数字倒着存下来而已。(*^__^*) 嘻嘻……
大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出。
详见代码。
while (a) //将每位数字取出来,取完为止
{
num1[i]=a%; //将每一个各位取出存在数组里面,实现了将数字反转
i++; //数组的变化
a/=;
}
趁热打铁 例题:hdu 4554 叛逆的小明
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4554
叛逆的小明
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 818 Accepted Submission(s):
568
小明会把1234它看成4321;把-1234看成-4321;把230看成032
(032=32);把-230看成-032(-032=-32)。
现在,小明做了一些a+b和a-b的题目(a,
b为整数且不含前导0),如果给你这些题目正确的答案,你能猜出小明会做得到什么答案吗?
接下来T行,每行是两个整数x,y(-1000000<=x,
y<=1000000), x表示a+b的正确答案,y表示a-b的正确答案。
输入保证合法,且不需考虑a或b是小数的情况。
t,之间用一个空格分开,其中s表示小明将得到的a+b答案,t表示小明将得到的a-b答案。
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int num1[],num2[]; int main ()
{
int T;
scanf("%d",&T);
while (T--)
{
int x,y,a,b,i=,j=;
int p=,q=;
scanf("%d%d",&x,&y);
memset(num1,,sizeof(num1));
memset(num2,,sizeof(num2));
a=(x+y)/;
b=x-a;
while (a)
{
num1[i]=a%;
i++;
a/=;
}
while (b)
{
num2[j]=b%;
j++;
b/=;
}
for (int k=; k<i; k++) //换成数字
p=num1[k]+p*;
for (int l=; l<j; l++)
q=num2[l]+q*;
printf ("%d %d\n",p+q,p-q);
}
return ;
}
大数加法:
数字很大,假如直接加。很容易超时!所以换一种方法就是直接取出来一位一位加,不过不能换成数字。
int l=l1>l2?l1:l2; //在两个之间取一个最长的
for (int i=; i<l; i++)
{
num3[i]=num1[i]+num2[i]; //一位一位加
if (num3[i-]>&&i>=) //考虑进位的问题,如果大于9就需要进位
{
num3[i]++;
} }
if (num[l-]>) //最后一位的进位问题
{
num[l]++; //仍需要进位
l++; //长度需要加一
}
例题:hdu 1002 A + B Problem II
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 238717 Accepted Submission(s):
46010
integers A and B, your job is to calculate the Sum of A + B.
T(1<=T<=20) which means the number of test cases. Then T lines follow,
each line consists of two positive integers, A and B. Notice that the integers
are very large, that means you should not process them by using 32-bit integer.
You may assume the length of each integer will not exceed 1000.
first line is "Case #:", # means the number of the test case. The second line is
the an equation "A + B = Sum", Sum means the result of A + B. Note there are
some spaces int the equation. Output a blank line between two test
cases.
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int main ()
{
int n,flag=;
char a[],b[];
int num1[],num2[],ans[];
scanf("%d",&n);
while (n--)
{
int k=,s=;
scanf("%s%s",a,b);
printf ("Case %d:\n",flag++);
int len1=strlen(a);
int len2=strlen(b);
memset(num1,,sizeof(num1));
memset(num2,,sizeof(num2));
memset(ans,,sizeof(ans));
int l=len1>len2?len1:len2;
for (int i=len1-; i>=; i--)
{
num1[k]=a[i]-'';
k++;
}
for (int j=len2-; j>=; j--)
{
num2[s]=b[j]-'';
s++;
}
for (int i=; i<l; i++)
{
ans[i]=num1[i]+num2[i];
if (i>=)
if (ans[i-]>)
{
ans[i]++;
}
//cout<<num1[i]<<" "<<num2[i]<<" "<<ans[i]<<endl;
}
if (ans[l-]>)
{
ans[l]=;
l++;
}
printf ("%s + %s = ",a,b);
for (int i=l-; i>=; i--)
printf("%d",ans[i]%);
if (n)
printf ("\n");
printf ("\n");
}
return ;
}
抓起根本(二)(hdu 4554 叛逆的小明 hdu 1002 A + B Problem II,数字的转化(反转),大数的加法......)的更多相关文章
- HDU 4554 叛逆的小明
叛逆的小明 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submiss ...
- HDU 1002 A + B Problem II(高精度加法(C++/Java))
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1002.A + B Problem II 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdi ...
- 大数加法~HDU 1002 A + B Problem II
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 题意: 数学题,A+B; 思路,这个数非常大,普通加法一定会超时,所以用大数加法.大数加法的基 ...
- HDU 1002 - A + B Problem II - [高精度]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 Problem DescriptionI have a very simple problem ...
- HDU 1002 A + B Problem II
A + B Problem II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted ...
- HDU 1002 A + B Problem II(AC代码)
#include <stdio.h> #include <string.h> #define MAX 1009 int main() { },b[MAX]={}; ,z=,r= ...
- HDU 1002 A + B Problem II(大整数相加)
A + B Problem II Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- hdu 1002 A + B Problem II【大数加法】
题目链接>>>>>> 题目大意:手动模拟大数加法,从而进行两个大数的加法运算 #include <stdio.h> #include <strin ...
随机推荐
- 【week5】psp
本周psp psp饼图: 随笔字数折线图: 代码行折线图:
- Thinkphp5的使用phpmailer实现发邮件功能(163邮箱)
Thinkphp5本身并没有实现发邮件的功能,至少据我所知. 本文利用网易邮箱作为发邮件的邮箱.作为发送邮件的前提是需要开启SMTP服务,打开网易邮件,点击设置按钮,如下图所示 勾选smtp服务 保存 ...
- Innodb 中 RR 隔离级别能否防止幻读?
问题引出 我之前的一篇博客 数据库并发不一致分析 有提到过事务隔离级别以及相应加锁方式.能够解决的并发问题. 标准情况下,在 RR(Repeatable Read) 隔离级别下能解决不可重复读(当行修 ...
- Nautilus-Share-Message: Called "net usershare info" but it failed: Failed to
See what nautilus processes are running : ps aux | grep nautilus Kill all nautilus processes you see ...
- phpcms找不到模板、空白模板的解决办法
有时候会出现这个问题,尤其是在调试模板的过程中,不知道别的朋友的操作习惯.我的习惯是,先保留一份原始的模板.比如: index原版.html. 以便对比之用.但是呢,这样,往往会造成 找不到模板的情况 ...
- C跟C++
工作以来几乎就只写C,下周开始的新工作可能就要开始写C++啦~ C: C++: 1.template 2.多态.重载.继承 3.虚函数 C++内存布局 看两张耗子的图片就清楚了.其中虚函数表一般放在数 ...
- 配置bond和vlan
网卡是光口还是电口的方法ethtool 网卡名字 一看速度二看port是否是firber首先查看需要做bond的物理网卡,如enp130s0f0,enp131s0f0以物理网卡为enp130s0f0, ...
- 用select模拟一个socket server成型版2
1.字典队列测试 import queue msg_dic={} msg_dic[1]=queue.Queue() msg_dic[1].put('hello') msg_dic[1].put('bo ...
- [清华集训2017]无限之环(infinityloop)
description 题面 solution 一开始的思路是插头\(DP\),然而复杂度太高 考虑将网格图黑白染色后跑费用流 流量为接口数,费用为操作次数 把一个方格拆成五个点,如何连边请自行脑补 ...
- [SDOI2014]数表 莫比乌斯反演
---题面--- 题解: 设$f(d)$表示数$d$的约数和,那么$(i, j)$中的数为$f(gcd(i, j))$,那么有2种枚举方法.1,枚举每一格看对应的$f(d)$是几.$$ans = \s ...