c++ question exercises (2)

2022 C/C++ Language University C Group E

 

method one:

#include <iostream>
#include<cstring>
using namespace std;
int main()
{
  int W = 841;
  int L = 1189;
  string str;
  cin >>str;
  int num = str[1]-'0';
  while(num>0){
    L = L/2;
    if(L<W){ 
      int b = L;
      L = W;
      W = b;
    
    }
    num--;
  }
  cout <<L<<"\n" <<W<<endl;
  return 0;
}

swap(a,b) //Exchange a and b

 Method Two:

        

#include <iostream>
#include<cstring>
using namespace std;
int main()
{
  char c;int x;
cin >>a >>x;
int a = 1189,b=841;
while(x--){
a/=2;
if(a<b) swap(a,b);
}
  cout << a <<"\n"<<b <<endl;
  return 0;
}

#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{

  string str = "WHERETHEREISAWILLTHEREISAWAY";
  sort(str.begin(),str.end());
  cout <<str<<endl;
  return 0;
}

 

#include <iostream>

#include <algorithm>
using namespace std;
const int maxn = 1e6+10; 
int a[maxn],b[maxn];
bool cmp(int x,int y)
{
  return b[x] < b[y] || b[x] ==b[y] && x <y;
}
int main()
{
  int n,m;
  cin >>n>>m;
  for(int i=1;i<=n;i++){
    int num = i;
    while(num){
      b[i] += num%10;
      num/=10;
    }
    a[i]=i;
  }
  sort(a+1,a+1+n,cmp); //从小到大排序,如果等于true,就正常排序,如果等于false,则交换位置
  cout <<a[m]<<endl;
  return 0;
}

sort(a,a+10,cmp);

1. The first parameter first: is the starting address of the array to be sorted;

2. The second parameter last is the end address (the address of the next data after the last data)

3. The third cmp is the sorting method, which can be in ascending or descending order. If the third parameter is ignored, the default sorting method is from smallest to largest.

cmp is equivalent to a bool type function. When the return value is true, it is not exchanged, and when it returns false, it is exchanged.

#include <iostream>
#include<cstring>
#include<algorithm>
#include <cstdio>

using namespace std;
int main()
{
  string str;
  cin >> str;
  bool is_minus = str[0] =='-';
  if(is_minus)
    str.erase(str.begin());
  reverse(str.begin(),str.end());
  int k =0;
  while(str[k]=='0' && str.size()-k>1)
    k++;
  str=str.substr(k);

  if(is_minus)
    cout << '-';
  cout << str <<endl;  
  return 0;
}


substr() is a C++ language function.
Its main function is to copy a substring,
starting from a specified position
and having a specified length.
If length _Count is not specified or
_Count+_Off exceeds the length of the source string,
the substring is continued to the end of the source string.
string, containing a copy of len characters starting from pos in s
(the default value of pos is 0, and the default value of len is s.size() - pos,
that is, without parameters, the entire s will be copied by default.

1. erase (pos,n);
delete n characters starting from subscript pos. For example, erase(0,1) deletes the first character.
2.erase(position);
deletes a character at position (position is a Iterator of string type)
3.erase(first,last)
deletes characters from first to last (first and last are both iterators

input Output:

 0-670-82162-4

Right

0-670-82162-0

0-670-82162-4

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

int main()
{

  // 请在此输入您的代码
  string str;
  cin >> str;
  int res=0;
  int a = 1;
  for(int i =0;i<str.size()-1;i++)
  {
    if (str[i]!='-')
    {  res += (str[i]-'0')*a;
      a++;}
    else continue;

  }
  int end = res %11;
  if(end==str[12]-'0'||end==10&&str[12]=='X')
    cout << "Right"<<endl;
  else {
    str = str.substr(0,str.size()-1); 
    cout <<str;
    if (end ==10)
      cout  <<'X'<<endl;
    else cout <<end<<endl;
         
      
  }
  return 0;
}

substr as a slice is similar to python's str[1:2], the end of the slice is str.size()-1: the string size minus one

str.back(): Returns the last character

 2020

gengzi

#include <iostream>
#include <cstring>
using namespace std;
string tg[]={
    "geng","xin","ren","gui","jia",
    "yi","bing","ding","wu","ji"
};
string dz[]={
    "shen","you","xu","hai",
    "zi","chou","yin","mao",
    "chen","si","wu","wei"
};
int main()
{
    int y;
    cin >> y;
    cout <<tg[y%10]<<dz[y%12]<<endl;
}

Bring in 2020, take the modulo of 10 and 12, get the serial numbers of the heavenly stems and the earthly branches, and send them to the array for sorting.

Guess you like

Origin blog.csdn.net/lishijie258/article/details/128264910