編程題7-5?查找整數(shù)(10?分)本題要求從輸入的N個整數(shù)中查找給定的X。如果找到,輸出X的位置(從0開始數(shù));如果沒有找到,輸出“Not Found”。輸入格式:輸入在第一行中給出兩個正整數(shù)N(≤20)和X,第二行給出N個整數(shù)。數(shù)字均不超過長整型,其間以空格分隔。輸出格式:在一行中輸出X的位置,或者“Not Found”。代碼如下:#include <stdio.h>int main(){? ? int n, x; ? ? ? ? ? ? ? ? ? ? ?//輸入n個數(shù),找x?? ? scanf("%d %d", &n, &x);? ??? ? int a[n], i, flag = 1; ? ? ? ? //將n個數(shù)存在a數(shù)組里?? ? for(i = 0; i < n; i++) {? ? ? ? scanf("%d", &a[i]);? ? ? ? if(a[i] == x) {? ? ? ? ? ? printf("%d", i); ? ? ? //當(dāng)a[i]等于x時,輸出i?? ? ? ? ? ? flag = 0;? ? ? ? }? ? }? ? if(flag == 1) { ? ? ? ? ? ? ? ?//當(dāng)數(shù)組a循環(huán)完后沒有找到x?? ? ? ? printf("Not Found");? ? }? ??? ? return 0;}
執(zhí)行的時候int a[n],i,flag=1;那里出了問題,求指點
名不見經(jīng)傳的小菜鳥
2017-10-26 19:27:43