////Problem A: 简单的整数排序
Description
对给出的若干整数按从小到大排序。 Input
输入的第一个数为n(n<=1000),后接n个整数。 Output
按从小到大的顺序输出这些整数,每两个整数之间用一个空格分隔开,最后一个整数后面没有空格。 Sample Input
10 3 9 1 5 2 8 5 6 7 3
Sample Output
1 2 3 3 5 5 6 7 8 9
#include
int i,j,n,t; int a[1000]; scanf(\ for(i=0;i scanf(\ for (i=1;i for(j=0;j {t=a[j]; a[j]=a[j+1];a[j+1]=t; } for(i=0;i printf(\ else printf(\ } } /////Problem B: 兔子的繁殖问题 Description 假设一对兔子每月能生一对小兔(一雌一雄),每对小兔出生后的下一个月是没有繁殖能力的,至出生后的第三个月开始又可以每月生一队小兔,问从一对刚出生的小兔开始,经过若干个月后一共有多少兔子(假设在此过程中兔子没有死亡)? Input 输入的第一个数为n,接下来有n个数字。每个数字为一个月份m(m<=45)。 Output 输出为n行,每行为第m个月后的兔子总数。 Sample Input 6 1 2 3 4 5 10 Sample Output 1 2 3 5 8 89 HINT 当n较大时,菲波那契序列的第n项值和计算量都是很大的,可以先计算出菲波那契序列并用数组存储下来,然后查询出每月兔子数,避免重复运算。 #include { int i,j,k,n; int a[50]={0,1,2},b[50]; for(i=3;i<=45;i++) a[i]=a[i-1]+a[i-2]; scanf(\ for(k=0;k scanf(\ for(k=0;k printf(\} //////Problem C: 产生等差序列之一 Description 根据给出的初始数、公差和序列长度求等差序列。 Input 输入为一行,格式见sample。其中,start为初始数,step为公差,times为序列长度。满足,times>0,step不为0。 Output 把这个等差序列输出在一行里,序列两数之间用一个空格分隔。 Sample Input start = 1, step = 2, times = 100 Sample Output 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199 HINT Append Code #include int i,j,k,n; char d[100],b[100],c[100]; int a[1000]; scanf(\ for(n=0;n a[n]=i+j*n; } for(n=0;n if(n==k-1) printf(\ else printf(\ } } ////Problem D: 产生等差序列之二 Description 根据给出的初始数、公差和终止条件求等差序列。 Input 输入为一行,格式见sample。其中,start为初始数,step为公差,end为终止条件。满足,step不为0,并且start和end的大小关系与step的方向一致。end不一定是序列的最后一个数。 Output 把这个等差序列输出在一行里,序列两数之间用一个空格分隔。 Sample Input start = 1, step = 2, end = 200 Sample Output 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199 #include int i,j,k,b,n; int a[1000]; scanf(\ a[0]=i; if(j>=0) for(n=0;a[n]<=k;n++) a[n+1]=i+(n+1)*j; else for(n=0;a[n]>=k;n++) a[n+1]=i+(n+1)*j; for(b=0;b printf(\ else printf (\} ////Problem A: 字符串的逆序 Sample Input abcde Sample Output edcba #include {int i,j; char s[101]; scanf(\ i=strlen(s); for(j=i-1;j>=0;j--)printf(\ Problem B: 去行首行尾的空白符 Description 在C语言中,将ASCII字符集中的制表符('\\t')、回车符('\\r')、换行符('\\n')、垂直制表符('\\v')、换页符('\\f')和空格字符(' ')称作空白符。 你的任务是读入每行字符串,去掉行首和行尾的连续空白符,但是在任意非空白符中间的空白符不要去除。 Input 输入为多行,每行为一个串(不超过100个字符),至某行输入的非空白符仅为“END”结束。 Output输出为多行,为每行输入的去掉前后空白符的串。“END”也输出。 #include int i,j=0,k,m,n,a; char s[1224][123]={{0},{0}},b[1234]={0}; for(i=1;strcmp(s[i-1]+j,\ { gets(s[i]); for(j=0;isspace(s[i][j])!=0;j++); for(n=strlen(s[i])-1;isspace(s[i][n])!=0;n--); s[i][n+1]='\\0'; puts(s[i]+j); } } /////Problem C: 回文 Input 输入为多行,到文件末尾结束。每行为一个串,且不会超过1000个字符,且全部由可显示的ASCII码字符组成。Output当一个串中的字母和数字部分能够构成一个回文,即输出“Yes.”;否则输出“No.”。 HINT 首先要考虑如何去除空白符(空格、回车、换行、制表符等),标点和各种符号(如“,!\和“#@<>{}”等),并且把串中的英文字符统一大小写,最后才能进行回文判定。 请注意,用gets()和scanf()判断文件尾的方法是不一样的。gets()函数的返回值请查阅C语言的语法手册。 #include int i,j,k; char a[1001],b[1001]; while(gets(a)!=NULL) { for(i=0,j=0;a[i]!='\\0';i++) { if(isalnum(a[i])!=0) { b[j]=a[i]; j++; } } for(i=0,k=0;i if(tolower(b[i])==tolower(b[j-1-i])) k++; } if(k==ceil(j/2.0)) printf(\ else printf(\ } } Problem D: Matrix Problem : Array Pratice Description 求一个m×n阶矩阵A的转置矩阵AT。矩阵A的每个元素都在int类型的范围之内。 Input 输入的第一行为一个整数M(M>0),后面有M组输入数据。每组数据以两个正整数m和n开始,满足0 输出为多组,每组输出A的转置矩阵AT。矩阵的输出为:每行两个元素之间用一个空格分开,每行最后一个元素之后为一个换行,在下一行开始输出矩阵的下一行。每两组输出之间用一个空行分隔开。 Sample Input 1 3 3 1 2 3 4 5 6