华科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;}
阅读全文
0 0
- 华科15_3
- 华科16_3
- 英语语法_3
- 12_3
- 2010_3
- 2_3
- 4_3
- 3_3
- 2_3
- 7_3
- 12_3
- 工作总结_3
- 集合_3
- 三分_3
- 贪心_3
- 练习题_3
- 牛客_3
- hibernate基础_3
- linux yum安装mysql及配置
- 斯坦福CS231N课程学习笔记
- ibatis 到 MyBatis区别
- wordpress后台汉化
- Java8系列--Java Stream入门篇(什么是Stream)
- 华科15_3
- java方法参数传递方式
- C++输入输出总结
- HDU 3342&&UVA10305&&考试day3 T1
- 多线程的共享资源的解决方案
- linux 安装mysql数据库——tar.gz包解压安装法
- Eclipse之task任务提醒
- JAVA中的this
- Quartz入门指南