18 删除重复字符
核心思想:从前往后扫描已知字符串,对于每一个字符再在其以前寻找是否存在重复。如果不存在,则将此字符输出,否则不输出。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str[101];
int i,j;
gets(str);
for (i=0;i<strlen(str);i++) {
for (j=0;j<i;j++)
if (str[i]==str[j])
break;
if (i==j)
printf("%c",str[i]);
}
if (strlen(str))
printf("\n");
//system("pause");
return 0;
}
19 单词排序
核心思想:冒泡排序,判断的时候只用字符串比较函数strcmp(str1,str2).
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str[5][256];
int t[5]={0,1,2,3,4};
int i,j,k;
for (i=0;i<5;i++)
gets(str[i]);
for (i=0;i<5;i++)
for (j=i;j<5;j++)
if (strcmp(str[t[i]],str[t[j]])<0) {
k=t[i];
t[i]=t[j];
t[j]=k;
}
for (i=0;i<5;i++)
printf("%s\n",str[t[i]]);
//system("pause");
return 0;
}
20 大数分解
核心思想:2开始,从小到大测试是否能够整除已知数,如果能够整除则下一次还试此数,否则换下一个数。如果测试的因子不是质因子,则其质因子一定比其小,因此一定在前面就没测试出来,故测试出来的所有因子都是质因子。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i,j,k;
int n;
scanf("%d",&n);
i=2;
while(n!=1) {
if (n%i)
i++;
else {
printf("%d\n",i);
n/=i;
}
}
//system("pause");
return 0;
}
Leave a Reply to 卡,卡 Cancel reply
You must be logged in to post a comment.