正确代码:

#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未通过测试

NC16644【字符串的展开】的更多相关文章

  1. AC日记——字符串的展开 openjudge 1.7 35

    35:字符串的展开 总时间限制:  1000ms 内存限制:  65536kB 描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h ...

  2. TYVJ P1053 字符串的展开 Label:字符 水

    背景 NOIP2007年提高组第2道 描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或“4-8”的子串,我们就把它当作一种简 ...

  3. Luogu 1098 - 字符串的展开 - [字符串操作][模拟]

    题目链接:https://www.luogu.org/problemnew/show/P1098 题目描述在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中 ...

  4. 洛谷P1098 字符串的展开【字符串】【模拟】

    题目描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输出时,用连续递增的字母或数 ...

  5. C语言 · 字符串的展开

     算法训练 字符串的展开   时间限制:1.0s   内存限制:256.0MB      在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d ...

  6. noip200706字符串的展开

    试题描述: 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输出时,用连续递增的字母获 ...

  7. 洛谷 P1098 字符串的展开

    题目描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输出时,用连续递增的字母或数 ...

  8. 洛谷P1098 字符串的展开

    P1098 字符串的展开 题目描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输 ...

  9. 洛谷——P1098 字符串的展开

    P1098 字符串的展开 题目描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输 ...

  10. 洛谷—— P1098 字符串的展开

    https://www.luogu.org/problem/show?pid=1098 题目描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类 ...

随机推荐

  1. 4. Lighting 窗口

    Lighting 实现烘焙或者实时渲染都在这里设置,其他灯光或者反射探头的作用相当于允许 Lighting (窗口)烘焙或者实时渲染. 0bject: Lightmap Static: 把烘焙的对象设 ...

  2. 安装torch_scatter,torch-sparse,torch-cluster,torch-spline-conv,torch-geometric

    1. 查询torch版本号 进入https://pytorch-geometric.com/whl/ 找到对应的torch版本>>点击进入 2. 找到匹配的包 点击下载即可 3. 使用pi ...

  3. HTML初步了解

    W3C:万维网联盟,是国际化最著名的标准化组织. HTML:(Hyper Text Markup Language )超文本标记语言,网页编程语言,用于定义文档的内容和结构. CSS:(Cascadi ...

  4. Windows.h 文件学习

    SDk :软件开发工具包 Api  :Windows操作系统提供给应用程序编程的接口,windows.h 窗口:窗口是屏幕上的一块矩形区域,是Windows应用程序与用户进行交互的接口,分为客户区与非 ...

  5. c++ class基础知识

    目录 class 创建对象 通过对象指针访问类成员 类方法可以定义在类体外部(要用到域解析符:. 类成员/类方法访问权限 构造函数 析构函数Destructor this指针 静态变量 静态函数 co ...

  6. Neo4j插件安装

    Neo4j插件安装 Author:wss Date:2022.6.9 Topic:Neo4j插件安装 一.前言 昨天再次安装Apoc插件,又去找之前看的教程,有些地方不够清晰要几个教程对比着看,想到可 ...

  7. (jmeter笔记) websocket接口测试

    1.在进程选择WebSocket Sampler 2.Websocket Sampler 界面 webserver Server Name or IP:输入连接的websocket服务器ip Port ...

  8. cdn全栈加速nginx二层代理实现

    1 nginx.conf中添加配置如下: server { listen 5050; location / { proxy_pass http://代理IP:3333; proxy_set_heade ...

  9. iOS开发之从UIColo到十六进制

    今天开发中需要将UIColor转为十六进制字符串,记录下修改代码 UIColor *color = [UIColor blueColor]; const CGFloat* components = C ...

  10. js-惰性函数

    1. 需求:我们现在需要写一个 foo 函数,这个函数返回首次调用时的 Date 对象,注意是首次. 使用场景:当我们每次都需要进行条件判断,其实只需要判断一次,接下来的使用方式都不会发生改变的时候, ...