咖啡


私信TA

用户名:Tianxn

访问量:129048

签 名:

十年OI一场空,不开LL见祖宗。

等  级
排  名 9
经  验 26176
参赛次数 10
文章发表 197
年  龄 22
在职情况 学生
学  校 西安电子科技大学
专  业 软件工程

  自我简介:

解题思路:

我的思路就是使用一个数组来储存每一个字符出现的次数,最后判断对应位置上的数组元素是否为1(因为删除的重复元素且不相邻的也算),例如:先设两个数组 char a[101]; int b[101](b的初始值全都为0);a用来储存输入的字符串,b用来判断字符串每一个位置上的字符串出现的次数。看下面例子:

a="432112",遍历这个字符串:第一个元素是4,那把b的4号位的元素加一(即0+1=1);第2个元素是3,那么把b数组3号位的元素加1;第3个元素把b数组2号位元素值加一,第4个元素是1把b数组的1号位元素值加一,第5个元素是1,把b数组的1号位元素加一(注意此时b数组的1号位元素值为2,因为前面加过一次了);后面原理一样。最终a、b数组的元素如下;

元素

下标

012345678
字符串a432112\0

数组b
2211



上面表格得出;不重复的元素只有3 4(因为出现次数均为1)



注意事项:


注意b数组的的含义

参考代码:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cctype>
#include <cstring>
#include <string>
#include <stack> 
#include <algorithm>
#include <functional>
using namespace std;
const int maxn=1<<8;
char p[maxn];
int str[maxn];
int main()
{
	fgets(p, maxn, stdin);
	for(int i = 0, len = strlen(p); i < len; ++i)
	{
		str[p[i]]++;    //统计每一个字符出现的次数
	}
	for(int i = 0, len = strlen(p); i < len; ++i)    //遍历字符串输出只出现1次的字符
	{
		if(str[p[i]] == 1)
		{
			printf("%c", p[i]);
		}
	}
	printf("\n");
	return 0;
}

或许有人数,题目要求要用指针,其实用数组来做本质上就是操作指针的,那么我在给一个指针的代码:

(注意数组和指针的异同,数组名不能用自增自减运算符,因为--  ++作用的对象是左值)

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cctype>
#include <cstring>
#include <string>
#include <stack> 
#include <algorithm>
#include <functional>
using namespace std;
const int maxn=1<<8;
char p[maxn];
int str[maxn];
int main()
{
	fgets(p, maxn, stdin);
	for(int i = 0; *(p+i); ++i) //注意不要用++p
	{
		str[*(p+i)]++;
	}
	for(int i = 0; *(p+i); ++i)
	{
		if(str[*(p+i)] == 1)
		{
			printf("%c", *(p+i));
		}
	}
	printf("\n");
	return 0;
}


 

0.0分

5 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

补充一点题目不严谨,按照题意应该包含  空格  等字符,可是用scanf读取也不会错(也就说题目中的字符串中没有空格)
当然我上面写的代码是包含了空格这种情况的,大家可以仔细斟酌一下。
2018-12-03 23:06:18
  • «
  • 1
  • »