[CSP-J 2019] The first question by the numbers game explanations opposite Ge

A brief analysis of the topic

Water is really the question, the basic input output would be if else's will write, emm ...... do not know what the talk.

Then take this opportunity to say a few metaphysics operate it

Conventional input string

The most common practice, the basic will:

#include<iostream>
using namespace std;
int ans;
char s[8];
int main() {
    cin >> s;
    for (int i = 0; i < 8; i++) {
        if (s[i] == '1')ans++;
    }
    cout << ans;
    return 0;
}

Instead of using an array or char string:

#include<iostream>
#include<string>
using namespace std;
int ans;
char s[8];
int main() {
    cin >> s;
    for (int i = 0; i < 8; i++) {
        if (s[i] == '1')ans++;
    }
    cout << ans;
    return 0;
}

Basis of this foundation (with no use cycle)

Because we know the length of the string is 8, so you can play eight hand input, not cycle (cycle but even people who will not go to the game why?!)

#include<iostream>
using namespace std;
int ans;
char ch;
int main() {
    cin >> ch;
    if (ch == '1')ans++;
    cin >> ch;
    if (ch == '1')ans++;
    cin >> ch;
    if (ch == '1')ans++;
    cin >> ch;
    if (ch == '1')ans++;
    cin >> ch;
    if (ch == '1')ans++;
    cin >> ch;
    if (ch == '1')ans++;
    cin >> ch;
    if (ch == '1')ans++;
    cin >> ch;
    if (ch == '1')ans++;
    cout << ans;
    return 0;
}

The following start (negative) optimized code

Enter the side edge a little provincial judge memory

#include<cstdio>
using namespace std;
int ans;
int main(){
    for(int i=0;i<8;i++){
        if(getchar()=='1')ans++;
    }
    printf("%d",ans);
    return 0;
}

Returned directly with getchar judgment function value, a character memory is omitted, and getchar faster than some cin.

bitset save 01 strings

bitset 01 is stored, it can be used to keep the string 01:

#include<iostream>
#include<bitset>
using namespace std;
bitset<8>s;
int ans;
int main() {
    cin >> s;
    printf("%d", s.count());//count函数统计s中"1"的个数
    return 0;
}

Instead of comparing with a comparison operator ^

Bit computing is the most basic computer operation, and it should be faster than some ordinary (it):

#include<cstdio>
using namespace std;
int ans;
int main() {
    for (int i = 0; i < 8; i++) {
        if (!(getchar() ^ '1'))ans++;
    }
    printf("%d", ans);
    return 0;
}

Object-Oriented

- In this way first of all you have an object -

- or immediately find an object -

Or you need to define a class (√)

#include<cstdio>
using namespace std;
class ONEZEROCHUAN {
    char ch[8];
    int geshu;
public:
    ONEZEROCHUAN():geshu(0){
        for (int i = 0; i < 8; i++) {
            ch[i] = getchar();
        }
        for (int i = 0; i < 8; i++) {
            if (ch[i] == '1')geshu++;
        }
    }
    void print() {
        printf("%d", geshu);
    }
}ans;
int main() {
    ans.print();
    return 0;
}

Code gradual abstraction ing

2.0 Object-Oriented

- At this time when we already have the object -

When we have mastered the art of object-oriented programming, we were surprised to find that,- With the object after we have need of nothing -, With the constructor since we do not need a main function:

#include<cstdio>
using namespace std;
class solve {
    int ans;
public:
    solve():ans(0) {
        for (int i = 0; i < 8; i++) {
            if (!(getchar() ^ '1'))ans++;
        }
        printf("%d", ans);
    }
}a;
int main(){}

3.0 Object-Oriented

- when we have been satisfied with just an object, we are ready to open the harem -

When we have been satisfied with trivial constructors, destructors we thought:

#include<cstdio>
using namespace std;
class solve {
    int ans;
public:
    solve() :ans(0) {}
    ~solve() {
        for (int i = 0; i < 8; i++) {
            if (!(getchar() ^ '1'))ans++;
        }
        printf("%d", ans);
    }
}a;
int main(){}

Ultimate Code (Windows only)

When we are strong enough, the fastest time and minimum memory has been unable to make us feel very brave, we began to pursue stimulation (evaluation machine shivering).

#include<Windows.h>
#include<cstdio>
using namespace std;
int ans;
int main(){
    new int[64225280];
    Sleep(990);
    for(int i=0;i<8;i++){
        if(getchar()=='1')ans++;
    }
    printf("%d",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/duimianlongge/p/12115902.html