10474 - Where is the Marble?

本文展示了一个使用C++实现的查找算法示例,通过读取输入数据并对其进行排序,之后进行查找操作来验证元素是否存在于数组中。该程序利用了标准模板库STL中的sort函数来进行排序,并用find函数来定位特定元素。
C++语言: Codee#25814
01 /*
02 +++++++++++++++++++++++++++++++++++++++
03                 author: chm
04 +++++++++++++++++++++++++++++++++++++++
05 */
06
07 #include <map>
08 #include <set>
09 #include <list>
10 #include <queue>
11 #include <cmath>
12 #include <stack>
13 #include <bitset>
14 #include <cstdio>
15 #include <cctype>
16 #include <string>
17 #include <vector>
18 #include <cassert>
19 #include <cstdlib>
20 #include <cstring>
21 #include <fstream>
22 #include <sstream>
23 #include <iomanip>
24 #include <iostream>
25 #include <algorithm>
26
27 using namespace std;
28
29 FILE *            fin         = stdin;
30 FILE *            fout         = stdout;
31 const int        max_size     = 10086;
32
33 int buf [ max_size ];
34 int main()
35 {
36 #ifndef ONLINE_JUDGE
37     freopen( "c: \\ in.txt" , "r" , stdin);
38     fout = fopen( "c: \\ garage \\ out.txt" , "w");
39 #endif
40     int n , qry;
41     int sq = 1;
42     while( scanf( "%d%d" , &n , & qry) && !( !n && ! qry))
43     {
44         for( int i = 0; i < n; ++ i)
45             scanf( "%d" , & buf [ i ]);
46         sort( buf , buf + n);
47
48         fprintf( fout , "CASE# %d: \n " , sq ++);
49         for( int j = 0; j < qry; ++ j)
50         {
51             int tmp;
52             scanf( "%d" , & tmp);
53             int * ptr = find( buf , buf + n , tmp);
54             if( ptr == buf + n)
55                 fprintf( fout , "%d not found \n " , tmp);
56             else
57                 fprintf( fout , "%d found at %d \n " , tmp , ptr - buf + 1);
58         }
59     }
60
61
62
63 #ifndef ONLINE_JUDGE
64     fclose( fout);
65     system( "c: \\ garage \\ check.exe");
66     system( "notepad c: \\ garage \\ out.txt");
67 #endif
68     return 0;
69 }

转载于:https://www.cnblogs.com/invisible/archive/2012/03/14/2396091.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值