網站首頁 編程語言 正文
1.題目內容:
孿生素數是指間隔為 2 的相鄰素數,例如最小的孿生素數對是3和5,5和7也是(5雖重復但算作2組)。
2.輸入格式:
輸入N,找出2至N之間的孿生素數的組數。
這里要注意輸入的N不要超過int表示的最大范圍了
3輸出格式:
輸出2至N之間的孿生素數,然后輸出組數。
代碼如下:
#include<stdio.h> int Function(int n)//函數判斷是否為素數 { ? ? for (int i=2; i<n; i++) ? ? { ? ? ? ? if ( n%i == 0 ) ? ? ? ? ? ? return 0; ? ? } ? ? return 1; } int main(void) { ? ? int N; ? ? int count=0;//count存放組數 ? ? scanf("%d",&N); ? ? for(int i=2; i<=N; i++) ? ? { ? ? ? ? if(Function(i)==1 && Function(i+2)==1)//判斷i和i+2是否同為孿生素數 ? ? ? ? { ? ? ? ? ? ? printf("%5d 和 %5d 是孿生素數\n",i,i+2); ? ? ? ? ? ? count++;//組數+1 ? ? ? ? } ? ? } ? ? printf("\n總共有%d組孿生素數",count); ? ? return 0; }
原文鏈接:https://blog.csdn.net/weixin_49125123/article/details/109010054
相關推薦
- 2022-07-16 windows安全加固--關閉非必要端口
- 2022-04-30 DataGridView清除顯示的數據、設定右鍵菜單_C#教程
- 2023-03-13 Android?Hilt依賴注入的使用講解_Android
- 2022-11-16 python多維列表總是只轉為一維數組問題解決_python
- 2022-08-02 淺談Redis常見延遲問題定位與分析_Redis
- 2022-07-27 Python中range函數的使用方法_python
- 2022-02-07 SecureCRT連Linux服務器,提示The remote system refused the
- 2023-05-24 golang?recover函數使用中的一些坑解析_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支