首页 > 技术文章 > C语言:对长度为7的字符串,除首尾字符外,将其余5个字符按ASCII降序排序。-计算并输出3~n之间所有素数的平方根之和。

ming-4 2019-03-25 11:39 原文

//对长度为7的字符串,除首尾字符外,将其余5个字符按ASCII降序排序。

 1 #include <stdio.h>
 2 #include <ctype.h>
 3 #include <string.h>
 4 
 5 void fun(char *s,int num)
 6 {
 7     int t,i,j,k;//使用指针
 8     s++;
 9     for ( i = 1; i < num-1; i++)
10     {
11         k = 1;
12         for (j = i+1; j < num - 1; j++)
13         {
14             if (*s < *(s + k))
15             {
16                 t = *s;
17                 *s = *(s + k);
18                 *(s + k) = t;
19             }
20             k++;
21         }
22         s++;
23     }
24     /*int m = s[1],t;//使用数组
25     for (int i = 1; i < num - 1; i++)
26     {
27         for (int j = i + 1; j < num - 1; j++)
28         {
29             if (s[i] < s[j])
30             {
31                 t = s[i];
32                 s[i] = s[j];
33                 s[j] = t;
34             }
35         }
36     }*/
37 }
38 
39 void main()
40 {
41   void NONO( );
42   char s[10];
43   printf("输入7个字符的字符串:");
44   gets(s);
45   fun(s,7);
46   printf("\n%s",s);
47   NONO();
48 }
49 void NONO()
50 {
51 /* 请在此函数内打开文件,输入测试数据,调用 fun 函数,
52    输出数据,关闭文件。 */
53    char s[10] ;
54    FILE *rf, *wf ;
55    int i = 0 ;
56 
57   rf = fopen("in.dat","r") ;
58   wf = fopen("out.dat","w") ;
59    while(i < 10) {
60      fgets(s,10,rf) ;
61      s[7] = 0 ;
62      fun(s,7);
63      fprintf(wf, "%s\n", s) ;
64      i++ ;
65    }
66    fclose(rf) ;
67    fclose(wf) ;
68 }

 //计算并输出3~n之间所有素数的平方根之和。

 1 #include  <stdio.h>
 2 #include  <math.h>
 3 double fun(int n)  
 4 {
 5     int a = 0,b=0;
 6     int s[100];
 7     double  sum = 0.0;
 8     for (int i = 3; i <= 100; i++)
 9     {
10         for (int j = 2; j < i; j++)
11         {
12             if (i%j == 0) { a = 1; break; }
13         }
14         if (a == 0)
15         {
16             s[b++] = i;
17             //printf("%d\n", i);
18         }
19         a = 0;
20     }
21     s[b] = '\0';
22     for (int c = 0; c < b; c++)
23     {
24         sum += sqrt(s[c]);
25     }
26     return sum;
27 }
28 void main()
29 {int n;
30  double sum;
31  FILE *out;
32  printf("Input  N=");
33  scanf("%d",&n);
34  sum=fun(n);
35  printf("\n\nsum=%f\n\n",sum);
36  /******************************/
37  out=fopen("out.dat","w");
38  fprintf(out,"%f\n",fun(180));//着重注意
39  fclose(out);
40  /******************************/
41 }

 

推荐阅读