数字的反转:

就是将数字倒着存下来而已。(*^__^*) 嘻嘻……

大致思路:将数字一位一位取出来,存在一个数组里面,然后再将其变成数字,输出。

详见代码。

  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

Problem Description
叛逆期的小明什么都喜欢反着做,连看数字也是如此(负号除外),比如:
小明会把1234它看成4321;把-1234看成-4321;把230看成032
(032=32);把-230看成-032(-032=-32)。

现在,小明做了一些a+b和a-b的题目(a,
b为整数且不含前导0),如果给你这些题目正确的答案,你能猜出小明会做得到什么答案吗?

 
Input
输入第一行为一个正整数T(T<=10000),表示小明共做了T道题。
接下来T行,每行是两个整数x,y(-1000000<=x,
y<=1000000), x表示a+b的正确答案,y表示a-b的正确答案。
输入保证合法,且不需考虑a或b是小数的情况。
 
Output
输出共T行,每行输出两个整数s
t,之间用一个空格分开,其中s表示小明将得到的a+b答案,t表示小明将得到的a-b答案。
 
Sample Input
3
20 6
7 7
-100 -140
 
Sample Output
38 24
7 7
-19 -23
 
题目大意:中文题目,清晰明了~
详见代码。
 #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

Problem Description
I have a very simple problem for you. Given two
integers A and B, your job is to calculate the Sum of A + B.
 
Input
The first line of the input contains an integer
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.
 
Output
For each test case, you should output two lines. The
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.
 
Sample Input
2
1 2
112233445566778899 998877665544332211
 
Sample Output
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
 
参考以上的详解。将数字一位一位取出来,倒过来相加,在输出~

 #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,数字的转化(反转),大数的加法......)的更多相关文章

  1. HDU 4554 叛逆的小明

    叛逆的小明 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submiss ...

  2. 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) ...

  3. hdu 1002.A + B Problem II 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdi ...

  4. 大数加法~HDU 1002 A + B Problem II

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 题意: 数学题,A+B; 思路,这个数非常大,普通加法一定会超时,所以用大数加法.大数加法的基 ...

  5. HDU 1002 - A + B Problem II - [高精度]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 Problem DescriptionI have a very simple problem ...

  6. HDU 1002 A + B Problem II

    A + B Problem II   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted ...

  7. HDU 1002 A + B Problem II(AC代码)

    #include <stdio.h> #include <string.h> #define MAX 1009 int main() { },b[MAX]={}; ,z=,r= ...

  8. HDU 1002 A + B Problem II(大整数相加)

    A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  9. hdu 1002 A + B Problem II【大数加法】

    题目链接>>>>>> 题目大意:手动模拟大数加法,从而进行两个大数的加法运算 #include <stdio.h> #include <strin ...

随机推荐

  1. html超出不自动换行

    1.使用overflow: hidden把超出的内容进行隐藏: 2.然后使用white-space: nowrap设置内容不换行: 3.最后使用text-overflow: ellipsis设置超出内 ...

  2. 【bzoj2763】[JLOI2011]飞行路线 分层图最短路

    题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...

  3. [洛谷P4345][SHOI2015]超能粒子炮·改

    题目大意:给你$n,k$,求:$$\sum\limits_{i=0}^k\binom n i\pmod{2333}$$题解:令$p=2333,f(n,k)\equiv\sum\limits_{i=0} ...

  4. BZOJ5286:[HNOI/AHOI2018]转盘——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5286 https://www.luogu.org/problemnew/show/P4425 ht ...

  5. BZOJ1026:[SCOI2009]windy数——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1026 Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2 ...

  6. C++中的显示类型转换

    本文参考了<C++ Primer(中文 第5版)>.<王道程序员求职宝典>以及网上相关博客,结合自己的理解写成.个人水平有限,若有错误欢迎指出. C++中显示转换也成为强制类型 ...

  7. 洛谷 U32911 道路维护 解题报告

    U32911 道路维护 题目背景 最近很多人投诉说C国的道路破损程度太大,以至于无法通行. C国的政府很重视这件事,但是最近财政有点紧,不可能将所有的道路都进行维护,所以他们决定按照下述方案进行维护. ...

  8. HDOJ(HDU).1864 最大报销额 (贪心)

    HDOJ(HDU).1864 最大报销额 题意分析 题目有点问题,原题中说的 单项物品的价值不得超过600元 应该是单类物品的价值不能超过600元. 一开始以为是01背包,后来按贪心写过了. 一张一张 ...

  9. ES6 Set,WeakSet,Map,WeakMap

    1. Set Set是一个集合,里面的值都是唯一的,没有重复的.Set中可以是任何数据类型,并且添加数据时会进行严格比较,重复数据无法加入. 2. WeakSet 弱引用Set.只能存储对象,不能存储 ...

  10. linux 使用vim文件加密/解密的方法

    一. 利用 vim/vi 加密:优点:加密后,如果不知道密码,就看不到明文,包括root用户也看不了:缺点:很明显让别人知道加密了,容易让别人把加密的文件破坏掉,包括内容破坏和删除: vi编辑器相信大 ...