NC16644【字符串的展开】
正确代码:
#include <iostream>
#include <algorithm>
using namespace std;
bool IsSame(char a, char b)
{
if(a<='9'&&a>='0'&&b<='9'&&b>='0')
{
return 1;
}
else if (a <= 'z' && a >= 'a' && b <= 'z' && b >= 'a')
{
return 1;
}
return 0;
}
int main(void)
{
int p1, p2, p3;
string str, t;
char k;
cin >> p1 >> p2 >> p3;
cin >> str;
int i = 0, j = 0;
while (str[i] != '\0')
{
if (str[i] != '-')
{
cout << str[i];
}
else if (str[i] == '-' && i - 1 >= 0 && i + 1 < str.length())
{
if (str[i - 1] >= str[i + 1] || !IsSame(str[i-1],str[i+1]))
cout << str[i];
else if (str[i - 1] + 1 < str[i + 1] && IsSame(str[i-1],str[i+1]))
{
if (p1 == 2 && str[i - 1] <= 'z' && str[i - 1] >= 'a')
{
for (k = str[i - 1] + 1;k < str[i + 1];k++)
{
for (int x = 0;x < p2;x++)
{
t += k - 'a' + 'A';
}
}
}
else if (p1 == 3)
{
for (k = str[i - 1] + 1;k < str[i + 1];k++)
{
for (int x = 0;x < p2;x++)
{
t += '*';
}
}
}
else
{
for (k = str[i - 1] + 1;k < str[i + 1];k++)
{
for (int x = 0;x < p2;x++)
t += k;
}
}
if (p3 == 2)
{
reverse(t.begin(), t.end());
}
cout << t;
t = "";
}
}
else
cout << str[i];
i++;
}
}
错误代码:
#include <iostream>
#include <algorithm>
using namespace std;
int main(void)
{
int p1, p2, p3;
string str, t;
char k;
cin >> p1 >> p2 >> p3;
cin >> str;
int i = 0, j = 0;
while (str[i] != '\0')
{
if (str[i] != '-')
{
cout << str[i];
}
else if (str[i] == '-' && i - 1 >= 0 && i + 1 < str.length())
{
if (str[i - 1] >= str[i + 1]||str[i - 1]+26<str[i+1])
cout << str[i];
else if (str[i - 1] + 1 < str[i + 1] && str[i - 1]+26>str[i+1])//此处错误
{
if (p1 == 2 && str[i - 1] <= 'z' && str[i - 1] >= 'a')
{
for (k = str[i - 1] + 1;k < str[i + 1];k++)
{
for (int x = 0;x < p2;x++)
{
t += k - 'a' + 'A';
}
}
}
else if (p1 == 3)
{
for (k = str[i - 1] + 1;k < str[i + 1];k++)
{
for (int x = 0;x < p2;x++)
{
t += '*';
}
}
}
else
{
for (k = str[i - 1] + 1;k < str[i + 1];k++)
{
for (int x = 0;x < p2;x++)
t += k;
}
}
if (p3 == 2)
{
reverse(t.begin(), t.end());
}
cout << t;
t = "";
}
}
else
cout << str[i];
i++;
}
}
总结:考虑以下可能:① --- ,② -a-d- ,③ -1-a- ④ -d-a ,这些是式子不变,直接输出,然而为了排掉 数字 - 字母 ,选择用str[i-1]+26>str[i+1],因为 Asc(9)+26 < Asc(a),Asc(0)+26 > Asc(9) ,因此觉得可以用于判断 数字 - 字母 的组合,但是忽略了 -的ASCII为45 ,Asc(-)+26 > Asc(0),因此 6--0未通过测试
最新文章
- 如何开启MySQL 5.7.12 的二进制日志
- 前端MVC框架Backbone 1.1.0源码分析系列
- ASP.NET和IIS工作原理
- ISP接口隔离原则
- 【翻译】CEDCE2010 制作魅力绘制而要知道的光学小知识
- php中PCRE正则表达式分隔符的使用
- css position: absolute、relative详解
- Magento控制器
- 九度oj 1528 最长回文子串
- php笔记07:http响应详解(禁用缓存设置和文件下载)
- Library中的title与Name
- VIM+qmake编译示例程序HelloQt出错问题的解决(文件名一定要使用.cpp,否则就会默认使用gcc编译,当然通不过)
- Mybatis——choose, when, otherwise可以达到switch case效果
- jquery 中json数组的操作 增删改
- Spring Data Jpa简单了解
- python实现简体中文和繁体相互转换
- jquery 同步加载
- Reactjs组件中的方法为什么绑定this?
- Python连接MySQL数据库之pymysql模块
- POJ - 1222: EXTENDED LIGHTS OUT (开关问题-高斯消元)
热门文章
- pg高可用方案repmgr带witness搭建
- DesignPatternPrinciple-设计模式原则
- Code Review在TDSQL-C 的应用实践
- DataGridView添加新一行数据可添加到最后一行或第一行
- [UnityShader]unity中2D Sprite显示阴影和接受阴影
- 2021.09 ccf csp 第四题 收集卡牌
- pytest框架增加log打印(包括pytest的执行结果、自定义的log信息)
- Oracle 的merge into 语法转postgre
- ES之分析器(Analyzer)及拼音分词器
- 【面试题】XSS攻击是什么?