[Swust OJ 610]--吉祥数

本篇介绍了一个趣味编程问题:寻找与吉祥数1898相关的素数序列。通过对不超过2008的所有素数进行分析,利用C++编程实现算法,找到满足条件的素数对,即两个素数的差值等于1898。

 

题目链接:http://acm.swust.edu.cn/problem/610/

 

Time limit(ms): 1000      Memory limit(kb): 65535
 
Description
马上就是新年了,在这里提前祝大家新年快乐,首先送大家一个吉祥数——1898。相信意思大家都明白吧,接着就请大家根据下面的提示来找出一些和我们这个吉祥数相关的一些数吧。 
请将不大于2008的所有素数从小到大排成第一行,第二行上的每个数都等于它头上的素数与它右肩上的素数之差。编程求出:第二行数中是否存在这样的若干个连续的整数,它们的和恰好是1898?假好存在的话,又有几种这样的情况?例如: 
第一行:2 3 5 7 11 13 17......1979 1987 1993 
第二行:1 2 2 4 2 4...... 8 6 
 
Input
没有输入
 
Output
输出有多个结果,输出所有满足条件的结果,要求只输出每个结果对应第1行数的起始数和终止数,两个数间空两格,每个结果间换行,所有的结果按起始数从大到小的顺序输出,具体见Sample
 
 
Sample Input
无输入

Sample Output
101 1999
89 1987
……
……
……

 
解题思路:(1)假设第一行中的素数为n[1]、n[2]、n[3]....n、...第二行中的差值为m[1]、m[2]、m[3]...m[j]...。其中m[j]为:m[j]=n[j+1]-n[j]。
     (2)则第二行连续N个数的和为:
        SUM=m[1]+m[2]+m[3]+...+m[j]
            =(n[2]-n[1])+(n[3]-n[2])+(n[4]-n[3])+...+(n[j+1]-n[j])
            =n[j+1]-n[1]
  由此题目就变成了:在不超过2008的所有素数中是否存在这样两个素数,它们的差恰好是1898。若存在,则第二行中必有所需整数序列,其和恰为1898,。
    对等价问题的求解是比较简单的。
    由分析可知,在素数序列中不必包含2,因为任意素数与2的差一定为奇数,所以不必考虑。(同样素数由素数表打表得到)
 
代码如下:
 
 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 int ptr[520], prime[2015] = { 0, 0, 1 };
 5 void init(){
 6     int i, j;
 7     memset(prime, 1, sizeof(prime));
 8     for (i = 2; i <= 2008; i++){
 9         if (prime[i]){
10             for (j = 2; i*j <= 2008; j++)
11                 prime[i*j] = 0;
12         }
13     }
14 }
15 int main(){
16     int i, j;
17     init();
18     for (j = 0, i = 3; i <= 2008; i += 2)
19     if (prime[i])
20         ptr[j++] = i;
21     for (j--; ptr[j] > 1898; j--){
22         for (i = 0; ptr[j] - ptr[i] > 1898; i++);
23         if (ptr[j] - ptr[i] == 1898)
24             cout << ptr[i] << "  " << ptr[j] << endl;
25     }
26     return 0;
27 }
View Code

 

转载于:https://www.cnblogs.com/zyxStar/p/4584327.html

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
### SWUST OJ Problem 32 Information and Solution Unfortunately, specific details about SWUST OJ problem number 32 are not directly provided in the available references. However, based on similar problems from this platform such as those mentioned in other citations, a general approach to solving typical programming challenges can be outlined. #### Understanding Common Elements of Programming Problems on SWUST OJ Platform Problems like SWUSTOJ276, SWUSTOJ77, SWUSTOJ78, SWUSTOJ1286, and SWUSTOJ1285 emphasize proper use of `if` and `else` statements along with maintaining good coding practices including appropriate formatting[^1]. For instance, when dealing with numerical outputs, `%g` is used for automatic selection between fixed-point notation (`%f`) or scientific notation (`%e`), depending on which provides more compact output without loss of precision. Given that detailed specifics regarding problem 32 aren't present here, one should look at common patterns found across different types of questions posed by platforms like these: - **Input Handling**: Typically involves reading inputs either single values or arrays/lists. - **Logic Implementation**: Applying algorithms ranging from simple arithmetic operations up through complex data structures manipulation. - **Output Formatting**: Ensuring results adhere strictly to specified formats using placeholders like `%d`, `%s`, etc., where applicable. Since no direct reference exists specifically addressing SWUST OJ problem 32 within given sources, consider exploring adjacent numbered problems around it for clues about its nature—whether mathematical computation, string processing, dynamic programming elements, et cetera—and adapt solutions accordingly while keeping best practice guidelines intact. ```c // Example C code snippet demonstrating basic structure often seen in contest-style programs #include <stdio.h> int main() { int n; scanf("%d", &n); // Read input value if (condition_based_on_problem_statement) { printf("Result under condition A\n"); } else { printf("Alternative result\n"); } return 0; } ``` --related questions-- 1. How does understanding how `%g` works help improve program efficiency? 2. What strategies could apply towards optimizing performance in competitive programming contests? 3. Can you provide examples illustrating effective usage of conditional operators (`if`, `else`) in algorithm design?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值