第一題、輸入一個正整數,并編碼為字符串進行輸出
描述: 1、輸入一個正整數,并編碼為字符串進行輸出。
編碼規則為:數字0-9分別編碼為字符a-j
2、輸入肯定是正整數,不用做錯誤較驗
運行時間限制: 無限制
內存限制: 無限制
輸入: 正整數
輸出: 字符串
樣例輸入: 123
樣例輸出: bcd
#include <iostream>
using namespace std;
void int2str(int n,char* str)
{if (str==NULL){return ;}int i=0;char temp[20];while (n){temp[i++]='a'+n%10;n/=10;}temp[i]='\0';i--;int j=0;while (i>=0){str[j++]=temp[i--];}str[j]='\0';
}
void main()
{int n;cin>>n;char a[20];int2str(n,a);cout<<a<<endl;
}
第二題、
通過鍵盤輸入一串小寫字母(a~z)組成的字符串。請編寫一個字符串過濾程序,若字符串中出現多個相同的字符,將非首次出現的字符過濾掉。
比如字符串“abacacde”過濾結果為“abcde”。
要求實現函數:void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
【輸入】 pInputStr: 輸入字符串
lInputLen: 輸入字符串長度
【輸出】 pOutputStr: 輸出字符串,空間已經開辟好,與輸入字符串等長;
【注意】只需要完成該函數功能算法,中間不需要有任何IO的輸入輸出
示例
輸入:“deefd” 輸出:“def”
輸入:“afafafaf” 輸出:“af”
輸入:“pppppppp” 輸出:“p”
main函數已經隱藏,這里保留給用戶的測試入口,在這里測試你的實現函數,可以調用printf打印輸出
當前你可以使用其他方法測試,只要保證最終程序能正確執行即可,該函數實現可以任意修改,但是不要改變函數原型。一定要保證編譯運行不受影響。
/***************************************************************
通過鍵盤輸入一串小寫字母(a~z)組成的字符串。請編寫一個字符串過濾程序,若字符串中出現多個相同的字符,將非首次出現的字符過濾掉。
比如字符串“abacacde”過濾結果為“abcde”。
要求實現函數:void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
【輸入】 pInputStr: 輸入字符串
lInputLen: 輸入字符串長度
【輸出】 pOutputStr: 輸出字符串,空間已經開辟好,與輸入字符串等長;
【注意】只需要完成該函數功能算法,中間不需要有任何IO的輸入輸出
示例
輸入:“deefd” 輸出:“def”
輸入:“afafafaf” 輸出:“af”
輸入:“pppppppp” 輸出:“p”
main函數已經隱藏,這里保留給用戶的測試入口,在這里測試你的實現函數,可以調用printf打印輸出
當前你可以使用其他方法測試,只要保證最終程序能正確執行即可,該函數實現可以任意修改,但是不要改變函數原型。一定要保證編譯運行不受影響。*************************************************************/#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <iterator>
using namespace std;void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{if (pInputStr==NULL||lInputLen<1){return;}int n=0;//用來存儲當前pOutputStr一共從pInputStr獲得多少元素for (int i=0;i<lInputLen;i++){if (i==0){pOutputStr[n++]=pInputStr[i];}else{int flag=0;for (int j=0;j<n;j++){if (pInputStr[i]==pOutputStr[j]){flag=-1;break;}}if (flag==0){pOutputStr[n++]=pInputStr[i];}}}
pOutputStr[n]='\0';
}
void stringFilter2(const char *pInputStr, long lInputLen, char *pOutputStr)//lInputLen指不包含'\0'的個數
//第二種寫法,來自http://blog.csdn.net/net_assassin/article/details/11660869
{bool g_flag[26]={0};if (pInputStr==NULL||lInputLen<1){return;}int i=0;while (*pInputStr!='\0'){if (g_flag[*pInputStr-'a']){pInputStr++;}else{pOutputStr[i++]=*pInputStr;g_flag[*pInputStr-'a']=1;pInputStr++;}}pOutputStr[i]='\0';}
//第三種寫法
void stringFilter3(const char *pInputStr, long lInputLen, char *pOutputStr)
{// 去除重復字符并排序的字符串,這個程序最終對所有的元素重新做了排序,如果沒有要求排序,則使用前兩種方法。if (pInputStr==NULL||lInputLen<1){return;}string str;copy(pInputStr,pInputStr+lInputLen,back_inserter(str));sort(str.begin(),str.end());str.erase(unique(str.begin(),str.end()),str.end());strcpy(pOutputStr,str.c_str());}
//
//void main()
//{
// char *pInputStr="deefd";
// long lInputLen=strlen(pInputStr);
// char *pOutputStr=new char[lInputLen+1];
// stringFilter(pInputStr,lInputLen,pOutputStr);
// cout<<"result is"<<endl;
// cout<<pOutputStr<<endl;
// delete[] pOutputStr;
//}void main()
{char *pInputStr="abacacde";long lInputLen=strlen(pInputStr);char *pOutputStr=new char[lInputLen+1];stringFilter3(pInputStr,lInputLen,pOutputStr);cout<<"result is"<<endl;cout<<pOutputStr<<endl;delete[] pOutputStr;
}