天天看點

C語言(二十):習題練習-求1898

作者:LearningYard學苑
C語言(二十):習題練習-求1898

分享興趣,傳播快樂,

增長見聞,留下美好!

親愛的您,這裡是LearningYard新學苑。

今天小編為大家帶來的是

C語言(二十):習題練習-求1898。

Sharing interests,

Spread happiness,

Growth insights,

Leave behind the beauty!

Dear you,

This is the LearningYard New School.

Today, the editor brings you C language (20):

Exercise Exercise - Find 1898.

一、思維導圖

此推文關于習題練習系列的内容主要如下:

The main content of this tweet about the exercise series is as follows:

C語言(二十):習題練習-求1898

通過一些列習題的練習,進行思維和能力上的提升。

By practicing a series of exercises, improve your thinking and abilities.

二、解題思路

題目:

Title:

C語言(二十):習題練習-求1898

首先我們需要判定2000以内的素數有哪些,其次第二排的數字連續相加等于第二行第一個數字左肩上的數字減去第二行最後一個數右肩上的數字,是以對于題目之和為1898可以用第一行首尾數相減。本道題的難點就在于思維的轉換和數組的調用。

Firstly, we need to determine which prime numbers are within 2000. Secondly, the continuous addition of the numbers in the second row is equal to subtracting the number on the left shoulder of the first number in the second row from the number on the right shoulder of the last number in the second row. Therefore, for the sum of questions of 1898, the first and last numbers in the first row can be subtracted. The difficulty of this question lies in the transformation of thinking and the invocation of arrays.

三、代碼分析

1、定義函數,判斷兩千以内的素數

1. Define a function to determine prime numbers within two thousand

C語言(二十):習題練習-求1898

2、将素數存入數組内,再将數組内的數從最後減去最前的數,看是否相差1898

2. Store prime numbers in an array, then subtract the first number from the last to see if there is a difference of 1898

C語言(二十):習題練習-求1898

四、解題代碼

#include <stdio.h>

#include <math.h>

#define NUM 320

int number[NUM];

int fflag(int i);

int main()

{

int i, j, count = 0;

printf("There are follwing primes in first row :\n");

for (j = 0, i = 3; i <= 1999; i += 2)

if (fflag(i)) number[j++] = i;

for (j--; number[j] > 1898; j--)

{

for (i = 0; number[j] - number[i] > 1898; i++);

if (number[j] - number[i] == 1898)

printf ("(%d).%3d,......,%d\n", ++count, number[i], number[j]);

}

return 0;

}

int fflag(int i)

{

int j;

if (i <= 1) return 0;

if (i == 2) return 1;

if (!(i % 2)) return 0;

for (j = 3; j <= (int)( sqrt((double)i) + 1); j += 2)

if (i % j == 0) return 0;

return 1;

}

結語:C語言的學習需要配合不斷地練習,對于推文有什麼需要改進的地方,代碼錯誤的地方還請大家提出來。

Conclusion: The editor thinks that the pointer part is difficult to understand, so if there is any problem in the article, please ask everyone to raise it.

今天的分享就到這裡了。

如果您對今天的文章有獨特的想法,

歡迎給我們留言,

讓我們相約明天,

祝您今天過得開心快樂!

That's all for today's sharing.

If you have unique ideas about today's article,

please leave us a message.

Let's meet tomorrow.

I wish you a happy day!

翻譯:百度翻譯

參考:《零基礎入門學習C語言:帶你學C帶你飛》、哔哩哔哩小甲魚視訊、網絡圖檔

聲明:本文由LearningYard新學苑原創,若有侵權請聯系删除

文案&排版:易春秀

稽核:闫慶紅