程序设计编程练习题解题报告(第6周)

《程序设计方法(C\C++)》是北京理工大学的一门课程,该课通过北京理工大学网络教室评测的编程练习题计算平时成绩。

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

Posted

in

, ,

by

Tags:

Comments

3 responses to “程序设计编程练习题解题报告(第6周)”

  1. 卡,卡 Avatar

    弱弱问一句:博主,你博客的模板这样设计pv高吗?

    1. George J. SUN Avatar

      如何设计pv高?还望赐教。

Leave a Reply to 卡,卡 Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.