C/C++ (5) Luogu brush questions basic questions---Happy New Year

The road to learning is long and long, and the process of writing study notes is the process of telling yourself the knowledge.

Only love can last for a long time, only love can not be afraid of the impermanence of the world!


Wishing you great success in the new year "Rabbit"

In the new year, everyone remember not to forget to brush up the questions (⊙o⊙)?


Table of contents

Only love can last for a long time, only love can not be afraid of the impermanence of the world!

Wishing you great success in the new year "Rabbit"

P1424 Little Fish's Voyage (Improved Version)

topic background

topic description

input format

output format

Input and output samples

Instructions/Tips

About the topic:

P1914 Little Book Boy——Caesar Cipher

topic background

topic description

input format

output format

Input and output samples

Instructions/Tips

AC: Accept, the program is passed.


P1424 Little Fish's Voyage (Improved Version)

topic background

topic description

There is a small fish that swims 250 to 250 kilometers a day on weekdays, and rests on weekends (weekends are implemented). Assuming that counting from week xx, after xx days, how many kilometers has the little fish swam in total?

input format

Enter two positive integers �,�x,n to indicate that �n days have passed since week �x.

output format

Output an integer indicating how many kilometers the fish has swum.

Input and output samples

Type #1 to copy

3 10

output #1 copy

2000

Instructions/Tips

Data guarantee, 1≤�≤71≤x≤7, 1≤�≤1061≤n≤106.


About the topic:

First of all, swim 150 kilometers in the morning, swim 100 kilometers in the afternoon, and rest in the evening and on weekends. These conditions can be combined into one: except weekends, swim 250 kilometers every day. (It is very simple and clear to say so.)

How do you know it's the weekend? Assuming that the small fish starts swimming from week n, and each day of swimming is n+1, then if n is 6 or 7, it proves that it is a weekend, otherwise, if it is not 6 or 7, then it is not a weekend, and 250 must be added.

Finally, the most important point is that if it is week 7 now, then tomorrow will not be week 8, but week 1, so when n==7, n should be assigned a value of 1.

It is easy to understand by comparing the code:

A.

#include<bits/stdc++.h>
using namespace std;
int main(){
	long long int x,n,m,sum=0;
	int s[7]={250,250,250,250,250,0,0};
	cin >> x >> n;
	m=x;
	for(int i=1;i<=n;i++){
		sum=sum+s[m-1];
		if(m==7){
			m=1;
		}
		else{
			m++;
		}
	}
	cout << sum << endl;
}

This is a very simple question, but it is also a very interesting question. Looking at the code above, I can see that I used an array. So is there any other way to solve this problem? The answer is without a doubt, yes! ! !

B.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    unsigned long long n,ans=0;
    int x;
    cin >> x >> n; //输入星期和天数
    for(int i=0;i<n;i++)
    {
        if((x!=6)&&(x!=7)) //星期不等于6和7
            ans += 250; //总长度增加250
        if(x==7) //当x等于7的时候
        x=1; // x归1
        else //其他情况下(x不等于7)
            x++; 
    }
    cout << ans; //输出总路程
    return 0;
}

 It can be seen that the B. problem solution uses simple addition and subtraction operations, and it is enough to list when x!=6&&x!=7 , while A. The sample code uses an array that is 0 when  x == 6&&x==7  . Of course, the common thinking is also the same, list Saturday and Sunday off, and when it is Sunday, it will start from Monday, different thinking, different solving process, but our answers are all correct! ! !

I don't know if this can open up your thinking about brushing questions?


P1914 Little Book Boy——Caesar Cipher

topic background

A certain Konjac fell in love with "Little Book Boy", and one day he forgot his password when he logged in (he didn't bind his email or mobile phone), so he threw the problem to Godben you.

topic description

Although Konjac has forgotten the password, he still remembers that the password consists of a character string. The password is formed by moving each letter backward by �n digits in the original text string (consisting of no more than 50 lowercase letters). z The next letter of is  a, and so on. Now he has found the original text string and �n before the move, please ask for the password.

input format

First line: �n. Second row: A string of letters before moving.

output format

One line is the password of this konjac.

Input and output samples

Type #1 to copy

1 
qwe

output #1 copy

rxf

Instructions/Tips

String length ≤50≤50.


A.

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,z;
	string s;
	cin >> n;
	cin >> s;
	for(int i=0;i<s.length();i++){
		if(s[i]+n>'z'){
			z=(s[i]+n)-'z'+96;
			cout << char(z);
		}
		else{
			cout << char(s[i]+n);
		}
	}
}

This question is also very interesting. We need to understand and use the ASCII code table. As we all know, in the ASCII code, a corresponds to

97, look at the question,

z=(s[i]+n)-'z'+96;

I don't know if you understand it?

Similarly, we can also try  to jump back to  a when encountering >z :

B.

for(i=0;i<s.size();++i)
    {
        for(j=1;j<=n;++j)
        {
            ++s[i];
            if(s[i]>'z')
            s[i]='a';
        }
    }

OKOKOKOKKKkkkk,


In the past few days of brushing the questions, I have really learned a lot, such as "Data-Oriented Programming", "Cheating the Sample, Violently Finding a Miracle, Searching, Hanging up and Playing the Table, and Saving One" Hahaha O(∩_ ∩) O, I wonder if you understand?

I can explain in detail in the following solution, what is data-oriented programming?


AC: Accept, the program is passed.

Guess you like

Origin blog.csdn.net/m0_63244368/article/details/128713853