| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 474 人关注过本帖
标题:错误在哪里 求解答
取消只看楼主 加入收藏
bbb222
Rank: 2
等 级:论坛游民
帖 子:31
专家分:54
注 册:2012-11-17
结帖率:77.78%
收藏
已结贴  问题点数:10 回复次数:3 
错误在哪里 求解答
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

void swap()
{
        string str;
        int a[50],b=0;
        getline(cin,str);

        int leng=str.length();
        for(int j = 0; j < leng; j++)
        {
            if(str[j] == ' ')
            {
                b++;
                a[b] = j;
            }
        }
        a[0] = -1;
        string str1[b+1];
        int record[b+1];

        for(int k = 0; k < b; k++)
        {
            int n = 0;
            for(int j = a[k]+1;j < a[k+1]; j++)
            {
                str1[k][n] = str[j];
                record[k+1] = n;
                n++;
            }
        }
        int m = 0;
        for(int j = a[b]+1; j < leng; j++)
        {
            str1[b][m] = str[j];
            record[b+1] = m;
            m++;
        }
        record[0] = 0;
        for(int j = 0; j < b+1; j++)
        {
            for(int k = record[j+1]; k >= 0; k--)
            {
                cout << str1[j][k];
            }
            cout << " ";
        }
        cout << endl;

}
int main()
{
    int times,leng;
    cin >> times;
    cin.get();
    for(int i = 0; i < times; i++)
    {
        swap();
    }


    return 0;
}
这个是错的,我后来换了种思路做了如下。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

void swap()
{
        string str;
        int a[50],b=0;
        getline(cin,str);

        int leng=str.length();
        for(int j = 0; j < leng; j++)
        {
            if(str[j] == ' ')
            {
                b++;
                a[b] = j;
            }
        }
        a[0] = -1;
        a[b+1] = leng;
        string st[b+1];
        char c;

        for(int j = 0; j < b+1; j++)
        {
            if(j ==  b)
            {
            st[j] = str.substr(a[j]+1,a[j+1]-a[j]-1);

            }
            if(j != b)
            {
            st[j] = str.substr(a[j]+1,a[j+1]-a[j]-1);

            }
        }
        for(int j = 0; j < b+1; j++)
        {
            for(int k = 0; k < (a[j]+1,a[j+1]-a[j])/2; k++){
            c = st[j][k];
            st[j][k]= st[j][a[j]+1,a[j+1]-a[j]-2-k];
            st[j][a[j]+1,a[j+1]-a[j]-2-k] = c;
            }
            if(j == b)
            cout << st[j]<<endl;
            if(j != b)
            cout << st[j] << " " ;
        }


}
int main()
{
    int times,leng;
    cin >> times;
    cin.get();
    for(int i = 0; i < times; i++)
    {
        swap();
    }


    return 0;
}
我想知道为什么第一种方案 运行一次会运行不下去。
搜索更多相关主题的帖子: void include record 
2012-11-18 09:53
bbb222
Rank: 2
等 级:论坛游民
帖 子:31
专家分:54
注 册:2012-11-17
收藏
得分:0 
题目是

For each list of words, output a line with each word reversed without changing the order of the words.


Input

You will be given a number of test cases. The first line contains a positive integer indicating the number of cases to follow. Each case is given on a line containing a list of words separated by one space, and each word contains only uppercase and lowercase letters.


Output

For each test case, print the output on one line.


Sample Input

3
I am happy today
To be or not to be
I want to win the practice contest


Sample Output

I ma yppah yadot
oT eb ro ton ot eb
I tnaw ot niw eht ecitcarp tsetnoc

2012-11-18 10:20
bbb222
Rank: 2
等 级:论坛游民
帖 子:31
专家分:54
注 册:2012-11-17
收藏
得分:0 
编译过了
定义也过了
输出是不正常...
2012-11-22 23:41
bbb222
Rank: 2
等 级:论坛游民
帖 子:31
专家分:54
注 册:2012-11-17
收藏
得分:0 
那应该如何改进
2012-11-23 10:21
快速回复:错误在哪里 求解答
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017176 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved