A. Nearest distance (check-in question)
subject
Idea:
It is only necessary to judge whether the AB straight line is perpendicular to the coordinate axis and point F is between the AB line segments. If not, it represents more than one shortest path between points A and B, and the final result is the sum of the absolute values of the difference between the abscissa and ordinate of points A and B; If yes, it means that there is only one shortest path between points A and B, and points A to B need to bypass point F, then add the detour distance 2 to the unqualified conditions.
code:
#include<iostream> #include<algorithm> using namespace std; int main() { int t; scanf("%d",&t); while(t--){ int Ax,Ay,Bx,By,Fx,Fy; //Read in the coordinates of points A, B and F scanf("%d %d",&Ax,&Ay); scanf("%d %d",&Bx,&By); scanf("%d %d",&Fx,&Fy); int ans = abs(Bx - Ax) + abs(By - Ay);//Calculate the sum of the absolute values of the difference between the abscissa and ordinate of points A and B if(Ax == Bx && Bx == Fx && Fy > min(Ay,By) && Fy < max(Ay,By)) ans += 2; //Judge whether the AB line segment is perpendicular to the x axis and the F point is between the AB line segment if(Ay == By && By == Fy && Fx > min(Ax,Bx) && Fx < max(Ax,Bx)) ans += 2; //Judge whether the AB line segment is perpendicular to the y axis and the F point is between the AB line segment printf("%d\n",ans); } return 0; }
B. This is a very simple problem (simple math problem)
Title:
Idea:
To judge whether a number has an odd factor other than 1, you only need to decompose the number completely (decompose it into the form of multiplication of prime numbers),
It is found that only when the factors are all 2 (in other words, the number is 2n), there is no odd factor other than 1.
code:
#include<bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; //If n is an integer, keep / 2 until it is odd while(n % 2 == 0 ){ n /= 2; } //It is only necessary to judge whether the odd number is 1. If it is 1, it is proved that the factors of complete decomposition of the number are all 2, and vice versa if(n != 1) cout<<"YES"; else cout<<"NO"; return 0; }
C. Character conversion of M78 Nebula (basic problem)
Title:
Idea:
Convert lowercase characters into uppercase letters through ASCL code conversion. If you forget the difference between uppercase and lowercase letters, you can output lowercase letters minus uppercase letters for a look.
code:
#include<stdio.h> #include<string.h> int main() { char str[1010]; //String read mode scanf("%s",str); //strlen function finds the length of a string for(int i = 0 ; i < strlen(str) ; i ++){ //Convert upper and lower case letters according to the ASCL code, the lower case letter is 32 larger than the upper case letter if(str[i] >= 'a' && str[i] <= 'z') str[i] -= 32; } printf("%s",str); return 0; }
D. Queuing accident of M78 Nebula (prefix and board questions)
Title:
Idea:
For the application of basic prefix and knowledge, sort first, and then sum the interval [l,r], that is, find the value of [1,r] - [1,l - 1].
code:
#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; const int N = 1e3 + 10; int arr[N]; int q[N];//Prefix and array, q[i] represents the sum of all values in the [1,i] interval int n,l,r; int main() { scanf("%d",&n); for(int i = 1 ; i <= n ; i ++){ scanf("%d",&arr[i]); } sort(arr + 1 , arr + 1 + n);//Sort sort function, you can also write bubble instead //Find prefix and array for(int i = 1 ; i <= n ; i ++) q[i] = q[i - 1] + arr[i]; scanf("%d %d",&l,&r); printf("%d",q[r] - q[l - 1]); return 0; }
F. Unconvinced student sister Jie (gcd)
Title:
Idea:
Subtract the same value from any interval of the array for an unlimited number of times, and finally make all the values of the array the same. It is assumed that the subtracted value is K. for any arri of the array, it can be assumed that it becomes the fixed value s after reducing the K value n times, that is, arri - nK = s. each value in the array satisfies this formula before and after the change, and find the maximum value of K, that is, find the maximum common divisor (gcd) of all the reduced amounts nK in the array. For each number, the amount of reduction must make the multiple of K or 0, so the difference between two adjacent numbers | arra - arrb |= |(aK + s) - (bK + s) |= |(a-b)K |, so you only need to find the maximum common divisor of the absolute value of the difference between all two adjacent numbers (not 0, not interesting).
code:
#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; const int N = 1e6 + 10; int arr[N],q[N]; int n; int gcd(int a , int b) { if(b == 0) return a; return gcd(b,a%b); } int main() { scanf("%d",&n); for(int i = 0 ; i < n ; i ++) scanf("%d",&arr[i]); int k = 0; //Make a difference between two adjacent bits to obtain the multiple of K for(int i = 0 ; i + 1 < n ; i ++) if(arr[i + 1] - arr[i])//Store the absolute values of all the differences that are not 0 into the q array q[k++] = abs(arr[i + 1] - arr[i]); int ans = q[0]; //Find the greatest common divisor of K multiples of K and get the result for(int i = 1 ; i < k ; i ++) ans = gcd(ans , q[i]); //If ans == 0, it means that all values in the arr array are equal, and the maximum value of K tends to infinity if(ans == 0) printf("-1"); else printf("%d",ans); return 0; }
E. Small test from M78 Nebula (binary conversion + palindrome)
Title:
Idea:
After the number is converted into m-ary, it is stored in the form of string, and then run from head to tail to the middle with double pointers, and constantly compare to judge whether it is palindrome.
code:
#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; const int N = 1e4 + 10; int n , m; char q[20] = "0123456789ABCDEF"; bool check(int a) { char s[100];//Store the string after converting a to m-ary int k = 0;//Number of bits after converting record to string a = a * a; //Perform binary conversion while(a){ s[k ++] = q[a % m]; a /= m; } //Compare whether the characters are the same from the beginning to the end to the middle. If there are differences, it is not a palindrome int i = 0, j = k - 1; while(i < j){ if(s[i] == s[j]) i++,j--; else return false;//If the palindrome is not satisfied, return false } return true; } int main() { scanf("%d %d",&n,&m); int ans = 0; for(int i = 1 ; i <= n ; i ++){ if(check(i)) ans ++;//Record the number of palindromes satisfied } printf("%d",ans); }
G. Subtraction from M78 Nebula (simulation problem / egg problem)
Title:
Idea:
python solution (the simplest solution):
Because python has no range limit for integers, you can define two numbers to make a difference between them.
code:
a = int(input().strip()) b = int(input().strip()) print(a-b)
c/c + + solution (large-scale simulation):
Use array to simulate the calculation process.
code:
#include<bits/stdc++.h> using namespace std; //Judge whether A is greater than B bool check(string str1 , string str2) { int len1 = str1.length(),len2 = str2.length(); if(len1 != len2) return len1 > len2; for(int i = len1 - 1 ;i >= 0 ; i --) if(str1[i] != str2[i]) return str1[i] > str2[i]; return true; } int main() { string str1,str2; cin>>str1>>str2; int ans[1000]; bool s = true; if(!check(str1,str2)){ //If a < B, exchange a and B and record with s. the result is negative swap(str1,str2); s = false; } int i = str1.length() - 1, j = str2.length() - 1; int k = 0,cn = 0; //k stands for borrowing //Simulation calculation process of A - B while(i >= 0 && j >= 0){ if(str1[i] + k < str2[j]){ ans[cn ++] = str1[i] - str2[j] + 10 + k ; k = -1; }else{ ans[cn++] = str1[i] - str2[j] + k ; k = 0; } i--,j--; } //If the number of digits of a is more than B, add the number of digits of A while(i >= 0){ if(str1[i] - '0' + k >= 0){ ans[cn++] = str1[i] - '0' + k; k = 0; }else{ ans[cn++] = str1[i] - '0' + k + 10; k = -1; } i--; } //Similarly, if the number of bits of B is more than A, add the number of bits of B while(j >= 0){ if(str2[j] - '0' + k >= 0){ ans[cn++] = str2[j] - '0' + k; k = 0; }else{ ans[cn++] = str2[j] - '0' + k + 10; k = -1; } j--; } if(!s) cout<<"-"; while(!ans[cn - 1] && cn > 1) cn--; for(int i = cn - 1 ; i >= 0 ; i --) cout<<ans[i]; }