第十八周代码(跟着罗勇军老师刷题)

2024/2/12        周一

修剪灌木

【参考代码】

#include <iostream>
using namespace std;
int main()
{
    //某个点草长到最大高度等于他右边最长边的二倍
    int n;
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        cout << max(i-1,n-i) * 2 <<endl;
    }
    return 0;
}

【代码分析】

某个点草长到最大高度等于他右边最长边的二倍

英文数字

题目链接

由于罗老师的博客提供的网站做不了题,找了一个别的相似的题目

【参考代码】 

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

int main()
{
    int n;
    cin>>n;
    string unit[] = { //0——19
	"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
	"eleven","twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
	"nineteen"
	};
	string decade[] = { //20+
		"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
	};
    if(n < 20)
    {
    	cout << unit[n];
	}
	else
	{
		int temp = n / 10;
		if(n % 10 == 0)
		    cout << decade[temp-2];
	    else
	    {
	        n %= 10;
	        cout << decade[temp-2] << '-' << unit[n];
	    }
		    
	}
}

【代码分析】 

2024/2/13        周二

栈的成员函数使用:十进制转2,8,16进制

取栈顶元素需要用top(),但是该函数不能弹出栈顶元素。pop()函数只能弹出栈顶元素,不能输出栈顶元素的值

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

void system(int num, int x)
{
	stack<int> s;
	while(num)
	{
		s.push(num % x);
		num /= x;
	}
	//已完成压栈
	//出栈,打印
	if( x < 16 )
	{
		while( !s.empty() )
		{	
			printf("%d", s.top());
			s.pop();
		}
	}
	else  //十六进制另外输出
	{
		while( !s.empty() )
		{
			printf("%X", s.top());
			s.pop();
		}
	}
}

int main()
{
    int num;
	cout << "输入一个十进制数:" << "\n";
	cin >> num;
	cout << "请选择转换哪种进制(输入数字):" << "\n";
	int x;
	cin >> x;
	system(num, x);  //传入函数中
	return 0;
}

栈的成员函数使用:C++括号匹配

   char case1[] = "(())abc{[)(]}";
    char case2[] = "(()))abc{[]}";
    char case3[] = "(()()abc{[]}";
    char case4[] = "(())abc{[]}";

这4个案例分别对应4种情况:右括号多于左括号、左右括号配对次序不正确!、左右括号匹配正确!、左括号多于右括号

#include <iostream>
#include <string>
#include <stack>

//扫描表达式
int scanExpression(char arr[])
{
	int i;
	std::stack<char> s1;
	int n = strlen(arr);
	for (i = 0; i < n; i++)
	{
		if (arr[i] == '(' || arr[i] == '[' || arr[i] == '{')  //左括号
		{
			s1.push(arr[i]);
		}
		if (arr[i] == ')' || arr[i] == '}' || arr[i] == ']')  //右括号
		{
			if (s1.empty())//栈空
			{
				printf("右括号多于左括号");
				return 0;
			}
			else
			{
				char topElem;
				topElem = s1.top();//取栈顶元素

				if ((arr[i] == ')' && topElem != '(') || (arr[i] == ']' && topElem != '[')
					|| (arr[i] == '}' && topElem != '{'))
				{
					printf("左右括号配对次序不正确!");
					return 0;
				}
				else
					s1.pop();
			}
		}
	}
	//检验结束,最后收尾	
	if (s1.empty())//栈空
		printf("左右括号匹配正确!");
	else
	{
		printf("左括号多于右括号");
		while (!s1.empty())
		{
			s1.pop();
		}
	}
	return 1;
}

int main()
{
	char case1[] = "(())abc{[)(]}";
	char case2[] = "(()))abc{[]}";
	char case3[] = "(()()abc{[]}";
	char case4[] = "(())abc{[]}";

	scanExpression(case1);
	printf("\n");

	scanExpression(case2);
	printf("\n");

	scanExpression(case3);
	printf("\n");

	scanExpression(case4);

}

【运行结果】

 

栈的成员函数使用:栈实现斐波那契数列

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

int main()
{
	int a1 = 1, a2 = 1, a3;
	int n, i;
	stack<int> s;
	
	//输入第n个值的位置
	scanf("%d", &n);
	for(i = 0; i < n; i++)
	{
		s.push(a1);
		s.push(a2);
		
		a3 = a1 + a2;
		s.pop();
		s.pop();
		a1 = a2;
		a2 = a3;
		printf("%d\n", a1);
	}
	//printf("%d\n", a3);
}

【运行结果】 

 

栈的成员函数使用:堆宝塔

题目链接

【参考代码】

大佬的代码

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

int towercount = 0;
int maxlayer = 0;

int max(int a, int b)
{
	return a>b?a:b;
}

int main()
{
    int n;
    cin>>n;
	stack<int> a, b;
    for(int i=0; i<n;i++)
    {
    	int c;
        cin>>c;
        if(a.empty() || c < a.top())
        	a.push(c);
        else
        {
        	if(b.empty() || c > b.top())
            {
                b.push(c);
            }
            else
            {
                towercount++;
                maxlayer = max(maxlayer, a.size());
                while(a.size())
                	a.pop();
                while(!b.empty() && b.top() > c)
                { 
                    a.push(b.top());
                    b.pop();
                }
                a.push(c);
            }
		}
    }
    if(!a.empty())
    {
    	maxlayer = max(maxlayer, a.size());
    	towercount++;
    	while(!a.empty())
    	{
    		a.pop();
		}
	}
	if(!b.empty())
    {
    	maxlayer = max(maxlayer, b.size());
    	towercount++;
    	while(!b.empty())
    	{
    		b.pop();
		}
	}
    cout << towercount << " " << maxlayer;
}

年初二到初七出去玩啦,去广西阳朔。开过去花了三天,开回来花了20+小时,两天时间。出阳朔景区就花了两个小时。旅途的惊喜还是挺难忘的,比如我们第一天下午开到了广宁,就下高速去看看有没有景点,意外发现一家老字号云吞店,10元一碗20颗,广州吃不到这种独特的味道。在竹海景区里面的

2024/2/17        周六

枚举题:

大乘积

题目链接

【参考代码和思路】

简单枚举题

#include <iostream>
using namespace std;
int main()
{
  // 请在此输入您的代码
  int a[] = {99, 22, 51, 63, 72, 61, 20, 88, 40, 21, 63, 30, 11, 18, 99, 12, 93, 16, 7, 53, 64, 9, 28,84,34,96,52,82,51,77};
  int result = 0;
  for(int i=0;i<30;i++)
  {
      for(int j=i+1;j<30;j++)
      {
          int number = a[i] * a[j];
          if(number == 2022 || number > 2022)
            result++;
      }
  }
  cout << result;
  return 0;
}

不高兴的津津

题目链接

【参考代码和思路】

多段输入,多次读取数据,并计算是否大于八,大于就输出当前是周几,结束程序

#include <iostream>
using namespace std;
int main()
{
  // 请在此输入您的代码
  int school_lesson, extra_lesson;
  
  for(int i=1;i<=7;i++) //周几
  {
      cin >> school_lesson >> extra_lesson;
      if(school_lesson + extra_lesson > 8)
      {
          cout << i;
          return 0;  //结束程序
      }
  }
}

找到最多的数

题目链接

【参考代码和思路】

虽然是矩阵,但是可以存在一维数组里面 

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
  // 请在此输入您的代码
  int n,m;
  cin>>n>>m;
  int a[1000001];
  int size = n*m;
  for(int i=0;i< n*m; i++)
  {
      cin>>a[i];
  }
  sort(a, a + size);
  cout << a[size / 2];
  return 0;
}

算法大挑战

题目链接

【参考代码】

#include <iostream>
using namespace std;
int main()
{
  // 请在此输入您的代码
  int L, r, result = 0;
  cin>>L>>r;
  for(int i=L;i<=r;i++)
  {
      if(i % 3 == 0)
        result++;
  }
  cout << result;
  return 0;
}

阿坤老师的烘焙挑战(简易版)

题目链接

【参考代码】

#include <iostream>
using namespace std;
int main()
{
  // 请在此输入您的代码
  int n, result = 0;
  int a[201];
  cin>>n;
  for(int i=0;i<n;i++)
  {
      cin>>a[i];
  }
  for(int i=0;i<n;i++)
  {
      for(int j=i+1;j<n;j++)
      {
          if( (a[i] + a[j]) % 3 == 0)
            result++;
      }
  }
  cout << result;
  return 0;
}

杂题:

矩阵拼接

题目链接

【参考代码】

#include <iostream>
using namespace std;
int main()
{
  // 请在此输入您的代码
  int t;
  cin>>t;
  int a[3][2];
  for(int i=0;i<t;i++)
  {
      int result = 8;
      cin>>a[0][0]>>a[0][1]>>a[1][0]>>a[1][1]>>a[2][0]>>a[2][1];
      for(int b=0;b<3;b++) //第一个矩形
      {
          for(int c=0;c<3;c++) //第二个矩形
          {
              if(b != c) //避免重复读数
              {
                  for(int d=0;d<3;d++) //第三个矩形
                  {
                      if(d != b && d != c)
                      {
                          for(int e=0;e<=1;e++)
                          {
                              for(int f=0;f<=1;f++)
                              {
                                  for(int g=0;g<=1;g++)
                                  {
                                      if(a[b][e] == a[c][f] + a[d][g]) //例2的情形
                                      {
                                          result = min(result, 6);

                                          if(a[c][1-f] == a[d][1-g]) //例1的情形,一横两竖
                                          //竖:1-f/1-g
                                            result = min(result, 4);
                                      }
                                      //任意一条边匹配,都是6边形
                                      if(a[b][e] == a[c][f] || a[b][e] == a[d][g])
                                        result = min(result, 6);
                                      //所有边匹配,4边形
                                      if(a[b][e] == a[c][f] && a[b][e] == a[d][g])
                                        result = min(result, 4);
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
      cout << result << endl;   
  }
  return 0;
}

队列习题:

队列的成员函数应用:小桥的神秘礼物盒

题目链接

【参考代码】

#include <iostream>
#include <queue>
using namespace std;
int main()
{
  // 请在此输入您的代码
  int n, x, operation;
  cin>>n;
  queue<int> q;
  for(int i=0;i<n;i++)
  {
      cin>>operation;
      if(operation == 1)
      {
          cin >> x;
          q.push(x);
      }
      if(operation == 2)
      {
          if(q.empty())
            cout << "lan" << endl;
          else
            q.pop();
      }
      if(operation == 3)
      {
          if(q.empty())
            cout << "qiao" << endl;
          else
            cout << q.front() << endl;
      }
      if(operation == 4)
      {
            cout << q.size() << endl;
      }
  }
  return 0;
}

队列的成员函数应用:餐厅排队

题目链接

【参考代码】

#include <iostream>
#include <queue>
using namespace std;
int main()
{
  // 请在此输入您的代码
  int operation, n, x;
  queue<int> q;
  cin>>n;
  for(int i=0;i<n;i++)
  {
      cin>>operation;
      if(operation == 1)
      {
          cin >> x;
          q.push(x);
      }
      if(operation == 2)
      {
            q.pop();
      }
      if(operation == 3)
      {
          if(q.size() == 1)
            cout << q.front() << " " << q.front() << endl;
          else
            cout << q.front() << " " << q.back() << endl;
      }
  }
  return 0;
}

栈习题

小鱼的数字游戏

题目链接

【参考代码】

解法1:

//法一
#include <iostream>
#include <stack>
using namespace std;

long long a[101] = {0};

int main()
{
    int number;
    stack<long long> s;
    while(1)
    {
        cin>>number;
        if(number == 0)
            break;
        else
        {
            s.push(number);
        }
    }
    while(!s.empty())
    {
        cout << s.top() << " ";
        s.pop();
    }
}

 解法2:

//法二
#include <iostream>
#include <algorithm>
//#include <stack>
using namespace std;

long long a[101] = {0};

int main()
{
    long long number, size;
    for(size=0;size<=100;size++)
    {
        cin>> number;
        if(number == 0)
            break;
        a[size] = number;
    }
    reverse(a, a+size);
    for(int i=0;i<size;i++)
    {
        cout << a[i] << " ";
    }
}

后缀表达式

题目链接

【参考代码】

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

int main()
{
    long long number = 0;
    char op;
    stack<int> s;
    while( (op = getchar())!='@' ) //读入一个字符
	{
		int elem1, elem2;
        if(op >= '0' && op <= '9') 
        {
        	number *= 10; 
			number += op - '0';
		}
			
        else if(op == '.')
		{
            s.push(number);
            number = 0;       //归零
        }
        else if(op == '+')
		{
            elem1 = s.top();
            s.pop();
            elem2 = s.top();
            s.pop();
            s.push(elem1 + elem2);
        }
        else if(op == '-')
		{
            elem1 = s.top();
            s.pop();
            elem2 = s.top();
            s.pop();
            s.push(elem2 - elem1);
        }
        else if(op == '*')
		{
            elem1 = s.top();
            s.pop();
            elem2 = s.top();
            s.pop();
            s.push(elem1 * elem2);
        }
        else if(op == '/')
		{
            elem1 = s.top();
            s.pop();
            elem2 = s.top();
            s.pop();
            s.push(elem2 / elem1);
        }
    }
    cout << s.top();
    return 0;
}

【模板】栈

题目链接

【参考代码】

64位以上建议使用__int64, __int128

#include <iostream>
#include <stack>
#include <string>
using namespace std;
// push(x):向栈中加入一个数 x。
// pop():将栈顶弹出。如果此时栈为空则不进行弹出操作,输出 Empty。
// query():输出栈顶元素,如果此时栈为空则输出 Anguei!。
// size():输出此时栈内元素个数。
int main()
{
    int t;
    stack<unsigned long long> stack;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            string s;
            cin>>s;
            if(s == "push")
            {
                unsigned long long x;
                cin>>x;
                stack.push(x);
            }
            if(s == "query")
            {
                if(stack.empty())
                    cout << "Anguei!" << endl;
                else
                	cout << stack.top() << endl;
            }
          
            if(s == "size")
            {
                cout << stack.size() << endl;
            }
            if(s == "pop")
            {
                if(stack.empty())
                    cout << "Empty" << endl;
                else
                	stack.pop();
            }
        }
        while(!stack.empty())
            stack.pop();
    }
}

  • 26
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值