洛谷题单 103【入门4】数组

    科技2026-04-20  5

    P1047 校门外的树

    500 3 150 300 100 200 470 471

    思路:

    三遍循环: 1.第一遍:将路上的所有树打上标记,表示这个点没有被访问过 2.第二遍:读入区间的头和尾,从这个区间的头开始循环,到尾结束,标记为访问过 3.第三遍:顺序访问路上的树,只要没访问过,计数加1,路上所有还存在的树的数目即为所求

    #include <iostream> #include <cstdio> #include <cstring> using namespace std; int L, M; int cnt = 0; //剩余树的数目 int vis[10000 + 10]; //用来判断树木有没有被访问过 int main() { memset(vis, 0, sizeof(vis)); //初始化该数组 cin >> L >> M; for (int i = 0; i <= L; i++) { vis[i] = 0; //从0这个树开始,将这条路上所有的树标记为未访问过 } for (int i = 1; i <= M; i++) { int head, tail; //定义区间的头和尾 cin >> head >> tail; for (int j = head; j <= tail; j++) { if (vis[j] == 0) { vis[j] = 1; //从这个区间的头和尾开始,将所有区间内的树标记为访问过 } } } for (int i = 0; i <= L; i++) { if (vis[i] == 0) { cnt++; //计算未被访问过的树的数目 } } cout << cnt << endl; return 0; }

    P1161 开灯

    3 1.618034 13 2.618034 7 1.000000 21 #include <iostream> using namespace std; int main() { int a[2000001], n; double x, y; cin >> n; for (int i = 1; i <= n; i++) { cin >> x >> y; for (double j = 1; j <= y; ++j) { if (a[int(j * x)] == 0) { a[int(j * x)] = 1; } else { a[int(j * x)] = 0; } } } for (int i = 1;; i++) { if (a[i] == 1) { cout << i; break; } } return 0; }

    P1205 [USACO1.2]方块转换 Transformations

    3 @-@ --- @@- @-@ @-- --@

    P1319 压缩技术

    7 3 1 6 1 6 4 3 1 6 1 6 1 3 7

    题解:

    !:表示相反,目的使得0和1的数量交替输入 1.如果原先为true就变为false 2.如果原先为false就变为true

    #include <iostream> using namespace std; int main() { int n; cin >> n; //矩阵长宽 int a[n * n + 10]; //设置一个登记用的数组,其实n*n就够了,+10是为了防爆 int full; //输入要用的 bool key = false; //判断当前输入的是1的数量还是0的数量,初始为0 int p = 0; //已经登记到第几个数,类似于指针 while (cin >> full) //持续输入 { int i; for (i = p; i < p + full; i++) { a[i] = key; //把这一块区域登记为key,就是0或1 } p = i; key = !key; } p = 0; //指针归零,下面要开始从头开始输入 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << a[p]; p++; } cout << endl; } return 0; }

    P1320 压缩技术(续集版)

    0001000 0001000 0001111 0001000 0001000 0001000 1111111

    P1427 小鱼的数字游戏

    3 65 23 5 34 1 30 0

    一般做法:

    #include <iostream> using namespace std; int main() { int x[100], c = 0; for (int i = 0;; i++) { cin >> x[i]; if (x[i] == 0) { break; } c = i; } for (int j = c; j >= 0; j--) { cout << x[j] << " "; } return 0; }

    STL:

    vector:

    #include <iostream> #include <vector> //STL vector的头文件 using namespace std; vector<int> a; //定义一个int型的vector int c; //STL可以完全不用担心数组大小的问题,这个和string类似 int main() { while (1) //有时候也可以巧用死循环 { cin >> c; if (c == 0) break; //终止条件 a.push_back(c); //将括号里的元素压入vector尾部 } while (!a.empty()) { cout << a.back() << " "; //.back()是一个返回vector尾部元素的函数 a.pop_back(); //删除vector尾部的元素 } return 0; }

    stack:

    #include <iostream> #include <stack> using namespace std; stack<int> a; int k; int main() { while (cin >> k) { a.push(k); } a.pop(); //这种输入方式在本地调试的时候需要在输入结束后按Ctrl+Z来结束输入 while (!a.empty()) { cout << a.top() << " ", a.pop(); } return 0; }

    了解更多方法可点击本处

    P1428 小鱼比可爱

    6 4 3 0 5 1 2

    注意:int a[101], b[101], n;写在main外! b[101]中的变量为随机数

    #include <iostream> using namespace std; int a[101], b[101], n; int main() { cin >> n; for (int i = 1; i <= n; i++) //读入每条鱼的可爱值 { cin >> a[i]; } for (int i = 1; i <= n; i++) //枚举n条鱼 { for (int j = i; j >= 1; j--) //从第i个位置倒着往前找 { if (a[j] < a[i]) { b[i]++; //如果找到比第i条鱼丑的,统计数组b对应的b[i]+1 } } } for (int i = 1; i <= n; i++) { cout << b[i] << " "; } return 0; }

    待续…

    Processed: 0.008, SQL: 9