天天看点

zcmu-1048: 子串(字符串找规律输出)

Time Limit: 1 Sec  Memory Limit: 128 MB

Submit: 695  Solved: 272

[Submit][Status][Web Board]

Description

有一个字符串A,然后执行下面程序

If(A==”X”)

       A=”XZCMUACMX”;

While(true)

{

       A=A+”ZCMUACM”+A;

}

现在让A=”X”,然后While循环无数次后,求出A在L到R这段区间内的字符串?

Input

多组测试数据,对于每组测试数据都只有一行包含两个整数l,r(1<=l<=r<=10^6,r-l<=100)

Output

对于每组测试数据,输出A的子串。

Sample Input

5 10

Sample Output

UACMXZ

下面两个方法都可以,要注意找规律的时候 left % 8 == 0 的时候要把left标记为8,而不是0!! 

【通过代码(预处理)】

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <stack>
#include <set>
#include <algorithm>
using namespace std;
string a = "X";
void init()
{
    for(int i = 0 ; i < 17;i++)
        a = a + "ZCMUACM" + a;
}
int main()
{
    init();
    int x,y;
    while(scanf("%d%d",&x,&y) != EOF)
    {
        int le = x % 8;
        const char *b = a.c_str();
        if(le == 0)//!!!!!!!!!!重要!!
            le = 8;
        for(int i = le - 1;i < le + y - x;i++)
            cout<<b[i];
        cout<<endl;
    }
    return 0;
}

           

【通过代码】 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <stack>
#include <set>
#include <algorithm>
using namespace std;
string a = " XZCMUACM";

int main()
{
    int x,y;
    const char *b = a.c_str();
    while(scanf("%d%d",&x,&y) != EOF)
    {
        while(x <= y)
        {
            if(x % 8 != 0)
                cout<<b[x % 8];//!!!!!
            else if(x % 8 == 0)
                cout<<b[8];
            x ++;
        }
        cout<<endl;
    }

    return 0;
}