6 turned over with hourglass printing

6 turned over with hourglass printing

The first question involves character strings. According to the meaning of the question, we only need to count the number of consecutive "6"s in the string, and then convert the corresponding value for output . Other characters remain unchanged. However, it is worth noting that there are spaces in the string, so you can choose getline() in the string class . Of course there are other methods, but one is recommended here. Subject: Candlelight

#include<iostream>
#include<string>
using namespace std;
int main()
{
    
    
	string str,str1;//str1的作用就是统计一段连续‘6’的字符串
	getline(cin, str);
	for (int i = 0; i < str.size(); i++)//对字符串进行遍历
	{
    
    
		if (str[i] != '6')//判断,如果该字符不等于为6,这输出该字符
		{
    
    
            /*这组判断是必要的,打个比方,比如当遍历完6666时,str1的长度为4。
           并且下一个字符不为6,在输出这个目前不为6的字符前,应当先把之前str1(str1="6666"),
           进行相应的转化输出,因此进入第二个输出为9
			*/
			if (str1.size() > 9)
			{
    
    
				cout << "27";
				str1 = "";//每一次输出记得把str1归空,不然会把不连续的6进行个数叠加。
			}
			else if (str1.size() <= 9 && str1.size() > 3)
			{
    
    
				cout << "9";
				str1 = "";
			}
			else if(str1.size()<=3&&str1.size()>0)
			{
    
    
				cout << str1;
				str1 = "";
			}
			cout << str[i];
			
			
		}
		else//对str[i]=='6'进行统计
		{
    
    
			str1 += str[i];
		}
	}

	/*跑完这个循环之后,下面的这组也要加上,原因就是如果一个字符串
	最后几个字符都是‘6’,这样就会造成最后一组连串的‘6’无法转换输出,
	原因是上面是要碰到下一个字符不为‘6’我才输出,如果到了字符串末端,
	没碰到不是‘6’的字符,会造成最后一个数据遗失的,而下面的判断正好解决了这个问题。*/

    if (str1.size() > 9)
	{
    
    
		cout << "27";
		str1 = "";
	}
	else if (str1.size() <= 9 && str1.size() > 3)
	{
    
    
		cout << "9";
		str1 = "";
	}
	else if (str1.size() <= 3 && str1.size() > 0)
	{
    
    
		cout << str1;
		str1 = "";
	}
	
	return 0;
}

The second question is to print the hourglass, to print graphics, I think first of all look at the details of the graph, and then find to find the law , which would be easier to feel it out method. Multiple-choice questioner: Spore

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	int num=0;char c;cin>>num>>c;
	int sum=1,len;
	//这个沙漏的打印我们要先确定低的长度
	//很明显这个沙漏至少要有两层那么我们在确定地的长度的长度的时候是从第二层开始逐层加2(以最中间为第一层)
	for(int i=3;i<1000;i+=2){
    
    //这个循环的上线要绝对够
		//我们要在这个沙漏所用的c的数量是在和下一个的数量
		//如果下一个的数量不够那么这次循环的i即使沙漏的底了
		if(num>=sum&&num<=sum+i*2){
    
    
			len=i-2;
			break;
		}
		sum+=i*2;
	}

	int sumtemp=len;
	int j=0,counter=0;
	//开始打印上半部分
	for(int i=len;i>=1;i-=2,j++){
    
    
		//打印每层前的空格
		for(int i1=0;i1<j;i1++)cout<<" ";
		for(int i2=0;i2<i;i2++){
    
    
			//统计c的用量,当然在第一个循环我们干了类似的事,那个sum的数据也是可以使用的
			cout<<c;counter++;
		}
		cout<<endl;
	}
	//下半部分
	for(int i=3;i<=len;i+=2,j--){
    
    
		for(int i2=0;i2<j-2;i2++)cout<<" ";
		for(int i3=0;i3<i;i3++){
    
    
			cout<<c;counter++;	
		}
		cout<<endl;
	}
	//结尾输出好友几个c没有使用
	cout<<num-counter;
	return 0;
}

The last AcWing question is a relatively simple one, that is, the enumeration that everyone knows best. The amount of data in the question itself is not large, and it can be directly violent. Of course, the loop can also be optimized according to the conditions of the subject, or the optimization method is feasible. Here is the moment of violence

#include<iostream>
#include<string>
using namespace std;

int main()
{
    
    
	int l, r, sum = 0;
	string str;
	cin >> l >> r;
	for (int i = l; i <= r; i++)
	{
    
    
		str = to_string(i);//把int类型转化为字符串(string)
		for (int j = 0; j < str.size(); j++)
		{
    
    
			if (str[j] == '2')
			{
    
    
				sum++;
			}
		}
		
	}
	cout << sum << endl;
	return 0;
}

I believe that everyone understands the above process, so I won’t explain more here.

Guess you like

Origin blog.csdn.net/m0_52068514/article/details/115339598