gpt4 book ai didi

c - 数组中彼此相邻的相同数字的序列号

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

#include <stdio.h>

void main(){
int i, j, n;
int num[5];
int serial;

for(i=0; i<5; ++i){
scanf("%d",&num[i]);
if(num[i]==num[i-1])
serial=i;
else
continue;
}
printf("Serial number of equal numbers next to each other:%d. %d.", serial-1, serial);
}

这可能很难理解,因为我的母语不是英语。如果相邻的数字相等,程序应打印这些数字的序列号。

例如:

Input: 1 2 3 7 7 7 6;
Output: 3. 4. 5.

输入:5 5 5 5 5输出:0.1.2.3.4。

我做了一些更改,现在它打印两个相等数字的序列。I: 1 2 2 3 4 - O: 1. 2.但如果所有数字都相等怎么办?

最佳答案

// ...

// deal with index 0
if (num[0] == num[1]) printf("0. ");

// deal with indexes 1 .. N - 2
for (int k = 1; k < n - 1; k++) {
if ((num[k - 1] == num[k]) || (num[k] == num[k + 1])) {
printf("%d. ", k);
}
}

// deal with index N - 1
if (num[n - 2] == num[n - 1]) printf("%d. ", n - 1);

// ... possibly with a printf("\n"); somewhere

关于c - 数组中彼此相邻的相同数字的序列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35869784/

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