先看下我的運行結果吧,會自動判斷輸入的m、n是否合法哦!
上代碼,註釋只寫了部分有助理解的。
#include <stdio.h>
int Found(int m, int n, int a[])
{
int i = 0; int Count = 0; int offset = 0;?//數組a的下標 int qian, bai, shi, ge;?//千位、百位、十位、個位的值 for(i=m; i<=n; i++) { //取出千位、百位、十位、個位的值 qian = i/1000; bai = (i%1000)/100; shi = (i%100)/10; ge = i%10; if((qian<= bai && bai<=shi && shi<=ge) && (qian+ge == bai+shi))?//找到壹個符合條件的數 {Count++;
a[offset++] = i;
} } return Count;}
int main()
{
int m = 0; int n = 0; int nResult[100] = {0}; int Count = 0; int i = 0; FILE* pFile = NULL; char szInfo[100] = {0}; while(1) { printf("Please enter m and n:"); scanf("%d %d", &m, &n); printf("\n"); if(m>=1000 && n<=1200 && m<n)break;
elseprintf("輸入數據不合法,請重新輸入\n");
} Count = Found(m, n, nResult); pFile = fopen("104.out", "w"); if(NULL == pFile) printf("打開文件失敗\n"); sprintf(szInfo, "found %d numbers in %d ~ %d:\n", Count, m, n); printf("%s", szInfo); if(NULL != pFile) { fwrite("No. 104\n", 1, strlen("No. 104\n"), pFile);?//?寫入學號 fwrite(szInfo, 1, strlen(szInfo), pFile);//寫入部分信息 } for(i=0; i<Count; i++) { printf("%d? ", nResult[i]); sprintf(szInfo, "%d? ", nResult[i]); if(NULL != pFile)?//輸出到屏幕的同時,順便把該值寫進文件fwrite(szInfo, 1, strlen(szInfo), pFile);
} printf("\n"); if(NULL != pFile) fclose(pFile); return 0;}
自從工作以來,兩年沒上過百度知道了,當初上大學的時候天天在百度知道問問題,今偶爾閑娛,幫忙解答,想不到這裏還是這麽熱鬧,不禁想起了當年為壹個鏈表、壹個函數挑燈夜戰的場景。只能嘆時間太快,想不到都過去好幾年了。做程序最需要毅力,小師弟,好好努力吧,嘿嘿,下班回家咯!