第一题:
本题要求最小的步数,我个人的看法就是比较low 和 high的大小 ,如果low = high low 和high各向中间前进一步。如果low<hign 将low的值加到low+1上,反之将high加到high-1上。
第二题:
这个题好像也没啥好说的,遍历判定就行了。
第三题:
这个题我个人的做法就是将乘法拆分成加法。
第四题:
这个题的基本做法就是直接排序比较就行了。
实验全部代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
bool cmpLexicalorder(string a, string b) {return a < b;}
bool cmpLength(string a, string b) {return a.length() < b.length();}
int sum_of_base(int, const int &);
string inverseString(string);
vector<int>addition(vector<int>, vector<int>);
vector<int>multi(char , string );
vector<string> SortLex(vector<string> arr);
vector<string> SortLength(vector<string> arr);
bool IsSame(vector<string> s1, vector<string> s2);
//001
void IsPalindrome()
{
int n, temp;
cin >> n;
vector<int>arr(n, 0);
for (auto i = 0; i < n; i++)
{
cin >> temp;
arr[i] = temp;
}
int low = 0, high = arr.size() - 1;
int count = 0;
while (low < high)
{
if(arr[low] == arr[high])
{
low++;
high--;
}
else if (arr[low] < arr[high]) {
arr[low + 1] += arr[low];
low++;
count++;
}
else {
arr[high - 1] += arr[high];
high--;
count++;
}
}
cout << count<<endl;
}
//002
void luckNumber()
{
int n;
int count = 0;
cin >> n;
for (auto i = 1; i <= n; i++)
if (sum_of_base(i, 10) == sum_of_base(i, 2))
count++;
cout << count << endl;
}
//003
void product_of_large_numbers()
{
string s1, s2;
cin >> s1 >> s2;
s1 = inverseString(s1);
s2 = inverseString(s2);
vector<vector<int>>str;
for (int i = 0; i < s1.length(); i++) {
str.push_back(multi(s1[i], s2));
for (int j = 0; j < i; j++)
str[i].insert(str[i].begin(), 0);
}
vector<int>sum = str[0];
for (int i = 1; i < str.size(); i++)
sum = addition(sum, str[i]);
for (int i = sum.size() - 1; i >= 0; i--)
cout << sum[i];
cout << endl;
}
//004
void comparison_method()
{
int n;
vector<string>arr;
cin >> n;
string str;
for (int i = 0; i < n; i++) {
cin >> str;
arr.push_back(str);
}
bool IsLexical = IsSame(arr, SortLex(arr));
bool IsLength = IsSame(arr, SortLength(arr));
if (IsLexical && IsLength)cout << "both" << endl;
else if (IsLexical && !IsLength)cout << "islexicalorder" << endl;
else if (!IsLexical && IsLength)cout << "lengths" << endl;
else cout << "none" << endl;
}
int main()
{
//IsPalindrome();
//luckNumber();
//product_of_large_numbers();
comparison_method();
return 0;
}
int sum_of_base(int n, const int &num) {
int sum = 0;
while (n > 0) {
sum += n % num;
n /= num;
}
return sum;
}
string inverseString(string s) {
for (int i = 0; i < s.length() / 2; i++) {
char temp = s[i];
s[i] = s[s.length() - 1 - i];
s[s.length() - 1 - i] = temp;
}
return s;
}
vector<int>multi(char a, string s) {
vector<int>arr;
int temp = 0;
for (int i = 0; i < s.length(); i++) {
int m = (a - '0') * (s[i] - '0') + temp;
arr.push_back(m % 10);
temp = m / 10;
}
if (temp != 0)arr.push_back(temp);
return arr;
}
vector<int>addition(vector<int>s1, vector<int>s2) {
int m = s1.size() - s2.size();
if (m > 0) {
for (int i = 0; i < m; i++)
s2.push_back(0);
}
else {
for (int i = 0; i < -m; i++)
s1.push_back(0);
}
int temp = 0;
vector<int>sum;
for (int i = 0; i < s1.size(); i++) {
sum.push_back((s1[i] + s2[i] + temp) % 10);
temp = (s1[i] + s2[i] + temp) / 10;
}
if (temp > 0)sum.push_back(temp);
return sum;
}
vector<string> SortLex(vector<string> arr) {
sort(arr.begin(), arr.end(), cmpLexicalorder);
return arr;
}
vector<string> SortLength(vector<string> arr) {
sort(arr.begin(), arr.end(), cmpLength);
return arr;
}
bool IsSame(vector<string> s1, vector<string> s2) {
for (int i = 0; i < s1.size(); i++)
if (s1[i] != s2[i])return false;
return true;
}```