在没有数组的情况下打印 C 中整数的相邻和序列
print the adjacent sum sequence of integers in C without array
通常是否可以存储一系列输入并稍后打印出来而不使用数组?
我需要按顺序接受整数输入,-1 标记为输入结束而不将其作为输入(称之为 xn)。输出应该是序列 an 其中
an:= xn-1 + xn, 其中 n> 1
以下是我的代码,但输出与输入一起发生,而不是像问题真正需要的那样一次性发生。这是代码:
#include <stdio.h>
int main() {
int xo,a;
int sum =0;
int previous=0;
scanf("%d",&xo);
previous = xo;
while ((!scanf("%d",&a)==0) && (!(a==-1))) {
sum = previous+a;
previous = a;
printf("%d",sum);
}
return 0;
}
输入:
1个
2个
3个
4个
-1
输出:
3 5 7
只需继续附加到字符串即可。在输入结束时,打印字符串。
一个字符串可以看作是一个数组。也许这会使这个答案无效
否则没有。您需要一个数组来存储输入或计算。
糟糕...正如 Daniel Jour 所说 你可以使用列表而不是数组。
我不完全确定,但您的任务目的可能是让您自己熟悉列表。那么你不需要一个数组来存储输入值:
struct list {
int val;
struct list * next;
};
处理列表应抽象为通用函数:
struct list * list_add(struct list * lst, int val) {
struct list * node =
(struct list *) malloc(sizeof(struct list));
node->val = val;
node->next = lst;
return node;
}
void list_free(struct list * lst) {
while (lst) {
struct list * temp = lst;
lst = lst->next;
free(temp);
}
}
struct list * list_nreverse(struct list * lst) {
struct list * nlst = lst;
if (lst) {
lst = lst->next;
nlst->next = NULL;
while (lst) {
struct list * temp = lst;
lst = lst->next;
temp->next = nlst;
nlst = temp;
}
}
return nlst;
}
要使用列表,您可以将输入值存储在列表中,或者为了接近您已有的代码,即时计算输出(相邻总和)并将其存储在列表中, 打印整个列表
最后(倒序):
int current;
int previous;
struct list * output = NULL;
scanf("%d", &previous);
while ((!scanf("%d", ¤t) == 0) && (current != -1)) {
sum = previous + current;
previous = current;
output = list_add(output, sum);
}
output = list_nreverse(output);
struct list * iterator = output;
while (iterator) {
printf("%d ", iterator->val);
iterator = iterator->next;
}
list_free(output);
注意:代码目前未经测试,但应该让您了解如何完成此操作。
整数输入按 'scan' 顺序获取。要找到输入的结尾,请使用 -1 检查扫描 return 值,以便扫描空元素。
下面是我的代码,输出根据您的要求进行。输出一次性发生,您的问题确实需要@subrat。代码如下:
#include <stdio.h>
int main()
{
int xo,a;
int sum =0;
int diff=0;
int previous=0;
scanf("%d",&xo); //dont need xo here :: use previous only
previous = xo;
while ((scanf("%d",&a)!=-1)) //important to use sum=0
{
sum = previous+a;
previous = a;
printf("%d ",sum);
sum=0;
}
return 0;
}
输入:1 2 3 4 -1
输出:3 5 7 3
通常是否可以存储一系列输入并稍后打印出来而不使用数组?
我需要按顺序接受整数输入,-1 标记为输入结束而不将其作为输入(称之为 xn)。输出应该是序列 an 其中
an:= xn-1 + xn, 其中 n> 1
以下是我的代码,但输出与输入一起发生,而不是像问题真正需要的那样一次性发生。这是代码:
#include <stdio.h>
int main() {
int xo,a;
int sum =0;
int previous=0;
scanf("%d",&xo);
previous = xo;
while ((!scanf("%d",&a)==0) && (!(a==-1))) {
sum = previous+a;
previous = a;
printf("%d",sum);
}
return 0;
}
输入: 1个 2个 3个 4个 -1
输出: 3 5 7
只需继续附加到字符串即可。在输入结束时,打印字符串。
一个字符串可以看作是一个数组。也许这会使这个答案无效
否则没有。您需要一个数组来存储输入或计算。
糟糕...正如 Daniel Jour 所说
我不完全确定,但您的任务目的可能是让您自己熟悉列表。那么你不需要一个数组来存储输入值:
struct list {
int val;
struct list * next;
};
处理列表应抽象为通用函数:
struct list * list_add(struct list * lst, int val) {
struct list * node =
(struct list *) malloc(sizeof(struct list));
node->val = val;
node->next = lst;
return node;
}
void list_free(struct list * lst) {
while (lst) {
struct list * temp = lst;
lst = lst->next;
free(temp);
}
}
struct list * list_nreverse(struct list * lst) {
struct list * nlst = lst;
if (lst) {
lst = lst->next;
nlst->next = NULL;
while (lst) {
struct list * temp = lst;
lst = lst->next;
temp->next = nlst;
nlst = temp;
}
}
return nlst;
}
要使用列表,您可以将输入值存储在列表中,或者为了接近您已有的代码,即时计算输出(相邻总和)并将其存储在列表中, 打印整个列表 最后(倒序):
int current;
int previous;
struct list * output = NULL;
scanf("%d", &previous);
while ((!scanf("%d", ¤t) == 0) && (current != -1)) {
sum = previous + current;
previous = current;
output = list_add(output, sum);
}
output = list_nreverse(output);
struct list * iterator = output;
while (iterator) {
printf("%d ", iterator->val);
iterator = iterator->next;
}
list_free(output);
注意:代码目前未经测试,但应该让您了解如何完成此操作。
整数输入按 'scan' 顺序获取。要找到输入的结尾,请使用 -1 检查扫描 return 值,以便扫描空元素。
下面是我的代码,输出根据您的要求进行。输出一次性发生,您的问题确实需要@subrat。代码如下:
#include <stdio.h>
int main()
{
int xo,a;
int sum =0;
int diff=0;
int previous=0;
scanf("%d",&xo); //dont need xo here :: use previous only
previous = xo;
while ((scanf("%d",&a)!=-1)) //important to use sum=0
{
sum = previous+a;
previous = a;
printf("%d ",sum);
sum=0;
}
return 0;
}
输入:1 2 3 4 -1
输出:3 5 7 3