2022.02.21

PPT(1)最后一题

题意:写一个程序来检查C程序的基本语法错误,如不匹配的圆括号、方括号和大括号。 不要忘记单引号和双引号、转义序列和注释。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
stack<char> st;
bool judge(string s)
{
	for (int i = 0;i < s.size();i++)
	{
		if (s[i] == '(' || s[i] == '[' || s[i] == '{' || s[i] == '\'' || s[i] == '"')
		{
			st.push(s[i]);//遇到左圆括号,左方括号,左花括号,单引号,双引号就入栈
		}
		else if (s[i] == ')')
		{
			if (st.top() != '(') {
				return false;
			}
			else st.pop();
		}
		else if (s[i] == ']')
		{
			if (st.top() != '[') {
				return false;
			}
			else st.pop();
		}
		else if (s[i] == '}')
		{
			if (st.top() != '{') {
				return false;
			}
			else st.pop();
		}
		else if (s[i] == '\'')
		{
			if (st.top() != '\'') {
				return false;
			}
			else st.pop();
		}
		else if (s[i] == '"')
		{
			if (st.top() != '"') {
				return false;
			}
			else st.pop();
		}
	}
	if (st.size()) return false; //如果栈不为空则不合法
	return true;
}
int main()
{
	string s;
	cin >> s;//输入一个语句,我们的目标是判断该语句是否合法
	bool isLegal=judge(s);
	if (isLegal) cout << "YES" << endl;//输出YES说明语句合法
	else cout << "NO" << endl;//输出NO说明语句非法
	return 0;
}

PPT(2)最后一题

题意:实现一个函数,该函数传入x和n,然后返回一个新x,该新x这样得到:将x从最右端移出的n位将从最左端再移入得到一个新x

为让问题简化,我们假设n不大于x的二进制表示下的长度

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
int rightrot(int n, int x)
{
	int len = 0;
	int temp = x;
	while (temp)
	{
		temp >>= 1;
		len++;           //计算x在二进制表示下的长度                
	}
	//cout << len << endl;
	int lef = 0, t = 1;      //lef表示放到左边的数,比如x=101010,要移动3位,lef就为010000
	for (int i = 0;i < n;i++)
	{
		if (x >> i & 1) lef += t;
		t *= 2;
	}
	int cnt = len - n;
	while (cnt--)
	{
		lef <<= 1;
	}
	int rig = 0;        //rig表示右边的数,比如x=101010,移了3位后,左边3位顺势到了右边,所以右边的数
	                    //为000101
	t = 1;
	for (int i = n;i < len;i++)
	{
		if (x >> i & 1) rig += t;
		t *= 2;
	}
	//cout << lef << " " << rig << endl;
	return lef + rig; 
}
int main()
{
	int x, n;
	cin >> x >> n;
	cout << rightrot(n,x) << endl;
	return 0;
}

PPT(3)最后一题

题意:编写一个接受三个而不是两个参数的版本的itoa函数。 第三个参数是最小字段宽度; 为了使转换后的数字足够宽,必须在左边填充空格。这里就不介绍itoa函数了

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
string new_itoa(int n,int radix,int wide)
{
	int len = 0;
	vector<int> res;
	while (n)        //将n从10进制转成radix进制,len记录转换后的长度
	{
		len++;
		res.push_back(n%radix);
		n /= radix;
	}
	string output;
	if (len < wide)
	{
		int cnt = wide - len;
		while (cnt--)
		{
			output.push_back(' ');  //补充空格
		}
	}
	for (int i = res.size() - 1;i >= 0;i--)
	{
		output.push_back(res[i]+'0');
	}
	return output;
}
int main()
{
	int n, r, w;
	cin >> n >> r >> w;
	cout << new_itoa(n, r, w) << endl;
	return 0;
}

PPT(4)最后一题

题意:写一个递归版的reverse函数

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
string s;
void recursive_reverse(int l, int r)
{
	if (l >= r) return; //只有l<r才进行交换,否则返回
	swap(s[l], s[r]);
	recursive_reverse(l + 1, r - 1);
}
int main()
{
	cin >> s;
	recursive_reverse(0, s.size() - 1);
	cout << s << endl;
	return 0;
}

 PTT(5)最后一题

题意:用指针代替数组索引重写前面章节和练习中的适当程序

我选择用指针写一下递归版的reverse函数

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
string s;
void recursive_reverse(char *l, char *r)
{
	if (l >= r) return; //只有l<r才进行交换,否则返回
	char temp = *l;
	*l = *r;
	*r = temp;
	recursive_reverse(l + 1, r - 1);
}
int main()
{
	cin >> s;
	char * l = &s[0];
	char * r = &s[s.size() - 1];
	recursive_reverse(l,r);
	cout << s << endl;
	return 0;
}

PTT(6)最后一题

 题意:编写一个程序,按出现频率的递减顺序打印输入中的不同单词。 按字数排在每个单词之前

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
using namespace std;
typedef pair<int, string> PIS;
map<string, int> h;
string s;
PIS p[100010];
int cnt;
int main()
{
	while (cin >> s)//读入多个单词
	{
		h[s]++;            //统计单词出现次数
	}
	for (auto x : h)
	{
		p[cnt++] = { x.second,x.first };
	}
	sort(p, p + cnt);  //根据次数从小到大排序
	for (int i = cnt - 1;i >= 0;i--)
	{
		cout << p[i].second << endl;
	}
	return 0;
}

PPT(7)最后一题

题意:实现isupper函数

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
string s;
int my_isupper(char c)
{
	if (65 <= c && c <= 90) return 1; //如果字符的ASCII码值在65到90之间则是大写字母
	return 0;
}
int main()
{
	char c;
	cin >> c;
    cout<<my_isupper(c)<<endl;
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值