C 如何进行n位不分隔的输入

C 如何进行n位不分隔的输入,c,scanf,C,Scanf,注意:请不要为此问题编写解决方案(即算法逻辑) 昨天厨师开了一个很棒的派对,他不记得是怎么庆祝的了。但他在厨房里发现了一张奇怪的纸,上面有n个数字(让我们给他们从1到n的索引,并把它们命名为a1,a2…aN) 厨师长记得他玩过这样的游戏: On each step he choose an index x from 1 to n. For all indices y (y < x) he calculated the difference by = ax - ay. Then he cal

注意:请不要为此问题编写解决方案(即算法逻辑)

昨天厨师开了一个很棒的派对,他不记得是怎么庆祝的了。但他在厨房里发现了一张奇怪的纸,上面有n个数字(让我们给他们从1到n的索引,并把它们命名为a1,a2…aN)

厨师长记得他玩过这样的游戏:

On each step he choose an index x from 1 to n.
For all indices y (y < x) he calculated the difference by = ax - ay.
Then he calculated B1 - sum of all by which are greater than 0 and B2 - sum of all by which are less than 0.
The answer for this step is B1 - B2.
输出

For each of m steps print single number in a line - answer of the step.
约束条件

1 ≤ n, m ≤ 10^5
0 ≤ ai ≤ 9
1 ≤ x ≤ n

现在如何输入n位数字?我的意思是如何使用scanf代码。我不知道n的确切值,因此无法声明这些变量。这是否意味着我输入一位数字?一次只输入一个字符:

int num = getc(stdin) - '0';
“0”的减法是将一个字符变成一个数字。显然,错误检查只是一个练习。

(假设n位输入是一个字符串,包含n个字符)使用动态内存分配和自定义的
scanf
格式化程序:

int n = 0;
char* digits = NULL;
char format[256];

printf("n=");
scanf("%d", &n);
digits = calloc(n + 1, 1); /* for the terminator 0 to be initialized*/

snprintf(format, 256, "%%%ds", n); /* to not to cause an overflow */

if(NULL == digits)
{
    printf("Not enough memory");
    exit(1);
}
scanf(format, digits);
printf("%s", digits);

// DO your algorithm here

free(digits);

@user3424954再次阅读问题陈述,它清楚地提到了第一行输入n,它是数字字符串的长度。 你可以尝试使用
*scanf(“%s”,数组名称)*

为什么会有这么长、这么长、完全不相关的故事?只有你的最后一段(完全用黑体字写,看这有多烦人?)是相关的,而且是一个公平的问题。我不知道这一切意味着什么,但我清楚地意识到OP怀疑我投了否决票。请阅读。也请阅读。最后,OP的状态表明没有阅读,这是对该网站的友好介绍。
int n = 0;
char* digits = NULL;
char format[256];

printf("n=");
scanf("%d", &n);
digits = calloc(n + 1, 1); /* for the terminator 0 to be initialized*/

snprintf(format, 256, "%%%ds", n); /* to not to cause an overflow */

if(NULL == digits)
{
    printf("Not enough memory");
    exit(1);
}
scanf(format, digits);
printf("%s", digits);

// DO your algorithm here

free(digits);