1. 程式人生 > >C語言:對長度為7的字符串,除首尾字符外,將其余5個字符按ASCII降序排序。-計算並輸出3~n之間所有素數的平方根之和。

C語言:對長度為7的字符串,除首尾字符外,將其余5個字符按ASCII降序排序。-計算並輸出3~n之間所有素數的平方根之和。

file print font 素數 open stdio.h sca math while

//對長度為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 }

C語言:對長度為7的字符串,除首尾字符外,將其余5個字符按ASCII降序排序。-計算並輸出3~n之間所有素數的平方根之和。