如何解决c中的五位数的数字总和?您能发现错误吗?
顺便说一句,我知道这不是最有效的方法,但是如果我想像以前那样做,那我做错了什么?任务是:给出一个五位数的整数,打印其位数的总和。
约束:10000
输入样本:10564
样本输出:16
我的代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,sum;
int remainder_array[4] = { n % 1000; n % 100,n % 10,n };
int digits_array[4];
scanf("%d",&n);
// Complete the code to calculate the sum of the five digits on n.
if (10000 <= n && n <= 99999) {
else if (remainder_array[0] = 0) {
digits_array[0] = (n - remainder_array[0]) / 1000;
n = remainder_array[1];
} else if (remainder_array[1] != 0) {
digits_array[1] = (n - remainder_array[1]) / 100;
n = remainder_array[2];
} else if (reminder_array[2] != 0) {
digits_array[2] = (n - remainder_array[2]) / 10;
n = remainder_array[3];
} else if (reminder_array[3] != 0) {
digits_array[3] = n - remainder_array[3];
} else {
printf("%d",n / 1000);
}
sum = digits_array[0] + digits_array[1] + digits_array[2] + digits_array[3];
printf("%d",sum);
}
return 0;
}
解决方法
您的代码中存在多个问题:
- 您在读取
remainder_array
的值之前,先从n
的值初始化n
。 - 初始化程序不正确:分隔符应为
,
,而不是;
。 - 您在
if
正文中以else
开头的语句,这是一个语法错误。 - 测试
if (remainder_array[0] = 0)
将remainder_array[0]
设置为0
并得出假值。 -
remainder_array
拼错了一个reminder_array
您的方法很好,但是在阅读并检查remainder_array
之后,您应该使用实际余数(其中5个)来初始化n
:
#include <stdio.h>
int main() {
int n;
if (scanf("%d",&n) == 1 && 10000 <= n && n <= 99999) {
int remainder_array[5] = { n / 10000,n / 1000 % 10,n / 100 % 10,n / 10 % 10,n % 10 };
int sum = remainder_array[0] + remainder_array[1] + remainder_array[2] +
remainder_array[3] + remainder_array[4];
printf("%d\n",sum);
}
return 0;
}
请注意,您实际上并不需要这个remainder_array
,您可以编写:
#include <stdio.h>
int main() {
int n;
if (scanf("%d",&n) == 1 && 10000 <= n && n <= 99999) {
int sum = n / 10000 + n / 1000 % 10 + n / 100 % 10 + n / 10 % 10 + n % 10;
printf("%d\n",sum);
}
return 0;
}
这是一个更具可读性和通用性的版本:
#include <stdio.h>
int main() {
int n;
if (scanf("%d",&n) == 1 && 10000 <= n && n <= 99999) {
int sum = 0;
while (n >= 10) {
sum += n % 10;
n = n / 10;
}
sum += n;
printf("%d\n",sum);
}
return 0;
}
,
您的算法过于复杂,无需数组即可轻松完成。
int sumof5LSD(int x)
{
int result = 0;
for(int digit = 1; digit <=5; digit++)
{
result += abs(x % 10);
x /= 10;
}
return result;
}
int main(void)
{
printf("SUM: %d",sumof5LSD(10564));
}
或者如果不允许使用循环:
int sumof5LSD(int x)
{
int result = 0;
result += abs(x % 10);
x /= 10;
result += abs(x % 10);
x /= 10;
result += abs(x % 10);
x /= 10;
result += abs(x % 10);
x /= 10;
result += abs(x % 10);
return result;
}
使用功能执行类似的任务是很好的。
带有scanf的版本
int main(void)
{
int n;
scanf("%d",&n);
printf("SUM of 5 digits of %d = %d",n,sumof5LSD(n));
}
它还将计算负数的5位数字之和
,除了错误和修改外,@ P_J_和@chqrlie提到的内容我注意到一些主要的逻辑错误和对代码中基本概念的误解(假设首先将if替换为if) 您已经反复给出了else if语句,现在的作用是,只要遇到第一个条件为true,它就会在内部执行该块并从分支中退出,即在if否则执行其余语句,这可能会导致严重的逻辑错误。您的程序。
if (10000 <= n && n <= 99999) {
else if (remainder_array[0] = 0) {
digits_array[0] = (n - remainder_array[0]) / 1000;
n = remainder_array[1];
} else if (remainder_array[1] != 0) {
digits_array[1] = (n - remainder_array[1]) / 100;
n = remainder_array[2];
} else if (reminder_array[2] != 0) {
digits_array[2] = (n - remainder_array[2]) / 10;
n = remainder_array[3];
} else if (reminder_array[3] != 0) {
digits_array[3] = n - remainder_array[3];
} else {
printf("%d",n / 1000);
}
现在在这张图片中,如果您注意到输出,则可以看到digits_array [1-3]为0,这是由于上述原因(它是零,因为我已经预先对其进行了初始化),因此总和为零。
第二个逻辑错误是您将5位数字除以1000,这将为您提供千位数的位置,即在示例10546中,此步骤将导致10,这是错误的,再举一个示例12233,而不是数字的和结果为11,但您将得到20,因为当您将12233除以1000时,digits_array(digit_array [0])的第一个值为12,因此整个输出都会出错。因此要更正此错误,请将其除以10000(仅适用于该程序语句,因为它有5位数字)。
但是,如果您希望在不更改算法的情况下继续操作,则此代码应该可以正常工作。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,sum;
int digits_array[5]={0}; //5 because it is a five digit number.
scanf("%d",&n);
int remainder_array[] = { n % 1000,n % 100,n % 10,n };//This is still a useless array this is kept so as to enter if.
// Complete the code to calculate the sum of the five digits on n.
if (10000 <= n && n <= 99999) {
if (remainder_array[0] != 0) {
digits_array[0] = (n) / 10000;//subtracting n with contents of remainder_array added to the complexity of the algorithm so took out the statement.
n =(n)%10000;
} if (remainder_array[1] != 0) {
digits_array[1] = (n) / 1000;
n =(n)%1000;
} if (remainder_array[2] != 0) {
digits_array[2] = (n) / 100;
n =(n)%100;
} if (remainder_array[3] != 0) {
digits_array[3] = (n)/10;
n=(n)%10;
}
digits_array[4]=n;//the last element of the number
}
sum = digits_array[0] + digits_array[1] + digits_array[2] + digits_array[3]+digits_array[4];
printf("%d",sum);
return 0;
}
注意:该程序不是最好的方法,并且进行一些更改以参考其他答案,以获得更有效的代码,并在嵌套条件之前正确掌握基本知识。
享受编码
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。