笔记:程序阅读题

dengruixun

2023-08-29 10:58:42

Personal

程序阅读题

[NOIP2015]

#include<iostream>
using namespace std;
int main(){
    int a,b,c;
    a = 1;
    b = 2;
    c = 3;
    if (a > b){
        if (a > c){
            cout << a << ' ';
        }else{
            cout << b << ' ';
        }
    }
    cout << c << endl;
    return 0;
}

输出:

答案:3

#include <iostream>
using namespace std;
struct point
{
    int x;
    int y;
};

int main()
{
    int a, b, c;
    struct EX
    {
        int a;
        int b;
        point c;
    } e;

    e.a = 1;
    e.b = 2;
    e.c.x= e.a + e.b;
    e.c.y= e.a * e.b;
    cout << e.c.x << ',' << e.c.y << endl;
    return(0);
}

输出:

答案:3,2

#include <iostream>
#include <string> 
using namespace std;

int main()
{
    string str;
    int i;
    int count; count = 0;
    getline( cin, str );
    for ( i = 0; i < str.length(); i++ )
        if ( str[i] >= 'a' && str[i] <= 'z' )
            count++;
    cout << "It has " << count << " lowercases" << endl; return(0);
}

输出:

答案:It has 18 lowercases

#include <iostream>
#include <string>
using namespace std;

void fun( char *a, char *b )
{
  a = b;
  (*a)++;
}

int main()
{
  char c1, c2, *p1, *p2;
  c1 = 'A';
  c2 = 'a';
  p1 = &c1;
  p2 = &c2;
  fun( p1, p2 );
  cout << c1 << c2 << endl;
  return(0);
}

输出:

答案:Ab

[NOIP2016]

#include <iostream>
using namespace std;
int main()
{
    int max, min, sum, count = 0;
    int tmp;
    cin >> tmp;
    if (tmp == 0)
        return 0;
    max = min = sum = tmp;
    count++;
    while (tmp != 0)
    {
        cin >> tmp;
        if (tmp != 0)
        {
            sum += tmp;
            count++;
            if (tmp > max)
                max = tmp;
            if (tmp < min)
                min = tmp;
        }
    }
    cout << max << "," << min << "," << sum / count << endl;
    return 0;
}

输入:1 2 3 4 5 6 0 7

输出:

答案:6,1,3

#include <iostream>
using namespace std;

int main()
{
    int i = 100, x = 0, y = 0;
    while (i > 0)
    {
        i--;
        x = i % 8;
        if (x == 1)
            y++;
    }
    cout << y << endl;
    return 0;
}

输出:

答案:13

#include <iostream>
using namespace std;

int main(){
    int a[6] = {1, 2, 3, 4, 5, 6};
    int pi = 0;
    int pj = 5;
    int t, i;
    while (pi < pj)
    {
        t = a[pi];
        a[pi] = a[pj];
        a[pj] = t;
        pi++;
        pj--;
    }
    for (i = 0; i < 6; i++)
        cout << a[i] << ",";
    cout << endl;
    return 0;
}

输出:

答案:6,5,4,3,2,1,

#include <iostream>
using namespace std;
int main()
{
    int i, length1, length2;
    string s1, s2;
    s1 = "I have a dream.";
    s2 = "I Have A Dream.";
    length1 = s1.size();
    length2 = s2.size();
    for (i = 0; i < length1; i++)
        if (s1[i] >= 'a' && s1[i] <= 'z')
            s1[i] -= 'a' - 'A';
    for (i = 0; i < length2; i++)
        if (s2[i] >= 'a' && s2[i] <= 'z')
            s2[i] -= 'a' - 'A';
    if (s1 == s2)
        cout << "=" << endl;
    else if (s1 > s2)
        cout << ">" << endl;
    else
        cout << "<" << endl;
    return 0;
}

输出:

答案:=

[NOIP2017]

#include<iostream>
using namespace std;
int main()
{
    int t[256];
    string s;
    int i;
    cin >> s;
    for (i = 0; i < 256; i++)
        t[i] = 0;
    for (i = 0; i < s.length(); i++)
        t[s[i]]++;
    for (i = 0; i < s.length(); i++)
        if (t[s[i]] == 1)
        {
            cout << s[i] << endl;
            return 0;
        }
    cout << "no" << endl;
    return 0;
}

输入:xyzxyw

输出:

答案:z

#include<iostream>
using namespace std;
int g(int m, int n, int x)
{
    int ans = 0;
    int i;
    if (n == 1)
        return 1;
    for (i = x; i <= m / n; i++)
        ans += g(m - i, n - 1, i);
    return ans;
}
int main()
{
    int t, m, n;
    cin >> m >> n;
    cout << g(m, n, 0) << endl;
    return 0;
}

输入:7 3

输出:

答案:8

#include<iostream>
using namespace std;
int main()
{
    string ch;
    int a[200];
    int b[200];
    int n, i, t, res;
    cin >> ch;
    n = ch.length();
    for (i = 0; i < 200; i++)
        b[i] = 0;
    for (i = 1; i <= n; i++)
    {
        a[i] = ch[i - 1] - '0';
        b[i] = b[i - 1] + a[i];
    }
    res = b[n];
    t = 0;
    for (i = n; i > 0; i--)
    {
        if (a[i] == 0)
            t++;
        if (b[i - 1] + t < res)
            res = b[i - 1] + t;
    }
    cout << res << endl;
    return 0;
}

输入:1001101011001101101011110001

输出:

答案:11

#include<iostream>
using namespace std;
int main()
{
    int n, m;
    cin >> n >> m;
    int x = 1;
    int y = 1;
    int dx = 1;
    int dy = 1;
    int cnt = 0;
    while (cnt != 2)
    {
        cnt = 0;
        x = x + dx;
        y = y + dy;
        if (x == 1 || x == n)
        {
            ++cnt;
            dx = -dx;
        }
        if (y == 1 || y == m)
        {
            ++cnt;
            dy = -dy;
        }
    }
    cout << x << " " << y << endl;
    return 0;
}

输入1:4 3

输出1:

输入2:2017 1014

输出2:

答案1:

答案2:

[NOIP2018]