研究生机试备考练习(1)- 程序设计导引

2.1 2750 O(1)


#include <cstdio>

int main(){
    int a;
    scanf("%d",&a);
    if (a%2 == 0) printf("%d %d",(a+3)/4,a/2);
    else printf("0 0");
    return 0;
}

2.3 2808 O(M*L)


#include <cstdio>
#include <vector>
using namespace std;

int main(){
    int L,M;
    scanf("%d %d",&L,&M);
    vector<int>road(L+1);
    road = {};
    for(int i = 0;i<M;i++){
        int tempstart,tempend;
        scanf("%d %d",&tempstart,&tempend);
        for(int j = tempstart;j<=tempend;j++){
            road[j] = 1;
        }
    }
    int count = 0;
    for(int i = 0;i<L+1;i++){
        if(road[i] == 0)count++;
    }
    printf("%d",count);
    return 0;
}

2.4 2801 O(N*M)


#include <stdio.h>

int main(){
    int hashtable[26] = {};
    int M,N,P;
    scanf("%d %d %d",&N,&M,&P);
    for(int i = 0;i<N;i++){
        char tempstr[100];
        scanf("%s",tempstr);
        for(int j = 0;tempstr[j]!='\0';j++){
            hashtable[tempstr[j]-'A']++;
        }
    }
    for(int i = 0;i<P;i++){
        char tempstr[101];
        scanf("%s",tempstr);
        for(int j = 0;tempstr[j]!='\0';j++){
            hashtable[tempstr[j]-'A']--;
        }
    }
    for(int i = 0;i<26;i++){
        for(int j = 0;j<hashtable[i];j++){
            printf("%c",'A'+i);
        }
    }
    printf("\n");
    return 0;
}

2.5 1017


#include <bits/stdc++.h>
int main(){
    int N, a, b, c, d, e, f, y, x;//N 用来存储需要的箱子数目,y 用来存储 2*2 的空位数目 // x 用来存储 1*1 的空位数目。
    int u[4]={0, 5, 3, 1};
    //数组 u 表示 3*3 的产品数目分别是 4 的倍数,4 的倍数+1, 4 的倍数+2, 4 的倍数+3 //时,为 3*3 的产品打开的新箱子中剩余的 2*2 的空位的个数
    while(1){
        scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
        if (a == 0 && b == 0 && c == 0 && d == 0 && e == 0 && f == 0) break;
        N = f + e + d + (c + 3) / 4;
        //这里有一个小技巧 - (c+3)/4 正好等于 c 除以 4 向上取整的结果,下同
        y = 5 * d + u[c % 4];
        if(b > y) N += (b - y + 8 ) / 9;
        x = 36 * N - 36 * f - 25 * e - 16 * d - 9 * c - 4 * b; if(a>x) N+=(a-x+35)/36;
        printf("%d\n", N);
    }
    return 0;
}

2-4 2713


# include <cstdio>

int main(){
    int n,a,b,t,count = 0,c0 = 0;
    scanf("%d",&n);
    for (int i = 0;i<n;i++){
        a = -1;
        b = -1;
        for(int j = 0;j<n;j++){
            scanf("%d",&t);
            if (t == 0){
                c0 ++;
                if (a == -1) a = j;
                else b = j;
            }
        }
//        printf("%d %d\n",a,b);
        if (a != -1){
            if (b != -1)count += (b-a+1);
            else count += 1;
        }
    }
    printf("%d",count-c0);
    return 0;
}

3.1 2972 O(1)


#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;

int toN(string a,int n){
    int result = 0,weight = 1;
    reverse(a.begin(), a.end());
    for(int i = 0;i<a.size();i++){
        result += (a[i]-'0') * weight;
        weight *= n;
    }
    return result;
}

int main(){
    int p,q,r;
    scanf("%d %d %d",&p,&q,&r);
    string strp = to_string(p),strq = to_string(q),strr = to_string(r);
    char max = strp[0];
    for(int i = 0;i<strp.size();i++){
        if(strp[i] > max)max = strp[i];
    }
    for(int i = 0;i<strq.size();i++){
        if(strq[i] > max)max = strq[i];
    }
    for(int i = 0;i<strr.size();i++){
        if(strr[i] > max)max = strr[i];
    }

    int result = 0;

    for(int i = max-'0'+1;i<17;i++){
//        printf("%d %d %d %d \n",i,toN(strp,i), toN(strq,i),toN(strr,i));
        if (toN(strp,i) * toN(strq,i) == toN(strr,i)){
            result = i;
            break;
        }
    }
    printf("%d",result);

    return 0;
}

3.2 2973


#include <cstdio>
#include <cstring>

int to10(char skew[]){
    int result = 0,weight = 2;
    int length = strlen(skew);
    for(int i = length-1;i>=0;i--){
        result += (skew[i]-'0')* (weight -1);
        weight *=2;
    }
    return result;
}

int main(){
    char skew[40];
    while (1){
        scanf("%s",skew);
        int res = to10(skew);
        if(res == 0){
            break;
        }else{
            printf("%d\n",res);
        }
    }
    return 0;
}

4.2 2742


#include <cstdio>
#include <algorithm>
#include <cstring>

int main(){
    int n;
    scanf("%d",&n);
    for(int i = 0;i<n;i++){
        int hashtable[26] = {};
        char str1[1001];
        scanf("%s",str1);
        int length = strlen(str1);
        for(int i = 0;i<length;i++){
            hashtable[str1[i]-'a']++;
        }
        int max = hashtable[0],pos = 0;
        for(int i = 0;i<26;i++){
            if(max < hashtable[i]){
                max = hashtable[i];
                pos = i;
            }
        }
        printf("%c %d\n",char('a'+pos),max);
    }
    return 0;
}

4.3 2974 AC

要么用快排 qsort(),要么用 pq,否则超时;同时注意输入的字符串数组要开到足够大!


# include <cstdio>
# include <cstring>
# include <algorithm>
# include <queue>
using namespace std;

int n;
int lut[26] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
//int res[200001] = {};

int get(char t[]){
    int res = 0;
    for(int i = 0;i<strlen(t);i++){
        if (t[i] >= '0' && t[i] <= '9'){
            res += (int)(t[i]-'0');
            res *= 10;
        }
        else if (t[i] >= 'A' && t[i] <= 'Y'){
            res += lut[(int)(t[i] - 'A')];
            res *= 10;
        }
    }
//    printf("%d-\n",res/10);
    return res/10;
}

int main(){
    scanf("%d",&n);
    char temp[100];
    priority_queue<int,vector<int>,greater<int> >res;
    for(int i = 0;i<n;i++){
        scanf("%s",temp);
        res.push(get(temp));
    }
//    sort(res,res+n);
    int count = 1,is = 0,p = 0;
    for(int i = 1;i<n;i++){
        int top = res.top();
        res.pop();
        if(top == res.top()){
            if (!is) printf("%03d-%04d",top/10000,top%10000);
            count++;
            is = 1;
        }
        else {
            if (count > 1) {
                printf(" %d\n",count);
                p++;
            }
            count = 1;
            is = 0;
        }
    }
    if (count > 1) {
        printf(" %d\n",count);
        p++;
    }
    if (p == 0)printf("No duplicates.\n");
    return 0;
}


4.4 2744 AC 如果没有满足条件的子串,输出零


# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;

bool ischild(char a[],int n,char b[]){
//    printf("%s--%s--\n",a,b);
    if (strstr(b, a) != NULL)return 1;
    reverse(a,a+n);
    if (strstr(b, a) != NULL)return 1;
    return 0;
}

int main(){
    int n,m;
    scanf("%d",&m);
    for(;m;m--){
        scanf("%d",&n);
        char child[10000];
        char pa[100][10000];
        int lenmin = 10000,pos = 0,judge = 0;
        for(int i = 0;i<n;i++){
            scanf("%s",pa[i]);
            if (strlen(pa[i]) < lenmin){
                lenmin = (int)strlen(pa[i]);
                pos = i;
            }
        }
        for(int i = lenmin;i>0;i--){
            int ok = 0;
            for(int j = 0;j + i <= lenmin;j++){
                ok = 0;
                for (int m = j;m<i+j;m++){
                    child[m-j] = pa[pos][m];
                }child[i] = '\0';
                for(int p = 0;p<n;p++)
//                    printf("%d==",ischild(child,i,pa[p]));
                    if (!ischild(child,i,pa[p])){
                        ok = 1;
                        break;
                    }
//                printf("%s--%d\n",child,ok);
                if (ok == 0)break;
            }
            if (ok == 0){
                printf("%d\n",i);
                judge = 1;
                break;
            }
        }
        if (!judge)printf("0\n");
    }
    return 0;
}

4.5 2975



#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

char map[27] = "VWXYZABCDEFGHIJKLMNOPQRSTU";

int main(){
    char str[201];
    for(int i = 0;i<100;i++){
        cin.getline(str,200);
        if (strcmp(str,"ENDOFINPUT") == 0)break;
        cin.getline(str,200);
        for(int j = 0;str[j] != '\0';j++){
            if(str[j] >= 'A' && str[j] <= 'Z'){
                printf("%c",map[str[j]-'A']);
            }else{
                printf("%c",str[j]);
            }
        }
        printf("\n");
        cin.getline(str,200);
    }
    return 0;
}

2713


# 字符串 和 高精 用 python!
print("YES" if input().replace(" ","").lower() == input().replace(" ","").lower() else "NO")

1936


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
string s,t;
int main(){
    while(cin>>s>>t){
        int i,j;
        i=j=0;
        while(j<t.length() && i<s.length()){
            if(s[i]==t[j]){
                i++;
                j++;
            }
            else
                j++;
        }
        if(i>=s.length())
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}

5.1 2733


#include <cstdio>

bool isRun(int n){
    if (n%4 == 0){
        if (n%100 == 0){
            if (n% 400 != 0)return false;
            if (n% 3200 == 0)return false;
        }
        return true;
    }
    return false;
}

int main(){
    int n;
    scanf("%d",&n);
    if (isRun(n))printf("Y");
    else printf("N");
    return 0;

}

5.2 2712


#include <cstdio>
#include <cmath>

int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

int main(){
    int n;
    scanf("%d",&n);
    for(int ii = 0;ii<n;ii++){
        int m1,d1,begin,m2,d2;
        scanf("%d %d %d %d %d",&m1,&d1,&begin,&m2,&d2);
        int daycount = d2 - d1;
        for(int i = m1;i<m2;i++){
            daycount += months[i-1];
        }
//        printf("====%d====\n",daycount);
        printf("%d\n",(int)pow(2,daycount)*begin);
    }
    return 0;
}


5.3 2964


# include <cstdio>

int months[2][12] = {{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
char dows[7][10] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};

bool isRun(int n){
    if (n%4 == 0){
        if (n%100 == 0){
            if (n% 400 != 0)return false;
//            if (n% 3200 == 0)return false;
        }
        return true;
    }
    return false;
}

int getdate(int num,int date[]){
    int year,month,day,dow = num % 7;
    for(year = 2000;num>=0;year ++){    // 这里等号!!
        if(isRun(year))num-=366;
        else num-= 365;
    }
    num += isRun(year-1)?366:365;
    year--;
    int sgn = isRun(year);
    for(month = 1;num >= 0;month ++){
        num -= months[sgn][month-1];
    }
    num += months[sgn][month-2];
    month--;
    day = num;
    date[0] = year;
    date[1] = month;
    date[2] = day+1;
    return dow;

}

int main(){
    while(1){
        int num;
        scanf("%d",&num);
        if(num == -1){
            break;
        }
        int date[3],dow;
        dow = getdate(num, date);
        printf("%d-%02d-%02d %s\n",date[0],date[1],date[2],dows[(dow+6)%7]);

    }
    return 0;

}

5.4 2965


#include <cstdio>
#include <cstring>
//#include <algorithm>

char habmonth[19][10] = {"pop", "no", "zip", "zotz", "tzec", "xul", "yoxkin", "mol", "chen", "yax", "zac", "ceh", "mac", "kankin", "muan", "pax", "koyab", "cumhu","uayet"};
// uayet 不要忘!!
char tzchar[20][10] = {"imix", "ik", "akbal", "kan", "chicchan", "cimi", "manik", "lamat", "muluk", "ok", "chuen", "eb", "ben", "ix", "mem", "cib", "caban", "eznab", "canac", "ahau"};

int main(){
    int n;
    scanf("%d",&n);
    printf("%d\n",n);
    for(int round = 0;round < n;round ++){
        int day,year,num = 0,hamonth;
        char month[10];
        scanf("%d. %s %d",&day,month,&year);
        num += year * 365;
        for(hamonth = 0;hamonth<20;hamonth++){
            if(strcmp(month,habmonth[hamonth]) == 0)break;
        }
        num += hamonth * 20 + day;

        int tznum,tzyear;
        char tzch[10];

        tzyear = num/260;
        tznum = num % 13 + 1;
        strcpy(tzch,tzchar[num%20]);

        printf("%d %s %d\n",tznum,tzch,tzyear);
    }
    return 0;
}


5.5 2966


#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
char zone[32][10] = {"GMT","BST","IST","WET","WEST","CET","CEST","EET","EEST","MSK","MSD","AST","ADT","NST","NDT","EST","EDT","CST","CDT","MST","MDT","PST","PDT","HST","AKST","AKDT","AEST","AEDT","ACST","ACDT","AWST"};
double zonnum[32] = {0,1,1,0,1,1,2,2,3,3,4,-4,-3,-3.5,-2.5,-5,-4,-6,-5,-7,-6,-8,-7,-10,-9,-8,10,11,9.5,10.5,8};
int getDistance(char zone1[],char zone2[]){
    int i;
    for(i = 0;i<32;i++){
        if (strcmp(zone[i], zone1) == 0)break;
    }
    int j;
    for(j = 0;j<32;j++){
        if (strcmp(zone[j], zone2) == 0)break;
    }
    return (zonnum[j] - zonnum[i])*60;
}
int main(){
    int n;
    scanf("%d\n",&n);
    for(int i = 0;i<n;i++){
        int hour = 0,minute = 0;
        char zone1[10],zone2[10],ap;
        char line[100];
        cin.getline(line,100);
        if (line[0]>='0' && line[0] <= '9'){
            sscanf(line, "%d:%d %c.m. %s %s",&hour,&minute,&ap,zone1,zone2);
            if(ap != 'a' && hour != 12)hour += 12;
            if(ap == 'a' && hour == 12)hour -= 12;
            // 这里反人类的 apm 12点!!
        }else if (line[0] == 'n'){
            sscanf(line, "noon %s %s",zone1,zone2);
            hour = 12;minute = 0;
        }else {
            sscanf(line, "midnight %s %s",zone1,zone2);
            hour = 0;minute = 0;
        }

        int origin = 0,res = 0;
        origin = minute + hour * 60;
//      printf("___%d__%d__%c___\n",hour,minute,ap);
//      printf("___%s__%s__%d___\n",zone1,zone2,getDistance(zone1, zone2));
        res = (origin + getDistance(zone1, zone2) + 14400)%1440;
        res = res % 1440;
        if (res == 0)printf("midnight\n");
        else if( res > 0 && res < 720)
            printf("%d:%02d a.m.\n",res/60==0?12:res/60,res%60);
        else if (res == 720)printf("noon\n");
        else
            printf("%d:%02d p.m.\n",res/60-12==0?12:res/60-12,res%60);
        }
    return 0;
}

6.1 2746


#include <cstdio>
#include <vector>
using namespace std;

int main(){
    while(1){
        int n,m;
        scanf("%d %d",&n,&m);
        if (n == 0)break;
        vector<int>monkey(n,0);
        int count = 0,j = 0;
        for(int i = 0;i<n-1;){
            if (monkey[(j++)%n] == 0)count ++;
            if (count == m){
                count = 0;
                monkey[(j-1)%n] = 1;
//                j++;
                i++;
//                for(int k = 0;k<n;k++){
//                    printf(" %d",monkey[k]);
//                }
//                printf("\n");
            }
        }
        for(int i = 0;i<n;i++){
            if (monkey[i] == 0){
                printf("%d\n",i+1);
            }
        }
    }
    return 0;
}

6.2 2950 WA TODO

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

struct point{
    int p,m,n;
    point(int _p,int _m,int _n):p(_p),m(_m),n(_n){};
};

bool cmp(point a,point b){
    return a.p > b.p;
}

int main(){
    int T;
    scanf("%d",&T);
    for (int round = 0;round < T; round ++){
        int m,n,k;
        scanf("%d %d %d",&m,&n,&k);
        vector<point>penuts;
        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                int temp;
                scanf("%d",&temp);
                if(temp != 0){
                    penuts.push_back(point(temp, i+1, j+1));
                }
            }
        }
        penuts.push_back(point(0, 0, 0));
        sort(penuts.begin(),penuts.end(),cmp);
//        for(int i = 0;i<penuts.size();i++){
//            printf("%d-%d-%d\n",penuts[i].m,penuts[i].n,penuts[i].p);
//        }
        int result = 0,i = 0;

        // 找到第一个最大的 若最大的不行,也不能继续了
        for(i = 0;i<1;i++){
            if (k >= (penuts[i].m * 2 + 1))
            {
                result += penuts[i].p;
                k -= penuts[i++].m;
                k -= 1;
//                printf("k = %d res = %d\n",k,result);
                break;
            }else{
                printf("0\n");
                return 0;
            }
        }

        // 继续遍历到最后
        while (i<penuts.size())
        {
            if (k >= (penuts[i].m + abs(penuts[i].m - penuts[i-1].m) + abs(penuts[i].n - penuts[i-1].n))){
                result += penuts[i].p;
                k -= abs(penuts[i].n-penuts[i-1].n);
                k -= abs(penuts[i].m-penuts[i-1].m);
                k--;
//                printf("k = %d res = %d\n",k,result);
            }
            i++;
        }
        printf("%d\n",result);

    }
    return 0;
}

6.2 2950 WA TODO

http://bailian.openjudge.cn/practice/2950/

# include <cstdio>
# include <vector>
# include <algorithm>
using namespace std;
struct node{
    int x,y,v;
    node(int _x,int _y,int _v):x(_x),y(_y),v(_v){}
};
int xx,yy,length,t;
vector<node>lst;
bool cmp(node a,node b){    // 按花生从大到小排序
    return a.v > b.v;
}
int gety(node a){return a.y;}   // 返回到路边距离
int getl(node a,node b){    // 返回两点距离
    if (a.x == 0)return b.y;
    return (a.y > b.y?(a.y-b.y):(b.y-a.y))+(a.x > b.x?(a.x-b.x):(b.x-a.x));
}
int main(){
    int ppp;
    scanf("%d",&ppp);
    for(;ppp;ppp--){    // ppp 组数据,题目样例少个 1
        
        // 输入,有花生的放到列表里
        scanf("%d%d%d",&xx,&yy,&length);
        for(int i = 1;i<=xx;i++){
            for(int j = 1;j<=yy;j++){
                scanf("%d",&t);
                if (t)lst.push_back(node(j,i,t));
            }
        }
        
        if (lst.size() == 0 || length == 0){
            printf("0\n");
            break;
        }
        sort(lst.begin(), lst.end(), cmp);
        //    for(int i = 0;i<lst.size();i++)
        //        printf("%d-%d-%d ",lst[i].x,lst[i].y,lst[i].v);
        node now  = node(0,0,0);
        int res = 0;
        for(int i = 0;i<lst.size();i++){    // 从大到小遍历,能去就去,不能去就停
            //        printf("%d:",length);
            if (length > getl(now, lst[i]) + gety(lst[i])){
                //            printf("-%d-",lst[i].v);
                length -= (getl(now, lst[i])+1);
                res += lst[i].v;
                now = lst[i];
            }
            else break;
        }
        printf("%d",res);
    }
    return 0;
}

6.3 2745 PE TODO


#include <cstdio>
#include <string>
using namespace std;
// 上 左上 右上 中 左下 右下 下
char ch[7][11] = {"- -- -----","|   ||| ||","|||||  |||","  ----- --","| |   | | ","|| |||||||","- -- -- --"};

void toArray(int m,int array[]){
    string str = to_string(m);
    int length = (int)str.size();
    for(int i = length-1;i>=0;i--){
        array[i] = m % 10;
        m /= 10;
    }
//    printf("Array: ");
//    for (int i = 0;i<10;i++){
//        printf(" %d",array[i]);
//    }
//    printf("\n");
}

void draw_(int n, int array[], int k, int judge){
    for(int i = 0;i<10 && array[i] != -1;i++){

        printf(" ");

        if(ch[k][array[i]] == '-'){
            for(int j = 0;j<n;j++)printf("-");
        }else{
            for(int j = 0;j<n;j++)printf(" ");
        }

        // judge 是为了最后一行的最后不输出空格
        if (judge == 0 && (i == 9 || array[i+1] == -1));
        else printf(" ");

        if (i != 9){
            if (array[i+1] != -1)printf(" ");
        }
    }
}
void draw1(int n, int array[], int k1, int k2){
    for(int i = 0;i<10 && array[i] != -1;i++){

        if(ch[k1][array[i]] == '|'){
            printf("|");
        }else printf(" ");

        for(int j = 0;j<n;j++)printf(" ");

        if(ch[k2][array[i]] == '|'){
            printf("|");
        }else printf(" ");

        if (i != 9){
            if (array[i+1] != -1)printf(" ");
        }
    }

}

void draw(int n, int array[],int p){
    if (p == 1) printf("\n");
    draw_(n,array,0,1);
    printf("\n");
    for(int i = 0;i<n;i++){
        draw1(n,array,1,2);
        printf("\n");
    }
    draw_(n,array,3,1);
    printf("\n");
    for(int i = 0;i<n;i++){
        draw1(n,array,4,5);
        printf("\n");
    }
    draw_(n,array,6,p);
}

int main(){
    int isfirst = 1;
    int n,m;
    scanf("%d %d",&n,&m);
    while(1){
        if (isfirst == 1){
            isfirst ++;
        }else{
            printf("\n");
        }
        int array[10] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
        toArray(m,array);
        int n1 = n;
        scanf("%d %d",&n,&m);
        if (n == 0){
            draw(n1,array,0);
            printf("\n");
            break;
        }else{
            draw(n1,array,1);
        }
    }
    return 0;
}

6.4 1833 TODO 手写


#include <cstdio>
#include <algorithm>
using namespace std;

int main(){
    int m;
    scanf("%d",&m);
    for(int ii = 0;ii<m;ii++){
        int n,k;
        scanf("%d %d",&n,&k);
        int a[1024] = {};
        for( int i = 0;i<n;i++){
            scanf("%d",&a[i]);
        }
        for(int i = 0;i<k;i++){
            next_permutation(a,a+n);
        }
        for( int i = 0;i<n;i++){
            printf("%d",a[i]);
            if(i != n-1)printf(" ");
        }
        printf("\n");

    }
    return 0;
}

1835 AC


# include <cstdio>
int pos[3] = {0,0,0},d[3] = {1,0,0};

void plus(int p){
    for(int i = 0;i<3;i++)
        pos[i] += d[i] * p;
}

int main(){
    int m;
    scanf("%d",&m);
    for(;m;m--){
        int n,step;
        char dir[10];
        for (int i = 0;i<3;i++){pos[i] = 0;d[i] = 0;}
        d[0] = 1;
        int h[3] = {0,0,1},l[3] = {0,-1,0};
        scanf("%d",&n);
        for(;n;n--){
            scanf("%s %d%*c",dir,&step);
            //        printf("%s %d\n",dir,step);
            if (dir[0] == 'f');
            else if (dir[0] == 'b') {
                d[0] = -d[0];d[1] = -d[1];d[2] = -d[2];
                l[0] = -l[0];l[1] = -l[1];l[2] = -l[2];
            }
            else if (dir[0] == 'u') {
                int a = d[0],b = d[1],c = d[2];
                d[0] = h[0];d[1] = h[1];d[2] = h[2];
                h[0] = -a;h[1] = -b;h[2] = -c;
            }
            else if (dir[0] == 'd'){
                int a = d[0],b = d[1],c = d[2];
                d[0] = -h[0];d[1] = -h[1];d[2] = -h[2];
                h[0] = a;h[1] = b;h[2] = c;
            }else if (dir[0] == 'l'){
                int a = d[0],b = d[1],c = d[2];
                d[0] = l[0];d[1] = l[1];d[2] = l[2];
                l[0] = -a;l[1] = -b;l[2] = -c;
            }else if (dir[0] == 'r'){
                int a = d[0],b = d[1],c = d[2];
                d[0] = -l[0];d[1] = -l[1];d[2] = -l[2];
                l[0] = a;l[1] = b;l[2] = c;
            }
            plus(step);
            //        printf("%d %d %d ----\n",pos[0],pos[1],pos[2]);
            
        }
        printf("%d %d %d ",pos[0],pos[1],pos[2]);
        if (d[0] == 1)printf("0");
        else if (d[0] == -1)printf("3");
        else if (d[1] == 1)printf("1");
        else if (d[1] == -1)printf("4");
        else if (d[2] == 1)printf("2");
        else if (d[2] == -1)printf("5");
        printf("\n");
    }
    return 0;
}

2964 AC


s = input()
while(len(s) > 1):
    # n = int(s)
    n = 0
    for i in s:
        n += int(i)
    s = str(n)
print(s)

2952 AC


def next(s):
    s += s[0]
    s = s[1:]
    return s

n = input()
l = len(n)
s = n
sgn = 0
for i in range(l):
    # print(s)
    if (int(s) % int(n) != 0 or s == next(s) or int(s) / int(n) > l):
        print(0)
        sgn = 1
        break
    s = next(s)
if sgn == 0:
    print(1)    

7.1 2981


#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 210;

string pluss(string a,string b){
    int i = (int)a.size(),j = (int)b.size();
    int cc = 0,temp = 0,k = 0;
    string c;
    int p = max(i,j);
    string place(p,'0');

//    cout<<place;
    a = place + a; i += p;
    b = place + b; j += p;
    for(k = 0; k < p; k ++){
        temp = a[--i] + b[--j] + cc - '0' - '0';
        cc = temp/10;
        temp %= 10;
        c += char(temp + '0');
    }
    if (cc != 0){
        c += char(cc + '0');
    }
//    cout<<c<<endl;
    string c1 = c;
    reverse(c1.begin(),c1.end());
    int iii = (int)c1.size() - 1;
    for(int i = 0;i<iii;i++){
//        cout<<c1[i]<<endl;
        if (c1[0] == '0'){
            c1.erase(c1.begin());
//            cout<<c1<<endl;
        }else{
            break;
        }
    }

    return c1;
}

int main(){
    char a[maxn],b[maxn];
    scanf("%s %s",a,b);
    string a1 = a;
    string b1 = b;
    string res = pluss(a1,b1);
    printf("%s",res.c_str());
    return 0;

}

7.2 2980


#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
//#include <iostream>
using namespace std;
const int maxn = 210;

string pluss(string a,string b){
    int i = (int)a.size(),j = (int)b.size();
    int cc = 0,temp = 0,k = 0;
    string c;
    int p = max(i,j);
    string place(p,'0');
//    cout<<place;
    a = place + a; i += p;
    b = place + b; j += p;
    for(k = 0; k < p; k ++){
        temp = a[--i] + b[--j] + cc - '0' - '0';
        cc = temp/10;
        temp %= 10;
        c += char(temp + '0');
    }
    if (cc != 0){
        c += char(cc + '0');
    }
//    cout<<c<<endl;
    string c1 = c;
    reverse(c1.begin(),c1.end());
    int iii = (int)c1.size() - 1;
    for(int i = 0;i<iii;i++){
//        cout<<c1[i]<<endl;
        if (c1[0] == '0'){
            c1.erase(c1.begin());
//            cout<<c1<<endl;
        }else{
            break;
        }
    }

    return c1;
}

string mul1(char a,string b){
    int reala = a - '0';

//    cout<< reala<<"  "<< b<<endl;
    string c;
    int cc = 0,temp;
    for(int i = int(b.size()-1);i>=0;i--){
        temp = (b[i] - '0') * reala + cc;
        cc = temp / 10;
        temp %= 10;
        c += char(temp + '0');
    }
    if (cc != 0) c += char (cc + '0');
    reverse(c.begin(), c.end());
    return c;
}

string mul(string a,string b){
    string result = "0";
    for (int i = 0;i < a.size();i++){

//        for(int j = 0;j<i;j++){printf(" ");}
//        cout<<mul1(a[i],b)<<endl;
        result = pluss(result+"0",mul1(a[i],b));
    }
    return result;
}

int main(){
    char a[maxn],b[maxn];
    scanf("%s %s",a,b);
    string a1 = a;
    string b1 = b;
//    printf("\n");
    string res = mul(a1,b1);
    printf("%s",res.c_str());
    return 0;

}

7.4 2737 AC

import math
a = int(input())
res = 1
b=pow(2,a,100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
print(int(a * math.log(2,10))+1)
r = str("%0500d" % (b-1))
for i in range(10):
    print(r[i*50:(i+1)*50])

2738 最简单的还是 java


a = input().split(".")
b = input().split(".")
if len(a[1]) > len(b[1]):
    db = int(b[1]) * pow(10,(len(a[1]) - len(b[1])))
    da = int(a[1])
    l = len(a[1])
else:
    da = int(a[1]) * pow(10,(len(b[1]) - len(a[1])))
    db = int(b[1])
    l = len(b[1])

d = da+db
d = str(d)
ld = len(d)
for i in range(len(d)):
    if d[-1] == '0':
        d = d[:-1]
    else:
        break
if ld > l:
    print(str(int(a[0])+int(b[0])+1) + "." + "0" * (l-ld) + d[1:])
else:
    print(str(int(a[0])+int(b[0])) + "." + "0" * (l-ld) + d)
# 2951 AC
while(1):   # python 循环输入
    try:
        s = input()
        s = s.split(" ")
        for i in range(len(s[0])):
            if s[0][-1] == '0':
                s[0] = s[0][:-1]
            else:
                break
        t = int(s[-1])
        s = s[0].split(".")
        c = int(s[0] + s[-1])
        res = str(pow(c,t))
        l = t*len(s[1])
        if l == 0:
            print(res)
        elif len(res) > l:
            print((res[:-l])+'.'+res[-l:])
        else:
            print("." + "0"*(l-len(res)) + res)
    except:
        break

8.2 2977 P177 改进方法不错值得借鉴


# include <cstdio>

int main(){
    int p,e,i,d,result;
    scanf("%d %d %d %d",&p,&e,&i,&d);
    for(int j = 1;;j++){
        if ((j * 33 + i - e)%28 == 0 && (j * 33 + i - p)%23 == 0){
            result = j * 33 + i - d;
            break;
        }
    }
    printf("%d",result);
    return 0;
}

8.3 2692 AC


m = int(input())
for repeat in range(m):
    l = [[],[],[]]
    l[0] = input().split(" ")
    l[1] = input().split(" ")
    l[2] = input().split(" ")
    a = "ABCDEFGHIJKLMN"
    for i in range(12):
        islight = 0
        for j in range(3):
            if l[j][2] == "even" and a[i] in l[j][0]+l[j][1]:
                islight = 0
                break
            elif l[j][2] == "up":
                if a[i] in l[j][0]:
                    if islight > -1:
                        islight = 1
                    else:
                        islight = 0
                        break
                elif a[i] in l[j][1]:
                    if islight < 1:
                        islight = -1
                    else:
                        islight = 0
                        break
                else:
                    islight = 0
                    break
            elif l[j][2] == "down":
                if a[i] in l[j][1]:
                    if islight > -1:
                        islight = 1
                    else:
                        islight = 0
                        break
                elif a[i] in l[j][0]:
                    if islight < 1:
                        islight = -1
                    else:
                        islight = 0
                        break
                else:
                    islight = 0
                    break
        if islight == -1:
            print("%c is the counterfeit coin and it is light." % a[i])
            break
        elif islight == 1:
            print("%c is the counterfeit coin and it is heavy." % a[i])
            break

8.4 2810 AC


# 2020-03-05 09:37:32
n = int(input())
# n = 110
p = [0]*110
for i in range(n+1):
    p[i] = pow(i,3)
for a in range(2,n+1):
    sgn = 0
    for b in range(2,a):
        for c in range(b,a):
            for d in range(c,a):
                resl = p[a]
                resr = p[b] + p[c] + p[d]
                if resl == resr:
                    # print(str([a,b,c,d]) + ',')
                    print("Cube = %d, Triple = (%d,%d,%d)" % (a,b,c,d))

8.4 2810 AC 打表


lst = [[6, 3, 4, 5],[12, 6, 8, 10],[18, 2, 12, 16],[18, 9, 12, 15],[19, 3, 10, 18],[20, 7, 14, 17],[24, 12, 16, 20],[25, 4, 17, 22],[27, 3, 18, 24],[28, 18, 19, 21],[29, 11, 15, 27],[30, 15, 20, 25],[36, 4, 24, 32],[36, 18, 24, 30],[38, 6, 20, 36],[40, 14, 28, 34],[41, 2, 17, 40],[41, 6, 32, 33],[42, 21, 28, 35],[44, 16, 23, 41],[45, 5, 30, 40],[46, 3, 36, 37],[46, 27, 30, 37],[48, 24, 32, 40],[50, 8, 34, 44],[53, 29, 34, 44],[54, 6, 36, 48],[54, 12, 19, 53],[54, 27, 36, 45],[56, 36, 38, 42],[57, 9, 30, 54],[58, 15, 42, 49],[58, 22, 30, 54],[60, 21, 42, 51],[60, 30, 40, 50],[63, 7, 42, 56],[66, 33, 44, 55],[67, 22, 51, 54],[69, 36, 38, 61],[70, 7, 54, 57],[71, 14, 23, 70],[72, 8, 48, 64],[72, 34, 39, 65],[72, 36, 48, 60],[75, 12, 51, 66],[75, 38, 43, 66],[76, 12, 40, 72],[76, 31, 33, 72],[78, 39, 52, 65],[80, 28, 56, 68],[81, 9, 54, 72],[81, 25, 48, 74],[82, 4, 34, 80],[82, 12, 64, 66],[82, 19, 60, 69],[84, 28, 53, 75],[84, 42, 56, 70],[84, 54, 57, 63],[85, 50, 61, 64],[87, 20, 54, 79],[87, 26, 55, 78],[87, 33, 45, 81],[87, 38, 48, 79],[88, 21, 43, 84],[88, 25, 31, 86],[88, 32, 46, 82],[89, 17, 40, 86],[90, 10, 60, 80],[90, 25, 38, 87],[90, 45, 60, 75],[90, 58, 59, 69],[92, 6, 72, 74],[92, 54, 60, 74],[93, 32, 54, 85],[95, 15, 50, 90],[96, 19, 53, 90],[96, 48, 64, 80],[97, 45, 69, 79],[99, 11, 66, 88],[100, 16, 68, 88],[100, 35, 70, 85],[102, 51, 68, 85],[103, 12, 31, 102],[105, 33, 70, 92],[106, 58, 68, 88],[108, 12, 72, 96],[108, 13, 51, 104],[108, 15, 82, 89],[108, 24, 38, 106],[108, 54, 72, 90],[110, 29, 75, 96]]

for i in range(len(lst)):
    if lst[i][0] <= n:
        print("Cube = %d, Triple = (%d,%d,%d)" % (lst[i][0],lst[i][1],lst[i][2],lst[i][3]))


8.5 2811


# include <cstdio>

void to2(int n,int res[][8]){
    for(int i = 0;i<6;i++){
        res[1][i+1] = n % 2;
        n /= 2;
    }
}

bool fill(int light[][8], int op[][8]){
    for (int i = 2;i<6;i++){
        for(int j = 1;j<7;j++){
            op[i][j] = (op[i-1][j-1] + op[i-1][j+1] + op[i-2][j] + op[i-1][j] + light[i-1][j]) % 2;
        }
    }
    for (int i = 2;i<6;i++){
        for(int j = 1;j<7;j++){
            if ((light[i][j] + op[i][j] + op[i-1][j] + op[i][j-1] + op[i+1][j] + op[i][j+1]) % 2 == 1 )return false;
        }
    }
    return true;
}

void print(int op[][8]){
    for (int i = 1;i<6;i++){
        for(int j = 1;j<7;j++){
            printf("%d",op[i][j]);
            if (j == 6)printf("\n");
            else printf(" ");
        }
    }
}

int main(){
    int light[8][8] = {},op[8][8] = {};
    for (int i = 0;i<5;i++){
        for (int j = 0;j<6;j++){
            scanf("%d",&light[i+1][j+1]);
        }
    }
    for(int i = 0;i<64;i++){
        to2(i,op);
        if(fill(light, op)) print(op);
//        printf("\n");
    }
    return 0;
}

8.6 2812 WA TODO http://bailian.openjudge.cn/practice/2812/


# include <cstdio>
# include <vector>
# include <algorithm>
//# include <cstring>
using namespace std;
const int maxn = 5010;
int fieldhash[maxn][maxn] = {};// fieldhash[i][j] = 1:是踩踏点

struct point{
    int x,y;
    point(int _x,int _y):x(_x),y(_y){};
};

bool cmp(point a,point b){
    if(a.x != b.x)return a.x < b.x;
    else return a.y < b.y;
}
// 包含 i j 两点的直线上,有多少个踩踏点
int getlength(int i,int j,int r,int c,vector<point>field,int fieldhash[][maxn]){

    int length = 2,existl = 1/*左上可以延伸*/,existr = 1/*右下可以延伸*/;

    // 向左上延伸
    point now = field[i],last = field[j];
    while(existl){
        if ((now.x * 2 - last.x) > 0 && (now.y * 2 - last.y) > 0){
            if (fieldhash[now.x * 2 - last.x][now.y * 2 - last.y] == 1){
                point temp = now;
                now = point(now.x * 2 - last.x,now.y * 2 - last.y);
                last = temp;
                length ++;
            }else return 0; // 如果下一步是空的,则不符合题目要求,长度是0!!
        }else existl = 0;
    }

    // 向右下延伸
    now = field[i],last = field[j];
    while(existr){
        if ((last.x * 2 - now.x) <= r && (last.y * 2 - now.y) <= c){
            if (fieldhash[last.x * 2 - now.x][last.y * 2 - now.y] == 1){
                point temp = last;
                last = point(last.x * 2 - now.x,last.y * 2 - now.y);
                now = temp;
                length ++;
            }else return 0;
        }else existr = 0;
    }
//    printf("当前两起始点及路长 %d(%d,%d) %d(%d,%d) %d\n",i,field[i].x,field[i].y,j,field[j].x,field[j].y,length);
    return length;
}

int main(){

    int r,c,n,tempx,tempy;
    scanf("%d %d %d",&r,&c,&n);

    vector<point> field;//field 为所有踩踏点的链表

    for(int i = 0;i<n;i++){
        scanf("%d %d",&tempx,&tempy);
        field.push_back(point(tempx,tempy));
        fieldhash[tempx][tempy] = 1;
    }

//    sort(field.begin(),field.end(),cmp);
//    for(int i = 0;i<field.size();i++){
//        printf("%4d,(%d,%d)-\n",i,field[i].x,field[i].y);
//    }
    int maxlength = 0;// 包含最多踩踏点的数量
    int maxmax = max(r,c);//达到最多的情况,剪枝,见*

    // 任取两个踩踏点,连成直线,判断直线上有多少踩踏点
    for(int i = 0;i<field.size()-1;i++){
        for(int j = i+1;j<field.size();j++){

            // 剪枝,先把相差太远的两个起始点排除
            point now = field[i],last = field[j];
            if (abs(now.x - last.x) >=3 || abs(now.y - last.y) >= 3)break;

            maxlength = max(maxlength,getlength(i, j, r, c, field, fieldhash));
            if (maxlength == maxmax){//*
                printf("%d",maxmax);
                return 0;
            }
        }
    }

    printf("%d",maxlength>2?maxlength:0);
    return 0;
}
//int main(){
//    printf("6 7\n42\n");
//    for(int i = 1;i<7;i++){
//        for(int j = 1;j<8;j++){
//            printf("%d %d\n",i,j);
//        }
//    }
//}

2739 AC


p = [1]*30
a = int(input())
b = int(input())

def get(x):
    if (p[x] > 1):
        return p[x]
    else:
        p[x] = pow(a,x)
        return p[x]

for i in range(30):
    if get(i) <= b and b < get(i+1):
        print(i)
        break

2747 AC


n = int(input())
maxs = 0
for a in range(0,n+1)[::-1]:
    for b in range(0,n+1)[::-1]:
        for c in range(0,n+1)[::-1]:
            if (a+b)%2 == 0 and (b+c)%3 == 0 and (a+b+c)%5 == 0:
                maxs =  maxs if maxs >= a+b+c else a+b+c
print(maxs) 

9.2 2753


# include <cstdio>

int fib(int n,int max,int a[]){
    if (n <= 6)return a[n];
    for(int i = 7;i<=n;i++){
        a[i] = a[i-1] + a[i-2];
    }
    return a[n];
}

int main(){
    int n,temp,a[21] = {1,1,1,2,3,5,8},max = 6;
    scanf("%d",&n);
    for(int i = 0;i<n;i++){
        scanf("%d",&temp);
        printf("%d\n",fib(temp,max,a));
    }
    return 0;
}

9.3 2756


# include <cstdio>
# include <vector>
# include <algorithm>
using namespace std;

int main(){
    int a,b;
    scanf("%d %d",&a,&b);
    vector<int>fa;
    while (a>1) {
        fa.push_back(a);
        a /= 2;
    }
    fa.push_back(1);

    int sgn = 0;
    while (b>1) {
        if (find(fa.begin(),fa.end(),b) != fa.end()){
            printf("%d",b);
            sgn = 1;
            break;
        }
        b /=2;
    }
    if (sgn == 0)printf("1");
    return 0;
}

9.4 2694 太妙了


# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;

double exp(){
    char temp[100];
    scanf("%s",temp);
    switch(temp[0]){
        case '+':return exp() + exp();
        case '-':return exp() - exp();
        case '*':return exp() * exp();
        case '/':return exp() / exp();
        default: return atof(temp);
    }
}
int main(){
    double ans = exp();
    printf("%lf\n",ans);
    return 0;
}

9.5 1664 妙


# include <cstdio>

int solution(int m,int n){
    if (n == 1 || m == 0)return 1;
    if (m < n)return solution(m, m);
    return  solution(m, n-1) + solution(m-n, n);
}

int main(){
    int t,m,n;
    scanf("%d",&t);
    for(int ii = 0;ii<t;ii++){
        scanf("%d %d",&m,&n);
        printf("%d\n",solution(m, n));
    }
    return 0;
}

9.6 2816


# include <cstdio>

const int maxn = 22;

void DFS(int graph[][maxn],int vis[][maxn],int i,int j){
    vis[i][j] = 1;
    if (graph[i-1][j] == 1 && vis[i-1][j] == 0)
        DFS(graph,vis,i-1,j);
    if (graph[i+1][j] == 1 && vis[i+1][j] == 0)
        DFS(graph,vis,i+1,j);
    if (graph[i][j-1] == 1 && vis[i][j-1] == 0)
        DFS(graph,vis,i,j-1);
    if (graph[i][j+1] == 1 && vis[i][j+1] == 0)
        DFS(graph,vis,i,j+1);
}

int main(){
    int w,h,startx = 0,starty = 0;
    while(1){
        scanf("%d %d",&w,&h);
        // 每次别忘记清零!
        int vis[maxn][maxn] = {0},graph[maxn][maxn] = {0};
        if (w == 0)break;
        for (int i = 1;i<=h;i++){
            // 测试数据每行没有空格!
            scanf("\n");
//            printf("\n");
            for(int j = 1;j<=w;j++){
                char temp;
//                printf("%d-%d-%c ",i,j,temp);
                scanf("%c",&temp);
//                printf("%c ", temp);
                if (temp == '.')graph[i][j] = 1;
                else if (temp == '@'){
                    graph[i][j] = 1;
                    startx = i;
                    starty = j;
                }
            }
//            printf("\n");
        }
//        printf("\n");
//        for (int i = 0;i<h+2;i++){
//            for(int j = 0;j<w+2;j++){
//                printf("%d ",graph[i][j]);
//            }
//            printf("\n");
//        }
        DFS(graph,vis,startx,starty);
        int count = 0;
        for (int i = 0;i<h+2;i++){
            for(int j = 0;j<w+2;j++){
                count += vis[i][j];
//                printf("%d ",vis[i][j]);
            }
//            printf("\n");
        }

        printf("%d\n",count);
    }
    return 0;
}

9.7 2754

# include <iostream>
using namespace std;
int c[20], n=8, cnt=0,result[93],counti = 0;
int print(){

    int res = 0;

    for(int i=0; i<n; ++i){
        res = res * 10 + c[i]+1;
    }
    return res;
}
void search(int r){
    if(r == n){
        result[counti++] = print();
        ++cnt;
        return;
    }
    for(int i=0; i<n; ++i){
        c[r] = i;
        int ok = 1;
        for(int j=0; j<r; ++j)
            if(c[r]==c[j] || r-j==c[r]-c[j] || r-j==c[j]-c[r]){
                ok = 0;
                break;
            }
        if(ok) search(r+1);
    }
}
int main(){
    int p,q;
    search(0);
    scanf("%d",&p);
    for(int i = 0;i<p;i++){
        scanf("%d",&q);
        printf("%d\n",result[q-1]);
    }
    return 0;
}

9.8 2817 不会

# include <cstdio>
# include <vector>
using namespace std;

bool meet(int k,int a[],int n){
    vector<int>vis(n,0);
    for(int i = 0;i<n;i++){
        for(int j = i;j<n;j++){
            if (a[j] + a[j] == k && ){
                vis[i] = 1;
                vis[j] = 1;
            }
        }
    }
}

int main(){

    while(1){
        int n;
        scanf("%d",&n);
        if (n == 0)break;
        int a[n],maxn = 1,res;
        for(int i = 0;i<n;i++){
            scanf("%d",&a[i]);
            maxn = maxn>a[i]?maxn:a[i];
        }
        for(res = maxn;res++;){
            if(meet(res,a,n)){
                printf("%d",res);
                return 0;
            }
        }
    }
    return 0;
}

10.1 2760 有问题 WA

# include <cstdio>
# include <algorithm>
using namespace std;
const int maxn = 30;

int main(){
    int n,tri[maxn] = {},sum[maxn] = {};
    scanf("%d",&n);
    scanf("%d",&tri[1]);
    sum[1] = tri[1];
    int count = 1;
    for(int i = 2;i<=n*(n+1)/2;i++){
        scanf("%d",&tri[i]);
        if (i != count*(count+1)/2 && i-1 != count*(count+1)/2)
            sum[i] = tri[i] + max(sum[i/2],sum[i/2+1]);
        else if(i == count*(count+1)/2 + 1 && i > 6){
            sum[i] = tri[i] + sum[i/2 + 1];
            count ++;
        }
        else if(i == count*(count+1)/2 + 1){
            sum[i] = tri[i] + sum[i/2];
            count ++;
        }else{
            sum[i] = tri[i] + sum[i/2];
        }
    }


    count = 1;
    printf("\n");
    for(int i = 1;i<=n*(n+1)/2;i++){
        printf("%3d ",sum[i]);
        if(i == count*(count+1)/2){
            printf("\n");
            for(int j = 0;j<(n - count);j++){
                printf("  ");
            }
            count++;
        }
    }

    int maxsum = sum[n*(n-1)/2];
    for(int i = n*(n-1)/2;i<=n*(n+1)/2;i++){
        maxsum = maxsum > sum[i]?maxsum:sum[i];
    }
    printf("%d",maxsum);
    return 0;
}

noi 8469 WA

# include <cstdio>
# include <cstring>
using namespace std;
char a[32],b[32];
int count = 0,countb = 1;

int main(){
    scanf("%s %s",a,b);
//    printf("%s--%s--",a,b);
    if (strlen(a) <= 2){
        if (strcmp(a, b) == 0)printf("0");
        else if (a[0] != b[0] && a[strlen(a)-1] != b[strlen(a)-1])printf("1");
        else printf("impossible");
    }else{
        char a2[32];
        strcpy(a2, a);
 
        for(int i = 0;i<strlen(a)-2;i++){
//            printf("%s,%d\n",a,count);
            if (a[i] != b[i]){
                count += 1;
                a[i] = b[i];
                if (a[i+1] == '1' )a[i+1] = '0';
                else a[i+1] = '1';
                if (a[i+2] == '1' )a[i+2] = '0';
                else a[i+2] = '1';
            }
        }
        
        if (a2[0] == '1' )a2[0] = '0';
        else a2[0] = '1';
        if (a2[1] == '1' )a2[1] = '0';
        else a2[1] = '1';
        for(int i = 0;i<strlen(a2)-2;i++){
            if (a2[i] != b[i]){
                countb += 1;
                a2[i] = b[i];
                if (a2[i+1] == '1' )a2[i+1] = '0';
                else a[i+1] = '1';
                if (a2[i+2] == '1' )a2[i+2] = '0';
                else a2[i+2] = '1';
            }
        }
            
        if (a[strlen(a)-1] == b[strlen(a)-1] &&
            a[strlen(a)-2] == b[strlen(a)-2]);
        else if (a[strlen(a)-1] != b[strlen(a)-1] &&
                 a[strlen(a)-2] != b[strlen(a)-2]) count ++;
        else count = 10000;
        
        if (a2[strlen(a)-1] == b[strlen(a)-1] &&
            a2[strlen(a)-2] == b[strlen(a)-2]);
        else if (a2[strlen(a)-1] != b[strlen(a)-1] &&
                 a2[strlen(a)-2] != b[strlen(a)-2]) countb ++;
        else countb = 10000;
//        printf("%s,%d\n",a,count);
//        printf("%s,%d\n",a2,countb);

        if (count < 10000 || countb < 10000){
            if (count < countb)printf("%d",count);
            else printf("%d",countb);
        }
        else printf("impossible");
    }
    return 0;
}

noi 51 AC


# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
char a[101],b[101];
int ha[30] = {},hb[30] = {};
int main(){
    scanf("%s %s",a,b);
    int len = (int)strlen(a);
    for (int i = 0;i<len;i++){
        ha[a[i]-'A']++;
        hb[b[i]-'A']++;
    }
    sort(ha,ha+26);
    sort(hb,hb+26);
    int sgn = 0;
    for(int i = 0;i<26;i++){
//        printf("%d %d\n",ha[i],hb[i]);
        if (ha[i] != hb[i]){
            sgn = 1;
            break;
        }
    }
    if (sgn == 0)printf("YES");
    else printf("NO");
    return 0;
}
%Ld long double
%g 可以省略后面的零

noi 03 AC

# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
char a[101];
int main(){
    scanf("%s",a);
    long double res = 0,p = 0.125;
    for (int i = 2;i<strlen(a);i++){
        res += int(a[i]-'0') * p;
        p /= 8;
    }
    printf("%s [8] = %.50Lg [10]\n",a,res);
    return 0;
}

http://noi.openjudge.cn/ch0306/2392/ WA 题目有错

# include <cstdio>

struct node{
    int id,lc = -1,rc = -1,layer = 1;
}a[10005];

int n,c,ta,tb,tc;

void DFS(int start){
    if (a[start].lc != -1){
        a[a[start].lc].layer = a[start].layer + 1;
        DFS(a[start].lc);
    }
    if (a[start].rc != -1){
        a[a[start].rc].layer = a[start].layer + 1;
        DFS(a[start].rc);
    }
}

int main(){
    scanf("%d %d",&n,&c);
    for(int i = 1;i<=n;i++)a[i].id = i;
    for(int i = 0;i<c;i++){
        scanf("%d %d %d",&ta,&tb,&tc);
        a[ta].lc = tb;
        a[ta].rc = tc;
    }
    DFS(1);
    for(int i = 1;i<=n;i++){printf("%d\n",a[i].layer);}
    return 0;


}
http://noi.openjudge.cn/ch0306/2392/
# include <cstdio>

struct node{
    int id,lc = -1,rc = -1,layer = 1;
}a[1000005];

int n,c,ta,tb,tc;

void DFS(int start){
    if (a[start].lc != -1){
        a[a[start].lc].layer = a[start].layer + 1;
        DFS(a[start].lc);
    }
    if (a[start].rc != -1){
        a[a[start].rc].layer = a[start].layer + 1;
        DFS(a[start].rc);
    }
}

int main(){
    scanf("%d %d",&n,&c);
    for(int i = 1;i<=n;i++)a[i].id = i;
    for(int i = 0;i<c;i++){
        scanf("%d %d %d",&ta,&tb,&tc);
        a[ta].lc = tb;
        a[ta].rc = tc;
    }
    DFS(1);
    for(int i = 1;i<=n;i++){printf("%d\n",a[i].layer);}
    return 0;
}

1023 TODO

// 2020-03-02 08:02:57
# include <cstdio>
# define maxn 100000
int price,p[maxn],amount[maxn],am;
int i = 0,a,b;

int in(int j){
    for(int k = 0;k<i;k++){
        if (p[k] == j)return k;
    }
    return 0;
}

int main(){
    scanf("%d",&price);
    while(1){
        scanf("%d %d",&a,&b);
        if (a == -1 && b == -1)break;
        else{
            p[i] = a;
            amount[i] = b;
            i++;
        }
    }
    scanf("%d",&am);
    int nowamount = 0,maxres = 0,price = 0,res = 0;
    for(int j = p[0];;j++){
        if (j <= p[i-1]){
            if (in(j)){
                nowamount = amount[in(j)];
                price = j;
            }else continue;
        }
        else {
            nowamount = amount[i-1]-(j-p[i-1])*am;
            price = j;
        }
        if (nowamount <= 0)break;
        
        int now = (price - p[0]) * nowamount;
        printf("--%d %d %d\n",price,nowamount,now);
        if (now > maxres){
            maxres = now;
            res = j;
        }
    }
    printf("%d",res);
    return 0;
}

发表评论