字符串全排列代码问题

xqn2017 2017-09-21 10:21:31
void Permutation(char *pStr, char *pBegin)
{
char *pCh = NULL;
char temp = 0;
if(pBegin == '\0')
{
printf("%s\n",pStr);
}
else
{
for(pCh = pBegin;*pBegin !='\0';++pCh)
{
temp = *pCh;
printf("%c%c",*pCh,*pBegin);
*pCh = *pBegin;
*pBegin = temp;
printf("%s\n",pStr);
}
Permutation(pStr,pBegin+1);
}
}
void Permutation(char* pStr)
{
if(!pStr)
{
return;
}

Permutation(pStr,pStr);
}

int main()
{
char *str = "bad";
Permutation(str);
return 0;
}

printf("%c%c",*pCh,*pBegin);有打印,下一行就报错了
错误信息:
Unhandled exception at 0x0040121e in DataProc.exe: 0xC0000005: Access violation writing location 0x00459d8c.
...全文
235 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
棉猴 2017-09-22
  • 打赏
  • 举报
回复
#5楼说的对 1 问题分析 void Permutation(char *pStr, char *pBegin)函数中的
*pCh = *pBegin;
代码,pCh和pBegin都指向常量“bad”,无法对pCh指向的内容进行赋值,所以报错! 2 问题解决 可以考虑在void Permutation(char *pStr, char *pBegin)函数定义字符数组,用来保存常量“bad” 这个数组用于函数中需要修改字符串内容的变量
Tragedyclown 2017-09-21
  • 打赏
  • 举报
回复
函数有错误①pre函数无法识别str字符串,函数有重名。算法也有问题。再找一下例子对比一下吧。
paschen 版主 2017-09-21
  • 打赏
  • 举报
回复
char *str = "bad"; 中的字符串是在常量区的,不能修改,如果要修改,可以在栈上创建字符数组来存储字符串
自信男孩 2017-09-21
  • 打赏
  • 举报
回复
报错的原因是 main中的str是指向只读字符串,但是Permutation函数是对其修改了,所以是非法访问了。 还有其他问题:函数名重名,这个在编译阶段肯定会报错吧,除非你用的C++编译器,支持重载;
if(pBegin == '\0')   /*这句有问题*/
    {
        printf("%s\n",pStr);
    }
改成这样:
 if(*pBegin == '\0')
    {
        printf("%s\n",pStr);
    }
最后,你的处理函数逻辑不是全排列,建议先研究一下算法;
#include <stdio.h>


void Permutation(char *pStr, char *pBegin)
{
    char *pCh = NULL;
    char temp = 0;
    if(*pBegin == '\0')
    {
        printf("%s\n",pStr);
    }
    else
    {
        for(pCh = pBegin;*pCh !='\0';++pCh)
        {
            temp = *pCh;
            printf("%c%c",*pCh,*pBegin);
            *pCh = *pBegin;
            *pBegin = temp;
            printf("%s\n",pStr);
        }
        Permutation(pStr,pBegin+1);
    }
}
void Permutation1(char* pStr)
{
    if(!pStr)
    {
        return;
    }

    Permutation(pStr,pStr);
}

int main()
{
    char str[] = "bad";
    Permutation1(str);
    return 0;
}
这是改后能正常运行的代码,但是逻辑没改,所以输出的结果不是全排后的结果,建议先研究全排算法吧
  • 打赏
  • 举报
回复
给你调的能运行了,但是逻辑对不对就先不管了哈。

#include<stdio.h>
void Permutation(char *pStr, char *pBegin)
{
	char *pCh = NULL;
	char temp = 0;
	if (*pBegin == '\0')
	{
		printf("%s\n", pStr);
	}
	else
	{
		for (pCh = pBegin; *pCh != '\0'; ++pCh)
		{
			temp = *pCh;
			printf("%c%c", *pCh, *pBegin);
			*pCh = *pBegin;
			*pBegin = temp;
			printf("%s\n", pStr);
		}
		Permutation(pStr, pBegin + 1);
	}
}
void Permutation(char* pStr)
{
	if (!pStr)
	{
		return;
	}

	Permutation(pStr, pStr);
}

int main()
{
	char str[] = "bad";
	Permutation(str);
	return 0;
}
赵4老师 2017-09-21
  • 打赏
  • 举报
回复
仅供参考:
//qplw.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int v=0;
int w=0;
int m;//记录字符串长度
int n;//记录字符串中的字符种类数
char map[256];//记录是哪几种字符
int count[256];//记录每种字符有多少个
int stack[1000];//递归用的栈,并记录当前生成的排列
void Make_Map(char *str) {//统计字符串的相关信息
    int s[256];
    int i;
    memset(s,0,sizeof(s));
    memset(count,0,sizeof(count));
    m=strlen(str);
    if (w<1 || m<w) w=m;
    while(*str) {
        s[*str]++;
        str++;
    }
    n=0;
    for (i=0;i<256;i++)
        if (s[i]) {
            map[n]=i;
            count[n]=s[i];
            n++;
        }
}
void Find(int depth) {//递归式回溯法生成全排列
    if (depth==w) {
        int i;
        for (i=0;i<depth;i++) putchar(map[stack[i]]);
        putchar('\n');
    } else {
        int i;
        if (v && depth>0) {
            for (i=0;i<depth;i++) putchar(map[stack[i]]);
            putchar('\n');
        }
        for (i=0;i<n;i++)
            if (count[i]) {
                stack[depth]=i;
                count[i]--;
                Find(depth+1);
                count[i]++;
            }
    }
}
void main(int argc,char**argv) {
    if (argc<2) {
        printf("%s 要产生全排列的字符串 [限定长度|-1]\n",argv[0]);
        return;
    }
    if (argc>=3) w=atoi(argv[2]);
    if (-1==w) v=1;
    Make_Map(argv[1]);
    Find(0);
}
//C:\test>qplw
//qplw 要产生全排列的字符串 [限定长度|-1]
//
//C:\test>qplw 123
//123
//132
//213
//231
//312
//321
//
//C:\test>qplw 123 2
//12
//13
//21
//23
//31
//32
//
//C:\test>qplw 122333 3
//122
//123
//132
//133
//212
//213
//221
//223
//231
//232
//233
//312
//313
//321
//322
//323
//331
//332
//333
//
//C:\test>qplw 123 -1
//1
//12
//123
//13
//132
//2
//21
//213
//23
//231
//3
//31
//312
//32
//321
//

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧