//函數fun功能:求n(n<10000)以內的所有四葉玫瑰數並逐個存放到result所指數組中,個數作為返回值。如果一個4位整數等於其各個位數字的4次方之和,則稱該數為函數返回值。
1 #include<stdio.h> 2 #pragma warning (disable:4996) 3 int fun(int n, int result[]) 4 { 5 int sum=0,j=0; 6 int a, b, c, d; 7 for (int i = 1000; i < n; i++) 8 { 9 a = i / 1000; 10 b = (i % 1000) / 100; 11 c = (i % 100) / 10; 12 d = i % 10; 13 if ((a*a*a*a + b * b*b*b + c * c*c*c + d * d*d*d) == i) 14 { 15 result[j++] = i; 16 } 17 } 18 return j; 19 } 20 main( ) 21 { 22 int result[10], n, i; 23 void NONO(int result[], int n); 24 n = fun(9999, result); 25 for(i=0; i<n; i++) printf("%d\n", result[i]); 26 NONO(result, n); 27 } 28 29 void NONO(int result[], int n) 30 {/* 本函數用於打開文件,輸入數據,調用函數,輸出數據,關閉文件。 */ 31 FILE *fp ; 32 int i; 33 34 fp = fopen("out.dat","w") ; 35 fprintf(fp, "%d\n", n); 36 for(i=0; i<n; i++) fprintf(fp, "%d\n", result[i]); 37 fclose(fp); 38 }
//函數fun將字符串s1和s2合並形成新的字符串s3,先取出1的第一個字符放入3,再取出2的第一個字符放入3,中,以此類推,若1和2的長度不等,較長的字符順序放在新生成的3后。
1 #include <stdio.h> 2 #include <string.h> 3 #pragma warning (disable:4996) 4 void fun( char *s1, char *s2, char *s3) 5 { int i,j; 6 /**********************found***********************/ 7 for(i = 0, j = 0; (s1[i] != '\0') && (s2[i] != '\0'); i++, j = j + 2) //j自增2 8 { s3[j] = s1[i]; 9 s3[j+1] = s2[i]; 10 } 11 if (s2[i] != '\0') 12 { for(; s2[i] != '\0'; i++, j++) 13 /**********************found***********************/ 14 s3[j] = s2[i]; 15 } 16 else if (s1[i] != '\0') 17 { for(; s1[i] != '\0'; i++, j++) 18 s3[j] = s1[i]; 19 } 20 /**********************found***********************/ 21 s3[j] = '\0'; 22 } 23 void main() 24 { char s1[128], s2[128], s3[255]; 25 printf("Please input string1:"); 26 gets(s1); 27 printf("Please input string2:"); 28 gets(s2); 29 fun(s1,s2,s3); 30 printf("string:%s\n", s3); 31 }