以下是一个代码中的一个部分 t是测试用例编号,然后每个t都有整数n 我想将整数分解为数字并存储在数组中,然后打印数组的每个元素。
输入
1
45
预期产出
5
4
实际输出
32767
0
代码
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int t,n,n1,tmp,in,len,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int arr[]={};
n1=n;
in=0;
len=0;
while(n1>0)
{
tmp=n1%10;
arr[in]=tmp;
len++;
n1=n1/10;
in++;
}
for(j=0;j<len;j++)
{
printf("%d\n",arr[j]);
}
}
}
答案 0 :(得分:1)
问题在于您对int arr[]={};
的定义,它创建了一个没有存储空间的空数组。除非动态分配,否则最好总是定义最大数组大小。修复该问题(以及初始化所有值)可以解决问题。
以下只是纠正问题的一种方法。它定义了MAXVALUES
的最大数组元素128
。它还添加了提示,以使用户了解所请求的数据,和可防止首次使用newline
时的scanf
作为'n'
的输入被读取}:
#include <stdio.h>
#include <stdlib.h>
#define MAXVALUES 128
int main () {
int t = 0;
int n = 0;
int n1 = 0;
int tmp = 0;
int in = 0;
int len = 0;
int j = 0;
printf ("\n Enter the number of numbers to convert: ");
scanf ("%d%*c", &t);
while (t--) {
printf ("\n Enter the number 'n' : ");
scanf ("%d%*c", &n);
int arr[MAXVALUES] = {0};
in = 0;
len = 0;
n1 = n;
while (n1 > 0) {
tmp = n1 % 10;
arr[in] = tmp;
len++;
n1 = n1 / 10;
in++;
}
for (j = 0; j < len; j++) {
printf ("%d\n", arr[j]);
}
}
return 0;
}
<强>输出:强>
$ ./bin/arrayval
Enter the number of numbers to convert: 2
Enter the number 'n' : 12345
5
4
3
2
1
Enter the number 'n' : 56789
9
8
7
6
5
根据arr
n
您可以动态分配arr
以防止#define
分配比所需更多的空间(这有点像使用大锤在这里拍苍蝇)。它需要更多的工作。具体来说,在分配n
之前需要知道arr
中有多少位数,这样您就可以分配不超过需要的内存。这里,n
中的位数由函数szitoa
计算,然后分配arr
。这是该类型解决方案的一种方法:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/* determine number of chars required for string of int i,
does NOT include space for null char (like strlen) */
size_t
szitoa (int val)
{
int it = 0;
int sz = (val > 0) ? 0 : 1; /* provide space of '-' */
val = (val > 0) ? val : -val; /* absolute value */
for (it = 1; it < INT_MAX; it*=10) {
sz++;
if (val >= it && val < (it*10))
break;
}
return sz;
}
int main () {
int t = 0;
int n = 0;
int n1 = 0;
int tmp = 0;
int in = 0;
int len = 0;
int j = 0;
printf ("\n Enter the number of numbers to covert: ");
scanf ("%d%*c", &t);
while (t--) {
printf ("\n Enter the number 'n' : ");
scanf ("%d%*c", &n);
/* dynamically allocate arr and validate */
int *arr = calloc (szitoa (n), sizeof (int));
if (!arr) {
fprintf (stderr, "error: arr allocation failed\n");
exit (EXIT_FAILURE);
}
in = 0;
len = 0;
n1 = n;
while (n1 > 0) {
tmp = n1 % 10;
arr[in] = tmp;
len++;
n1 = n1 / 10;
in++;
}
for (j = 0; j < len; j++) {
printf ("%d\n", arr[j]);
}
if (arr) free (arr); /* free memory allocated to arr */
}
return 0;
}
答案 1 :(得分:0)
这应该适合你:
#include <stdio.h>
int main() {
int number, numberCount, tmp, count;
printf("Please enter a number:\n>");
scanf("%d", &number);
tmp = number;
for(numberCount = 0; tmp > 0; numberCount++)
tmp /= 10;
int numbers[numberCount];
for(count = (numberCount-1); number > 0; count--) {
numbers[count] = number % 10;
number /= 10;
}
for(count = 0; count < numberCount; count++)
printf("%d digit is: %d\n", count+1, numbers[count]);
return 0;
}
输入:
45
输出:
1 digit is: 4
2 digit is: 5
答案 2 :(得分:0)
您正在尝试为数组写入比为已分配空间更多的整数。这段代码在这里:
while(n1>0)
{
tmp=n1%10;
arr[in]=tmp; // You initialize the array space to in, which is ambiguous.
len++;
n1=n1/10;
in++; // You increment the array after already initializing space which you can't do in this manner.
}
如果您希望根据用户输入的数量来扩展数组,这也很模糊 - 您可能会考虑使用动态内存。