华科15_3

问题:

输入一个字符串 

1.对于不是首次出现的字符进行过滤 入abcdacdef过滤后为abcdef

2.对于字符0-9 A-F a-f 将其对应的ASCII码的低4位进行对调,如1011对调后变为1101.将对调后的ASCII码对应的字符输出,若为字母,转换为大写。

分析:

1.过滤重复的大写字母、小写字母、数字,采用一个标记数组来记录某个字符是否出现过。

2.ASCII码对调。

先将字符的ASCII码转化为2进制表示,然后对调后四位,然后再转换为10进制进行输出。

代码:

#include <iostream>  
#include <stdio.h>   
#include <string.h>  
#include <math.h>  
#include <vector>  
#include <queue>  
#include <stack>  
#include <map>  
#include <algorithm>  
#include <iomanip>
#define MAX 1000
using namespace std;
bool mark1[26] ; //标记26个小写字母是否出现 
bool mark2[26] ; //标记26个大写字母是否出现 
bool mark3[10] ; //标记10个数字是否出现 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int change(int x){
	int tmp = x;
	int num[100] ; // 存储后四位
	int size=0; 
	while(tmp!=0){
		num[size++] = tmp%2;
		tmp/=2; 
	}
	
	//倒置低四位 
	swap(num[0],num[3]);
	swap(num[1],num[2]);
	
	int ans=0;
	int c=1; 
	for(int i=0;i<size;i++){
		ans+=num[i]*c;
		c*=2;
	}
	return ans;
}
void display(char *str,int size){
	for(int i=0;i<size;i++){
		printf("%c",str[i]);
	}
	
	printf("\n");
}
int main(int argc, char** argv) {
	char  str[MAX];
	int size=0;
	
	//1.过滤不是首次出现的字符
	memset(mark1,false,sizeof(mark1)) ;
	memset(mark2,false,sizeof(mark2)) ;
	memset(mark3,false,sizeof(mark3)) ;
	
	char tmp;
	while((tmp = getchar())!= '\n') {
		if(tmp >= 'a' && tmp <='z')	{
			if(mark1[tmp-'a'] == false){
				mark1[tmp-'a'] = true;
				str[size++]=tmp;
			}else{
				continue;
			}
		}else if(tmp >= 'A' && tmp <='Z'){
			if(mark2[tmp-'A'] == false){
				mark2[tmp-'A'] = true;
				str[size++]=tmp;
			}else{
				continue;
			}
		}else{
			if(mark3[tmp-'0'] == false) {
				mark3[tmp-'0'] = true;
				str[size++] = tmp;
			}else{
				continue;	
			}
		}
	}
	//	display(str,size);
	//2.将数字的ASCII码的低四位进行对调   若为字母则转换为大写 
	for(int i=0;i<size;i++) {
		if((str[i] >= '0' && str[i] <= '9') || (str[i] >= 'a' && str[i] <= 'f') || (str[i] >= 'A' && str[i] <= 'F')){
			str[i] = change(str[i]);
			
			if(str[i] >= 'a' && str[i] <= 'z'){
				str[i] = 'A'+(str[i]-'a');
			}
		}
	}
	 display(str,size);
	 
	return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值