找出第一总和和任意数量的最后一位数字 [英] Finding out the sum of first and the last digit of any number
本文介绍了找出第一总和和任意数量的最后一位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
欲写找出第一的总和,并通过键盘输入的任何数量的最后数字的程序。例如,我进入了52264.输出必须 5 + 4 = 9
。
I want to write a program for finding out the sum of first and the last digit of any number entered through keyboard. For example, I entered 52264. Output must be 5+4 = 9
.
是的,这是一个任务。
推荐答案
好吧,最后一个数字是很容易搞清楚,是吧?
Well, the last digit's easy enough to figure out, right?
int lastDigit = input % 10;
作为第一位的,我不知道最有效的方式来获取。紧随弹簧在我脑海的第一个念头是:
As for the first digit, I'm not sure about the most efficient way to get that. The first thought that immediately springs to my mind is:
int firstDigit = input;
while (firstDigit >= 10)
{
firstDigit /= 10;
}
所以,用52264例如:
So, with 52264 for example:
int lastDigit = 52264 % 10; // 52264 % 10 = 4
int firstDigit = 52264;
firstDigit /= 10; // 5226
firstDigit /= 10; // 522
firstDigit /= 10; // 52
firstDigit /= 10; // 5 -- less than 10
这篇关于找出第一总和和任意数量的最后一位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文