zoukankan      html  css  js  c++  java
  • 一个FLAG #09# STL初步

    题&例子

    例题5-1 大理石在哪儿

    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 10000;
    
    int main()
    {
        int n, q, x, a[maxn], kase = 0;
        
        while (scanf("%d %d", &n, &q) == 2 && n) {
            
            printf("CASE# %d:
    ", ++kase);
            
            for (int i = 0; i != n; ++i) {
                scanf("%d", &a[i]);
                // 读入 n 个大理石上的数字 
            }
            
            sort(a, a + n); // 排序
            
            while (q--) {
                // 查找
                scanf("%d", &x);
                int p = lower_bound(a, a + n, x) - a; // 在已排序数组 a 中寻找 x 
                if (a[p] == x) {
                    printf("%d found at %d
    ", x, p + 1);
                } else {
                    printf("%d not found
    ", x);    
                }
            } 
        }
        
        return 0;
    }

    关于“随机数,数组与指针”的例子。

    #include <cstdio>
    #include <algorithm>
    
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    const int maxn = 10000;
    
    int a[maxn];
    
    void printArray()
    {
        for (int i = 0; i != 10; ++i) {
            printf("%d ", a[i]);
        }
        printf("
    ");
    }
    
    int main()
    {    
        srand((int) time(0)); // 随机数种子 
    
        for (int i = 0; i != 10; ++i) {
            a[i] = rand() % 100; // 产生 [0,100) 之间的随机数 
        }
        
        printArray();
        // => 68 34 94 28 88 91 82 28 16 2
        
        sort(a, a + 10); // 对下标为[0, 10)的元素进行排序 
        
        printArray();
        // => 2 16 28 28 34 68 82 88 91 94
            
        printf("%d %d
    ", a, &a[0]); // => 4415552 4415552
        printf("%d
    ", lower_bound(a, a + 10, a[2])); // => 4415560
        printf("%d
    ", &a[2]); // => 4415560
        printf("%d
    ", &a[2] - a); // => 2
        for (int i = 0; i != 3; ++i) {
            printf("%d ", &a[i]);
        } // => 4415552 4415556 4415560
        printf("
    ");
        printf("%d %d", *(a + 2), a[2]); // 28 28
        
        return 0;
    }

    参考

    [1] C++产生随机数 - Lucky& - 博客园

  • 相关阅读:
    AcWing:141. 周期(KMP)
    暑假集训
    AcWing:112. 雷达设备(贪心 + 笛卡尔坐标系化区间)
    AcWing:111. 畜栏预定(贪心 + 小根堆)
    20200207总结
    Miku and Generals(二分图染色+可行性dp)
    20200205总结
    Coding Contest(最小费用最大流)
    Wannafly-Winter-Camp day5总结
    Wannafly-Winter-Camp day4总结
  • 原文地址:https://www.cnblogs.com/xkxf/p/12644110.html
Copyright © 2011-2022 走看看