Vegdie 2020-01-24 16:08 采纳率: 100%
浏览 715
已采纳

输出亲朋字符串-看似很简单,但不知为什么错!

亲朋字符串, 求给定字符串 s 的亲朋字符串 s1

s1定义如下:给定字符串 s 的第一个字符的 ASCII 值加第二个字符的 \text{ASCIIASCII 值,得到第一个亲朋字符;给定字符串 s 的第二个字符的 ASCII 值加第三个字符的 ASCII 值,得到第二个亲朋字符;依此类推,直到给定字符串 s 的倒数第二个字符。亲朋字符串的最后一个字符由给定字符串 s 的最后一个字符 ASCII 值加 s 的第一个字符的 ASCII 值。

题目链接见:https://nanti.jisuanke.com/t/T1107

我的代码为什么错,

#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
    string s;
    string ans = "";
    getline(cin, s);
    int length = s.size();
    s[length] = s[0];

    /*
    cout << "ASCLL:" << endl;
    for(int i = 40; i < 130; i++)
    {
        cout << i << "->" << char(i) << endl;
    }
    cout << "----------" << endl;
    cout << int('4') << endl;
    cout << int('5') << endl;
    cout << char(int('4') + int('5')) << endl;

    cout << "----------" << endl;
    */
    for(int i = 0; i < length; i++)
        ans[i] = s[i] + s[i+1];
    for(int i = 0; i < length; i++)
        cout << ans[i];
    cout << endl;
    return 0;
}

错误信息:(答案错误,不是编译错)
已通过 3 组测试数据,共 20 组。
求源代码。

  • 写回答

2条回答 默认 最新

  • threenewbee 2020-01-25 22:32
    关注

    问题解决请点采纳

    // Q1052492.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <stdlib.h>
    
    using namespace std;
    
    int main()
    {
        char s[102];
        char ans[102];
        scanf("%[^\n]", s);
        int length = strlen(s);
        ans[length] = '\0';
    
        /*
        cout << "ASCLL:" << endl;
        for(int i = 40; i < 130; i++)
        {
            cout << i << "->" << char(i) << endl;
        }
        cout << "----------" << endl;
        cout << int('4') << endl;
        cout << int('5') << endl;
        cout << char(int('4') + int('5')) << endl;
    
        cout << "----------" << endl;
        */
        for(int i = 0; i < length - 1; i++)
            ans[i] = s[i] + s[i+1];
        ans[length - 1] = s[0] + s[length - 1];
        for(int i = 0; i < length; i++)
            cout << ans[i];
        cout << endl;
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器