6. 統計出現最大次數的數字,輸出該數字以及該數字出現的次數
輸入:323324423343 輸出:3,6
#include<stdio.h>
#include<string.h>
int main(void)
{
char pInputStr[20];
int i,j,StrLength,max_times=0,max_number=0;
int pInputInt[20];
int pFrequents[10]={0};
printf("Input the number sequences:\n");
gets(pInputStr);
StrLength=strlen(pInputStr);
for(i=0;i<StrLength;++i)
{
pInputInt[i]=pInputStr[i]-48; //將對應的數字字符串轉換成數字
for(j=0;j<=9;++j)
{
if(pInputInt[i]==j)
++pFrequents[j]; //統計數字j對應出現的次數,並0-9順序依次存入數組中
}
}
for(k=0;k<=9;++k)
{
max_times=(pFrequents[k]>max_times)?pFrequents[k]:max_times; //遍歷輸出數組中最大值,最大值爲出現次數最大值
}
for(k=0;k<=9;++k)
{
if(max_times==pFrequents[k])
max_number=k; //返回最大次數對應的數字
}
printf("the max times number: %d, and the times: %d",max_number,max_times);
return 0;
}
7.字符串過濾
輸入:abbadce 輸出:abdce
#include<stdio.h>
#include<string.h>
void StrFilter(char *pInputStr, int StrLength, char *pOutputStr)
{
int i,j=0;
bool hash[26]={0};
for(i=0;i<StrLength;)
{
if(hash[pInputStr[i]-'a']==false) //如果後面出現不同的字符
{
hash[pInputStr[i]-'a']=true;
pOutputStr[j++]=pInputStr[i++];
}else
++i;
}
pOutputStr[j]='\0';
}
int main(void)
{
char pInputStr[20],pOutputStr[20];
int StrLength;
printf("Input the string:\n");
gets(pInputStr);
StrLength=strlen(pInputStr);
StrFilter(pInputStr,StrLength,pOutputStr);
puts(pOutputStr);
return 0;
}
9. 將整數倒序輸出,剔除重複數據
輸入:12336544 輸出:456321 如果最後是0,則不輸出,輸入:1750 輸出:571 如果是負數,比如輸入:-175 輸出:-571
#include<stdio.h>
#include<string.h>
void NumberReverse(char *pInputStr, int StrLength, char *pOutputStr)
{
int i,j=0,count;
bool hash[10]={0};
for(i=StrLength-1;i>=0;)
{
if(pInputStr[StrLength-1]=='0') //如果最後是0
{
--i;
--StrLength; //不減1的話,重複執行此句會出錯
}
if(hash[pInputStr[i]-'1']==false) //如果前面沒有重複的數字
{
hash[pInputStr[i]-'1']=true;
pOutputStr[j++]=pInputStr[i--];
}else
--i;
}
if(pInputStr[0]=='-') //如果輸入負號
{
for(count=j;count>=1;--count)
{
pOutputStr[count]=pOutputStr[count-1]; //向後移位,空出第0位
}
pOutputStr[0]='-';
++j; //之前判斷負號時,沒++j
}
pOutputStr[j]='\0';
}
int main(void)
{
char pInputStr[20],pOutputStr[20];
int StrLength;
printf("Input the number sequences:\n");
gets(pInputStr);
StrLength=strlen(pInputStr);
NumberReverse(pInputStr,StrLength,pOutputStr);
puts(pOutputStr);
return 0;
}