紫薯 第三章

 

3-2 开灯问题

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
using namespace std;
int a[5000];
int main()
{
    int n,k,f=1;
    cin>>n>>k;
    for(int i=1;i<=n;i++) a[i]=1;

    for(int i=2;i<=k;i++){
        for(int j=i; j<=n; j+=i){
            a[j] = !a[j];
        }
    }
    for(int i=1;i<=n; i++){
        if(a[i]){
           if(f){
            f=0;
            printf("%d",i);
           }
           else
           printf(" %d", i);
        }
    }
    return 0;
}
/*
    7 3
    1 2 3 4 5 6 7
    1 1 1 1 1 1 1
    1 0 1 0 1 0 1
    1 0 0 0 1 1 1
*/
View Code

 

3-3 🐍形填数

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
#define N 5000
using namespace std;
int a[N][N];
int n;

int main()
{
    cin>>n;
    int tot = 0;
    int x, y;
    tot = a[x=0][y=n-1] = 1;
    while(tot < n * n){
        while(x+1<n && !a[x+1][y]) a[++x][y] = ++tot;
        while(y-1>=0 && !a[x][y-1]) a[x][--y] = ++tot;
        while(x-1>=0 && !a[x-1][y]) a[--x][y] = ++tot;
        while(y+1<n && !a[x][y+1]) a[x][++y] = ++tot;
    }
    for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
        printf("%3d ",a[i][j]);
    }
        cout<<endl;
    }
    return 0;
}
View Code

 

3-4 竖式问题

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
#define N 5000
using namespace std;
int a[N][N];
char s[N];
char buf[N];
int n;

int main()
{
    scanf("%s",s);
    int c = 0;
    for(int abc = 111; abc <= 999; abc++)
        for(int de = 11; de <= 99; de++){
            int x =  abc * (de % 10), y = abc * (de / 10), z = abc * de;
            sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);
            int ok = 1;
            for(int i=0; i<strlen(buf); i++)
                if(strchr(s, buf[i]) == NULL) ok = 0; //
            if(ok){
                printf("<%d>\n",++c);
            printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n",abc, de, x, y, z);
        }
    }
    printf("cnt = %d\n",c);
    return 0;
}
sprintf + strchr

 

3-6: 环状序列

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    while(n--){
        string s, str[150];
        cin>>s;
        int len = s.size();
        s = s + s;
        for(int i=0; i<len; i++){
            str[i] = s.substr(i,len);
        }
        sort(str,str+len);
        cout<<str[0]<<endl;
    }
}
倍增+substr

 

例题:3-1 得分

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
#define N 5000
using namespace std;
int a[N][N];
char s[N];
char buf[N];
int n;
int f = 1;
int main()
{
    string s;
    cin>>s;
    int o = 0, x = 0, sum = 0;
    for(int i=0; i<s.size(); i++){
        if(s[i]=='O') o++;
        else{
            o = 0;
        }
        sum += o;
    }
    cout<<sum<<endl;
    return 0;
}
View Code

 

3-2:分子量

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
#define N 5000
using namespace std;
int a[N][N];
char s[N];
char buf[N];
int n;
int f = 1;
int main()
{
    string s;
    cin>>s;
    map<char,double> mp;
    mp['C'] = 12.01;
    mp['H'] = 1.008;
    mp['O'] = 16.00;
    mp['N'] = 14.01;
    double sum = 0;
    for(int i=0; i<s.size(); i++){

        if(s[i]=='C'){
            if( isdigit(s[i+1]) && !isdigit(s[i+2]) ){
                    sum += mp[s[i]] * (s[i+1]-'0');
            }
            else if(isdigit(s[i+1]) && isdigit(s[i+2]) )
                    sum += (mp[s[i]] * ((s[i+1]-'0')*10 + (s[i+2]-'0')));
            else if( !isdigit(s[i+1])){
                    sum += mp[s[i]];
            }
        }
        if(s[i]=='O'){
            if( isdigit(s[i+1]) && !isdigit(s[i+2]) ){
                    sum += mp[s[i]] * (s[i+1]-'0');
            }
            else if(isdigit(s[i+1]) && isdigit(s[i+2]) )
                    sum += (mp[s[i]] * ((s[i+1]-'0')*10 + (s[i+2]-'0')));
            else if( !isdigit(s[i+1])){
                    sum += mp[s[i]];
            }
        }
        if(s[i]=='H'){
            if( isdigit(s[i+1]) && !isdigit(s[i+2]) ){
                    sum += mp[s[i]] * (s[i+1]-'0');
            }
            else if(isdigit(s[i+1]) && isdigit(s[i+2]) )
                    sum += (mp[s[i]] * ((s[i+1]-'0')*10 + (s[i+2]-'0')));
            else if( !isdigit(s[i+1])){
                    sum += mp[s[i]];
            }
        }
        if(s[i]=='N'){
            if( isdigit(s[i+1]) && !isdigit(s[i+2]) ){
                    sum += mp[s[i]] * (s[i+1]-'0');
            }
            else if(isdigit(s[i+1]) && isdigit(s[i+2]) )
                    sum += (mp[s[i]] * ((s[i+1]-'0')*10 + (s[i+2]-'0')));
            else if( !isdigit(s[i+1])){
                    sum += mp[s[i]];
            }
        }

    }

    cout<<sum<<endl;

    return 0;
}
sb

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
#define N 5000
using namespace std;
double a[5] = {12.01, 1.008, 16.00, 14.01};
char s[N];
char buf[5] = {'C','H','O','N'};
int n;
int f = 1;
double sum = 0;
void add(int f, int i)
{
    if(isdigit(s[i+1]))
        if(isdigit(s[i+2]))
            sum += a[f] * ( (s[i+1]-'0')*10 + s[i+1]-'0' );
        else
            sum += a[f] * (s[i+1]-'0');
    else
        sum += a[f];
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        gets(s);
        sum = 0;
        for(int i = 0; s[i]; i++){
            for(int j = 0; j < 4; j++){
                if(s[i] == buf[j]){
                    add(j, i);
                }
            }
        }
        printf("%.3f\n",sum);
    }

    return 0;
}
OK

 

3-3:数数字

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
#define N 5000
using namespace std;

int a[10005];
char s[N];
int n;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        for(int i=1; i<=n; i++){
            int tmp = i;
            while(tmp){
                a[tmp%10]++;
                tmp/=10;
            }
        }
        for(int i=0; i<=9; i++){
            printf(i == 9 ? "%d\n" : "%d ",a[i]);
        }
    }
    return 0;
}
数位

 

3-4 :周期串

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--){
    string s;
    cin>>s;
    int len = s.size();
     for(int i=1; i<=len; i++)
     {
        if(len % i == 0)
        {
            int f = 1; //判断最小周期串是否找到,找到值为1,否则为0  
            for(int j=i; j<len; j++)
            {
                if(s[j] != s[j%i])
                {
                    f = 0;
                    break;
                }
            }
            if(f)
            {
                printf("%d",i);
                break;
            }
        }
      }
        if(t) printf("\n\n");
        else printf("\n");
    }
}
/*
从字符串的起始位置遍历,对于字符串长度为N的字符串,
那么周期串的可能长度介于1—N之间,所以可以假设周期串的长度为i,
其中1<=i<=N。满足最小周期串的条件之一是length%i==0,
这是因为如果所述i是字符串的周期串长度,
那么整个字符串可以被划分成整数个长度为i的周期子串,
如果这个整除的条件不成立,说明i也不是字符串的周期串长度。
满足最小周期串的条件之二是,
变量j从i的位置开始一直到字符处结束的位置,
满足(s[j]!=s[j%i])这个条件,这是因为若从0到i-1这个区间组成的子串是该字符串的周期串,
那么从i到length-1的位置是该最小字符串的若干次重复。由于上述i的取值是从1即从最小值开始取值的,
因此当上述两个条件都成立时,即可求得最小周期串长度和最小周期串。
*/
View Code

 

posted @ 2018-04-02 13:26  Roni_i  阅读(134)  评论(0编辑  收藏  举报