华科15_3

来源:互联网 发布:淘宝visa卡怎样付款 编辑:程序博客网 时间:2024/05/21 06:17

问题:

输入一个字符串 

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 1000using 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;}


 

原创粉丝点击