Blue Bridge letters graphics cup of basic training

Graphic letter

Resource constraints

Time limit: 1.0 s memory limit: 256.0 MB

Problem Description

Use of the letters can form some beautiful graphics, an example is given below:

ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC

This is a pattern row 5 7, find the pattern of this rule, and outputs a pattern of n rows and m columns.

Input Format

Input line, comprising two integers n and m, respectively represent the number of columns you want output pattern lines.

Output Format

N output lines, each m characters for your graphics.

Sample input

5 7

Sample Output

ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC

Data size and convention

1 <= n, m <= 26。

Thinking

First, the subject can be analyzed by the following rules

  • The first character of each line is the i-th character
  • It can be divided into two parts
    • Is reversed in the first part, the i-th character from the beginning of backwards to 0, a total of i-0 + 1, up to the m
    • The second part is positive, starting from the first character (or characters 0th repeats), this part of a co-mi-1, up to the m

-C ++ Code

#include <iostream>
using namespace std;

int main(){
	int n,m;
	cin>>n>>m;
	string str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	for (int i=0;i<n;i++){
		string res = "";
        //第一部分
		for (int j=i;j>i-m;j--){
			if (j<0) break;
			res+=str1[j];
		} 
		////第二部分
		for (int k=1;k<m-i;k++){
			res+=str1[k];
		}
		cout<<res<<endl;
		//在两个循环内也可以直接cout,可以不用res
	}
	return 0;
}

to sum up

In actual operation, the first pass and no ac off, then I checked the binding data, mainly in the length of the first portion exceeds the limit m (the error is then j = i; j> 0) , and lead no right, 70 points, and later direct sub.str (0,2) can have a direct part to make up for this mistake. But still re-processing a bit cause of the error, and my idea of the first part of the re-write in, constraint length m, while the break is to prevent the C ++ slicing operations such as: str [negative] get is "", so the break limit.

I was on the other authors of the blog, find a clever law, its blog at the following address, talking about very thin can look at:
https://blog.csdn.net/richenyunqi/article/details/84261936

发布了8 篇原创文章 · 获赞 1 · 访问量 77

Guess you like

Origin blog.csdn.net/Lurker_hunter/article/details/104562702