Reversed Numbers
Reversed Numbers
//先想一個簡單的問題:如果是單純的數字倒置,要怎麼寫?
eg:輸入1234,應得到4321
可以用綜合被調實現這一點…【從後往前取餘再乘以位數】
具體程式碼如下:
#include<stdio.h>
int sub(int m)
{
int r=0;
while(m!=0)
{
r=r*10+m%10;
m=m/10;
}
return r;
}
int main()
{
int m;
scanf("%d",&m);
printf("%d\n",sub(m));
return 0;
}
再來看這個英文題:
Adding Reversed Numbers
Description
The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard because the basic sense of the play must be kept intact, although all the things change to their opposites. For example the numbers: if any number appears in the tragedy, it must be converted to its reversed form before being accepted into the comedy play. Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros. ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).
Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add. Numbers will be at most 200 characters long.
Output
For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.
Sample Input
3
24 1
4358 754
305 794
Sample Output
34
1998
1
【此題與上面那個簡單題的不同就在於這是把每一行的兩個數先倒置再相加輸出結果,所以一定不能先相加】
具體程式碼如下:
#include<stdio.h>
int sub(int sum)
{
int c=0;
while(sum)
{
c=c*10+sum%10;
sum/=10;
}
return c;
}
int main()
{
int n,sum,a,b,x,y;
scanf("%d",&n);
while(n--)
{
sum=0;
scanf("%d %d",&a,&b);
x=sub(a);
y=sub(b);
sum=x+y;
printf("%d\n",sub(sum));
}
return 0;
}
相關文章
- Python中reversed()方法如何使用?Python
- 400多種Numbers模板 DesiGN for Numbers Templates for macMac
- Collecting Numbers II
- Codeforces - Jzzhu and Numbers
- Python 中的反轉字串:reversed()、切片等Python字串
- different random numbers generatorrandom
- 【Lintcode】1267. Lexicographical Numbers
- 829. Consecutive Numbers Sum
- 165. Compare Version Numbers
- Leetcode 165 Compare Version NumbersLeetCode
- Find All Numbers Disappeared in an ArrayAPP
- LeetCode 2 Add Two NumbersLeetCode
- 201-Bitwise AND of Numbers Range
- python-reversed返回順序反轉後的迭代器Python
- CF1406E Deleting Numbers
- B. Numbers Box(思維)
- C. k-Amazing Numbers
- LeetCode-2 Add Two NumbersLeetCode
- Sum of Square Numbers 平方數之和
- Self Dividing Numbers 自除數
- Note 886532 - Pricing: Displaying and rounding numbers
- Leetcode 967 Numbers With Same Consecutive DifferencesLeetCode
- LeetCode 2. Add Two NumbersLeetCode
- 129-Sum Root to Leaf Numbers
- Add_Two_Numbers python 求解Python
- [題解]SP10606 Balanced Numbers
- LeetCode 129. Sum Root to Leaf NumbersLeetCode
- 421-Maximum XOR of Two Numbers in an Array
- 題解:UVA13185 DPA Numbers I
- CF878E Numbers on the blackboard 題解
- [題解]CF55D Beautiful Numbers
- LeetCode之Sum of Even Numbers After Queries(Kotlin)LeetCodeKotlin
- Bitcoin Node Numbers Fall After Spam Transaction "Attack"
- POJ3252Round Numbers(數位dp)
- LeetCode 448. Find All Numbers Disappeared in an ArrayLeetCodeAPP
- Solution - Atcoder Atcoder ARC137C Distinct Numbers
- React數字滾動元件 numbers-scrollReact元件
- LeetCode2: Add two numbers(兩數相加)LeetCode