NOIP普及赛阅读程序

16-01 读入整数

#include <iostream>
using namespace std;
int readint(){
    int num = 0;          // 存储读取到的整数
    int negative = 0;    // 负数标识
    char c;                  // 存储当前读取到的字符
    c = cin.get();
    while ((c < '0' || c > '9') && c != '-')
        c = ①;//cin.get() 非合法字符再读取一个 
    if (c == '-')
        negative = 1;
    else
        ②;//num=c-'0'  不是负号,此时已经读入了一个数字 
    c = cin.get();//读入一个字符 
    while (③){//c>='0' && c<='9' 继续读入合法字符 
        ④;//num=num*10+c-'0'  //读入的字符按位权合并整数 
        c = cin.get();
    }
    if (negative == 1)
        ⑤;//num=-num 给负数加一个符号 
    return num;
}
int main()
{
    int a, b;
    a = readint();
    b = readint();
    cout << a << endl
         << b << endl;
    return 0;
}

 16-02 郊游活动

#include <iostream>
using namespace std;
#define MAXN 1000000

int n, B, A, M[MAXN], C[MAXN], l, r, ans, mid;
//判断是否可以租到nn辆车 
bool check(int nn) {
    int count = 0, i, j;
    i = ①;//n-nn+1 从n-nn+1到n保证循环nn次 
    j = 1;
    while (i <= n) {
        if(②)//M[i]<C[j] 不够买需要借用学校经费 
            count += C[j] - M[i]; //贪心 少借原则 使用最有钱的学生租最便宜的车 
        i++;
        j++;
    }
    return ③;//count<=A 借用总经费不超出学校经费就可以租到nn辆车 
}

//归并排序-升序    
void sort(int a[], int l, int r) { 
    int i = l, j = r, x = a[(l + r) / 2], y;
    while (i <= j) {
        while (a[i] < x) i++;
        while (a[j] > x) j--;
        if (i <= j) {
            y = a[i]; a[i] = a[j]; a[j] = y;
            i++; j--;
        }
    }
if (i < r) sort(a, i, r);
if (l < j) sort(a, l, j);
}

int main() {
    int i;
    cin >> n >> B >> A;
    for (i = 1; i <= n; i++)
        cin >> M[i];
    for (i = 1; i <= B; i++)
        cin >> C[i];
    sort(M, 1, n);//学生自带经费排序 升序 
    sort(C, 1, B);//自行车租用价格排序 升序 
    l = 0;
    r = n;
    while (l <= r) {
        mid = (l + r) / 2;
        if(④){//check(mid) 如果可以租到mid辆车, 判断是否可以多租 ,右移增加mid 
            ans = mid;
            l = mid + 1;//可以租 增大租车数量 继续二分尝试 
        }else
            r = ⑤;//mid-1 //不可以租 减少租车数量 继续二分尝试
    }
    cout << ans << endl;
    return 0;
}

17-01快速幂

 

#include<iostream>
using namespace std;
int x, p, m, i, result;
int main(){
    cin >> x >> p >> m;
    result = ①;// result=1  初始为1 
    while (②){// p>0   指数不为0就递归处理 
        if (p % 2 == 1)  //奇数 底数*x 先处理掉 变成偶数 2^5=2*2^4 
            result = ③;//result=result*x%m  最后一个是1,所以这个下面x=x*x会乘到result 
        p /= 2;
        x = ④;// x = x*x%m 偶数则底数平方 
    }
    cout << ⑤ << endl;
    return 0;
}

 

 

 

17-02切割绳子

 

#include<iostream>
using namespace std;
int n, m, i, lbound, ubound, mid, count;
int len[100]; // 绳子长度
int main()
{
    cin >> n;
    count = 0;
    for (i = 0; i < n; i++)
    {
        cin >> len[i];
        ①;//count+=len[i] 累计绳子长度 
    }
    cin >> m;
    if (②)//count<n  不够分 
    {
        cout << "Failed" << endl;
        return 0;
    }
    lbound = 1;
    ubound = 1000000;
    while (③)//lbound<ubound  做边界<右边界 
    {
        mid = ④;//(lbound+ubound+1)/2  快速找每段长度 避免特殊情况死循环 
        count = 0;
        for (i = 0; i < n; i++)
            ⑤;//count+=len[i]/mid 每段绳子切mid长,可以切割几根 
        if (count < m)
            ubound = mid - 1;
        else
            lbound = mid;
    }
    cout << lbound << endl;
    return 0;
}

 

posted @ 2021-08-01 10:26  new-code  阅读(51)  评论(0)    收藏  举报