C语言 找出任何数字的第一位和最后一位数字之和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3540440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding out the sum of first and the last digit of any number
提问by shujaat
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.
我想编写一个程序来找出通过键盘输入的任何数字的第一位和最后一位数字的总和。例如,我输入了 52264。输出必须是5+4 = 9.
Yes, this is an assignment.
是的,这是一个任务。
回答by Dan Tao
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;
}
So, with 52264 for example:
因此,以 52264 为例:
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
回答by Mahmoud Younes
int n;
cin >> n;
int num1 = n % 10;
int num2 = n;
while (num2 >= 10)
num2 /= 10;
cout << num1+num2 << endl;
回答by Edward Leno
A different approach would be to treat the input as a string.
另一种方法是将输入视为字符串。
char buf[BUFSIZ];
char *p;
char bufTemp1[2];
char bufTemp2[2];
int sum;
fgets(buf, sizeof(buf), stdin);
if ((p = strchr (buf, '\n')) != NULL)
{
*p = '#include <iostream>
using namespace std;
int main()
{
int sum=0;
int first,last;
int n;
cin>>n;
first=n %10;
while (n!=0)
{
last=n/10;
}
sum=first+last;
cout<<sum<<endl;
}
';
}
bufTemp1[0] = buf[0];
bufTemp1[1] = 'uint32_t digitCount = number == 0 ? 0 : (uint32_t)(log10(abs(number))) + 1;
';
strncpy (bufTemp2, &buf[strlen(buf)-1], 1);
bufTemp2[1] = '#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 256
int main () {
char number[MAX_LENGTH];
int first, last, sum;
while (scanf("%s", number) == 1 ) {
first = number[0];
last = number[strlen(number)-1];
sum = atoi(&first) + atoi(&last);
printf("Sum = %d\n", sum);
}
return 0;
}
';
sum = atoi (bufTemp1) + atoi (bufTemp2);
回答by dato datuashvili
123456
Sum = 7
845731
Sum = 9
35846
Sum = 9
23
Sum = 5
11
Sum = 2
回答by void-pointer
A much more efficient way to go about doing this would be to find the number of digits beforehand, like so:
一种更有效的方法是事先找到位数,如下所示:
/*program to find the sum of first and last digits of a given number */
#include <stdio.h>
int main(void)
{
int firstdigit,lastdigit,number,sum;
printf("Enter the number whose sum of first and last digit to be found: \n\n");
scanf("%d",&number);
if (number >=10)
{
firstdigit = number;
while (firstdigit >= 10)
firstdigit /=10; /*firstdigit=firstdigit/10 which will neglect mantissa part;*/
lastdigit = number % 10;
sum = firstdigit + lastdigit;
printf("The sum of first=%d and last=%d digit is %d\n",firstdigit,lastdigit,sum);
}
else
{
printf("wrong input given\n");
}
return 0;
}
From there, the process of finding the first and last digits is simplified.
从那里,找到第一个和最后一个数字的过程被简化了。
回答by Mahmoud
This should work. Since the input is an array you just need to to look at the zero element and the last one, then sum them up.
这应该有效。由于输入是一个数组,您只需要查看零元素和最后一个元素,然后将它们相加即可。
printf("%d\n", input[0] - '0' + input[strlen(input)-1] - '0');
Here's a sample run:
这是一个示例运行:
b = int(raw_input())
c = str(b)
d = int(c[0])
e = int(c[-1])
print d+e
回答by abrahamj
here is the program:
这是程序:
int temp = number;
int n = 0;
while(temp > 0)
{
temp /= 10;
n++;
}
回答by Yves Daoust
sum = number / (int)pow(10, n - 1) + number % 10;
回答by Anshul Garg
回答by Hamid Nazari
Find out total number of digits in the entered number, suppose it's n.
找出输入数字的总位数,假设它是n。
##代码##Then
然后
##代码##
