CSP[2019.12]1.2.3题总结

CSP[2019.12]1.2.3题总结

1题找7倍数或者含有7的数。

//
//  main.cpp
//  [201912-1]count-num
//
//  Created by 陈冉飞 on 2020/2/9.
//  Copyright © 2020 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
int cnt = 0,num = 0,n,a[4];

bool check(int num){
    if (num%7 == 0) return true;
    while (num > 0) {
        if (num%10 == 7) return true;
        num/=10;
    }
    return false;;
}

int main(int argc, const char * argv[]) {
    scanf("%d",&n);
    while (cnt < n) {
        num++;
        if (check(num)) {a[(num-1)%4]++;cnt--;}
        cnt++;
    }
    printf("%d\n%d\n%d\n%d\n",a[0],a[1],a[2],a[3]);
    return 0;
}

第二题一开始想复杂了,想成搜索了,后来网页往下滚动看到了1e3果断循环暴力,结果还真是,,。

//
//  main.cpp
//  [201912-2]search-trush
//
//  Created by 陈冉飞 on 2020/2/9.
//  Copyright © 2020 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
#define maxn 1010
int n,x[maxn],y[maxn],cnt[maxn],flag[maxn],dir[8][2] = {{1,0},{-1,0},{0,-1},{0,1},{-1,-1},{1,-1},{-1,1},{1,1}},ans[5];


int main(int argc, const char * argv[]) {
    scanf("%d",&n);
    for (int i = 0; i < n; i++) scanf("%d%d",&x[i],&y[i]);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < 8; j++){
            for (int k = 0; k < n; k++)
                if (x[i] + dir[j][0] == x[k] && y[i] + dir[j][1] == y[k])
                    cnt[i]++;
            if (j == 3 && cnt[i] == 4) flag[i] = 1;
        }
    for (int i = 0; i < n; i++){
        if (flag[i]) ans[cnt[i]-4]++;
    }
    for (int i = 0; i < 5; i++)
        printf("%d\n",ans[i]);
    return 0;
}

第三题,,,菜炸了好tm麻烦,一堆坑,想法用的是原来数据结构课设里用的化简多项式的思路,一层一层括号的往下递归, 这个唯一不同的就是自变量稍微复杂一点,多了个大小写,然后系数是由前后共同决定的罢了,但是却写了一下午,,,然后还才50分。。。无语了,不过菜归菜,顺手复习了C++的string库

#include <string>  //具体哪个需要哪个记不太清了,
#include <cctype>  //#include <ctype.h>
// 截取 
str.substr(bg,length);
// 替换
str = str.replace(bg,length,goalstr);
// 插入
str.insert(index,insertstr);  // index 为插入之后的索引。
// 判断是否数字、大小写、字母
if (isdigit(str[i])){}
if (isupper(str[i])){}
if (islower(str[i])){}
if (isalpha(str[i])){}
// string to int
int num = stoi(str);

然后debug真是帮大忙了
中间索引啥的脑袋要炸掉了
在这里插入图片描述

最后附上50分代码后面接着改

//
//  main.cpp
//  [201912-3]chemistry-equation
//
//  Created by 陈冉飞 on 2020/2/9.
//  Copyright © 2020 陈冉飞. All rights reserved.
//

#include <iostream>
using namespace std;
struct alp{
    string name;
    int cnt;
    alp(string name,int cnt):name(name),cnt(cnt){}
};
int n;
#include <vector>
vector<alp> l;
vector<alp> r;
string str,strl,strr;
#include <string>
#include <ctype.h>
#include <cmath>

string trans(string str){
    int flag = 1;
    for (int i = 1; i < str.length(); i++) if (str[i] == '(')flag = 0;else if (str[i] == ')' && str[i+1] == ')') str.insert(i+1, "1");
    if (!flag) {
        for (int i = 1; i < str.length(); i++)
        if (str[i] == '(') {
            int ti = 0;
            for (int j = int(str.length()-1); j > i; j--){
                if (str[j] == ')') ti++;
                if (str[j] == ')' && ti == 2) {
                    int k = j+1;
                    for (; k < str.length() && isdigit(str[k+1]); k++) {}
                    str = str.replace(i, k-i+1, trans(str.substr(i,k-i+1)));
                }
            }
        }
    }
    // mutliply the coefficient solve ()x
    string transtr;
    int coef = 0;
    for (int i = int(str.length()-1); i >=0 && str[i] != ')'; i--)
        if (isdigit(str[i])) coef += (str[i]-'0')*pow(10,int(str.length()-1)-i);
    for (int i = 1; str[i] != ')'; i++)
        if (isupper(str[i]) && islower(str[i+1])) {
            if (isdigit(str[i+2])) {
                int j = i+2;
                for (; j < str.length() && isdigit(str[j+1]); j++) {}
                transtr += (str.substr(i,2)+to_string(coef*stoi(str.substr(i+2,j-i-1))));
            }else transtr += (str.substr(i,2)+to_string(coef));
        }else if (isupper(str[i])) {
            if (isdigit(str[i+1])) {
                int j = i+1;
                for (; j < str.length() && isdigit(str[j+1]); j++) {}
                transtr += (str.substr(i,1)+to_string(coef*stoi(str.substr(i+1,j-i))));
            }else transtr += (str.substr(i,1)+to_string(coef));
        }
    return transtr;
}

vector<alp> trans_to_vector(string str,vector<alp> b){
    if (isdigit(str[0])) {
        int di = 0;
        for (; isdigit(str[di+1]); di++) {}
        str = trans("("+str+")"+str.substr(0,di+1));
    }else str = trans("("+str+")1");
    
    for (int i = 0; i < str.length(); i++) {
        if (isupper(str[i]) && islower(str[i+1])) {
            int j = i+2;
            for (; j < str.length() && isdigit(str[j+1]); j++) {}
            int flag = 1;
            for (int k = 0; k < b.size(); k++)
                if (b[k].name == str.substr(i,2)) {b[k].cnt += stoi(str.substr(i+2,j-i-1));flag = 0;}
            if (flag) b.push_back(alp(str.substr(i,2),stoi(str.substr(i+2,j-i-1))));
        }else if (isupper(str[i])) {
            int j = i+1;
            for (; j < str.length() && isdigit(str[j+1]); j++) {}
            int flag = 1;
            for (int k = 0; k < b.size(); k++)
                if (b[k].name == str.substr(i,1)) {b[k].cnt += stoi(str.substr(i+1,j-i));flag = 0;}
            if (flag) b.push_back(alp(str.substr(i,1),stoi(str.substr(i+1,j-i))));
        }
    }
    return b;
}

bool solve(string str){
    for (int i = 0; i < str.length(); i++)
        if (str[i] == '=') strl = str.substr(0,i)+'+',strr = str.substr(i+1,str.length()-i)+'+';
    for (int bg = 0,i = 0; i < strl.length(); i++)
        if (strl[i] == '+') {l = trans_to_vector(strl.substr(bg,i-bg),l);bg = i+1;}
    for (int bg = 0,i = 0; i < strr.length(); i++)
        if (strr[i] == '+') {r = trans_to_vector(strr.substr(bg,i-bg),r);bg = i+1;}
    int flag = 1;
    for (int i = 0; i < l.size(); i++){
        int mark = 1;
        for (int j = 0; j < r.size(); j++)
            if (l[i].name == r[j].name && l[i].cnt == r[j].cnt) mark = 0;
        if (mark) {flag = 0;}
    }
    if (flag) return true;
    return false;
}

int main(int argc, const char * argv[]) {
    scanf("%d",&n);
    for (int i = 0; i < n; i++) {
        l.clear();r.clear();
        cin>>str;
        if(solve(str)){printf("Y\n");}
        else printf("N\n");
    }
}
发布了95 篇原创文章 · 获赞 19 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43345204/article/details/104241579