64位数字和(C)【随意编辑我的英文】
64 Bit digitsum (C) [Feel Free to edit my English]
这是我作业的一部分,但我不知道为什么输出不正确。有帮助吗?
/**
* Create a function called digitsum that takes an long integer (64 bits) and
* sums the value of each of its digits. i.e. the number 316 should sum to
* 3+1+6 or 10. Return the sum as the value of the function.
* Hints:
* - If the number is negative, make it positive before anything else.
* - n % 10 will give you the value of the first digit.
* - n / 10 will shift the integer to the right one digit.
* - You're done summing when n is zero.
*/
示例input/output:
* ./p2 316
* num = 316, sum = 10
* ./p2 -98374534984535
* num = -98374534984535, sum = 77
*/
#include <stdio.h>
#include <stdlib.h>
int digitsum(int n){ //first try using function
if (n % 10)
return digitsum == n % 10;
else if (n / 10)
return digitsum == n / 10;
else
return 0; //done summing when n is zero.
}
// Read down in the comments and do **NOT** modify main below.
int main(int argc, char **argv)
{
int64_t n;
if (argc < 2) {
printf("usage: %s <int>\n", argv[0]);
exit(1);
}
n = atoll(argv[1]);
printf("num = %ld, sum = %d\n", n, digitsum(n));
return 0;
}
当我用gcc编译时,它只显示输出"sum is 310"而不是"sum is 10"?我是C编程新手,还在学习中..
int digitsum(int n)
函数错误
你应该在循环中添加每个数字,如下所示 code
:
int digitsum(int64_t n){ //first try using function
int ret = 0;
if (n < 0)
n = -n;
while (n != 0) {
ret += n % 10;
n /= 10;
}
return ret; //done summing when n is zero.
}
考虑递归的基本情况,如果你的数字是 0,你应该 return 0,否则你用 n % 10
得到你的最后一个数字,然后再次调用你的函数来处理另一个除最后一位以外的数字 digitsum(n / 10)
:
long long digitsum(long long n) { // I assume n is non-negative
if (n == 0) return 0; // terminal case
return n % 10 + digitsum(n / 10);
}
这是我作业的一部分,但我不知道为什么输出不正确。有帮助吗?
/** * Create a function called digitsum that takes an long integer (64 bits) and * sums the value of each of its digits. i.e. the number 316 should sum to * 3+1+6 or 10. Return the sum as the value of the function. * Hints: * - If the number is negative, make it positive before anything else. * - n % 10 will give you the value of the first digit. * - n / 10 will shift the integer to the right one digit. * - You're done summing when n is zero. */
示例input/output:
* ./p2 316 * num = 316, sum = 10 * ./p2 -98374534984535 * num = -98374534984535, sum = 77 */
#include <stdio.h>
#include <stdlib.h>
int digitsum(int n){ //first try using function
if (n % 10)
return digitsum == n % 10;
else if (n / 10)
return digitsum == n / 10;
else
return 0; //done summing when n is zero.
}
// Read down in the comments and do **NOT** modify main below.
int main(int argc, char **argv)
{
int64_t n;
if (argc < 2) {
printf("usage: %s <int>\n", argv[0]);
exit(1);
}
n = atoll(argv[1]);
printf("num = %ld, sum = %d\n", n, digitsum(n));
return 0;
}
当我用gcc编译时,它只显示输出"sum is 310"而不是"sum is 10"?我是C编程新手,还在学习中..
int digitsum(int n)
函数错误
你应该在循环中添加每个数字,如下所示 code
:
int digitsum(int64_t n){ //first try using function
int ret = 0;
if (n < 0)
n = -n;
while (n != 0) {
ret += n % 10;
n /= 10;
}
return ret; //done summing when n is zero.
}
考虑递归的基本情况,如果你的数字是 0,你应该 return 0,否则你用 n % 10
得到你的最后一个数字,然后再次调用你的函数来处理另一个除最后一位以外的数字 digitsum(n / 10)
:
long long digitsum(long long n) { // I assume n is non-negative
if (n == 0) return 0; // terminal case
return n % 10 + digitsum(n / 10);
}