我只是想知道如何将数组“cadenaNumeros”的值和索引保存为整数,如果用户条目是这样的:123456789或48934013或183853025...等。
我想用 FOR 来做,但我的代码不起作用。
例如:如果我们有这个数组“123456789”,则该值将为 2,索引 = 1,并且该数字将其保存为整数。
我正在使用 ANSI C 进行编程。
#include <stdio.h>
#include <stdlib.h>
void main()
{
int a = 0, i = 0, indice, numero[20], vuelta, quedan, x;
int cadenaNumeros[20000];
int num, num2;
printf("\n\nEscribe el numero de palancas: ");
scanf("%d", &num2);
printf("\n\nEscriba el numero de la palanca roja: ");
scanf("%d", &num);
printf("\n\nIntroduzca una cadena de numeros: ");
scanf("%d", &cadenaNumeros);
for(i = 0; cadenaNumeros[i] < num2; i++); {
cadenaNumeros[0] == numero[x];
printf("%d", numero[x]);
}
system("PAUSE");
return 0;
}
最佳答案
int index = -1, i = 0;
// Assuming num2 is the number you're searching for
// And the array size is 20000
for( ; i < 20000; i++) {
if( cadenaNumeros[i] == num2) {
index = i;
break;
}
}
if( index != -1) {
printf( "Found %d at index %d\n", num2, index);
}
else {
printf( "Number not found\n");
}
关于c - 我如何保存 ANSI C 数组中位置的值和索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11421836/