#include<stdio.h> int main() { int n; int a[100]; int i = 1; printf("please input n"); scanf_s("%d", &n); while (n--) { printf("input the number") scanf_s("%d",&a[i]); i++; } n = i - 1; i = 1; printf("please input the number to find"); int goal; scanf_s("%d", &goal); for (i = 1; i <= n; i++) { if (goal == a[i]) { printf("%d", found it : i); i = n + 2; } } if (i = n + 1) printf("can't find it") return 0; }把这个c++改成python语言
时间: 2024-02-06 22:02:59 浏览: 163
当然可以,这是一段C++代码:
```c++
#include<stdio.h>
int main() {
int n;
int a[100];
int i = 1;
printf("please input n");
scanf_s("%d", &n);
while (n--) {
printf("input the number");
scanf_s("%d",&a[i]);
i++;
}
n = i - 1;
i = 1;
printf("please input the number to find");
int goal;
scanf_s("%d", &goal);
for (i = 1; i <= n; i++) {
if (goal == a[i]) {
printf("%d", found it : i);
i = n + 2;
}
}
if (i = n + 1)
printf("can't find it");
return 0;
}
```
这是对应的Python代码:
```python
n = int(input("please input n: "))
a = []
for i in range(n):
a.append(int(input("input the number: ")))
goal = int(input("please input the number to find: "))
for i in range(n):
if goal == a[i]:
print("found it: ", i+1)
break
else:
print("can't find it")
```
阅读全文