gpt4 book ai didi

代码没有读取整个字符串

转载 作者:行者123 更新时间:2023-11-30 20:31:27 25 4
gpt4 key购买 nike

问题:

The first scan is the number of converting numbers the user requests. The following are the ones that are are going to be converted. The point is to count the number of LEDs that are going to be needed to write these numbers, like the following pic Numbers in LED

#include <stdio.h>
#include <stdlib.h>

int main(){
int i, j, k, leds, a, clean;

//here i scan the number of times the program will be executed

scanf ("%i", &a);

int n[a], l[a];
char c[101] = {0}; //the string where the numbers will be armazened

for (i=0; i< a; i++)
{
scanf ("%i", &n[i]); //here i get the numbers
}

for (i=0; i<a; i++)
{
sprintf(c, "%d", n[i]);
leds = 0; //where the numbers of leds needed will be armazened

while(c[j] != '\0')
{
if(c[j] == '0')
{
leds = leds + 6;
}
else if(c[j] == '1')
{
leds = leds + 2;
}
else if(c[j] == '2')
{
leds = leds + 5;
}
else if(c[j] == '3')
{
leds = leds + 5;
}
else if(c[j] == '4')
{
leds = leds + 4;
}
else if(c[j] == '5')
{
leds = leds + 5;
}
else if(c[j] == '6')
{
leds = leds + 6;
}
else if(c[j] == '7')
{
leds = leds + 3;
}
else if(c[j] == '8')
{
leds = leds + 7;
}
else if(c[j] == '9')
{
leds = leds + 6;
}
clean = j;
j++;

}


l[i] = leds; //where the number of leds of each will be armazened to be printed afterwards
}

for (i = 0; i<a; i++)
{
printf("%i\n", l[i]);
}

}

我的输入:3115380281931123456

我得到的输出:27(唯一正确的)20

我应该得到什么:272925

我只是不知道我做错了什么,但我认为是 c[] 和 sprintf 。但我该怎么办呢?

抱歉,如果英语不太正确

最佳答案

您没有初始化j

   sprintf(c, "%d", n[i]);
leds = 0; //where the numbers of leds needed will be armazened

应该是

   sprintf(c, "%d", n[i]);
j = 0;
leds = 0; //where the numbers of leds needed will be armazened

关于代码没有读取整个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50892824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com