code4sk/c/sourcecode/朱森森带领的奇妙冒险/2022-2023第二学期单开班第2次月考/查找满足条件的最后一个整数.cpp
2023-12-15 22:38:26 +08:00

56 lines
No EOL
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
7-8 查找满足条件的最后一个整数
分数 10
作者 颜晖
单位 浙大城市学院
本题要求编写程序将输入的n个整数存入数组a中然后在数组a中查找给定的x。如果数组a中的元素与x的值相同输出满足条件的最后一个元素的下标下标从0开始如果没有找到输出“Not Found”。
输入格式:
输入在第1行中给出一个正整数n1≤n≤100和一个整数x第2行输入n个整数其间以空格分隔。题目保证数据不超过长整型整数的范围。
输出格式:
如果找到输出与x的值相同的最后一个元素的下标如果没有找到在一行中输出“Not Found”。
输入样例1:
5 9
2 9 8 1 9
输出样例1:
4
输入样例2:
10 101
2 8 10 1 9 8 -101 0 98762 1
输出样例2:
Not Found
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
*/
#include <stdio.h>
int main()
{
int n, num, cnt = 0, maxi;
scanf("%d %d", &n, &num);
int a[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++)
{
if (a[i] == num)
{
cnt++;
maxi = i;
}
}
if (cnt == 0)
printf("Not Found");
else if (cnt != 0)
printf("%d", maxi);
return 0;
}