C语言2011年3月 实验题
来源:互联网 发布:网络陈世美啥意思 编辑:程序博客网 时间:2024/05/19 06:37
Write a program that finds all triples of consecutive positive three-digit integers each of which is
the sum of two squares.
Hint: As we are not constrained by memory space for this problem, and as we have some experience
with integers but not with floating point numbers, we might declare an array that can store an int
for all numbers n in the range [100, 999], equal to 1 in case n is the sum of two squares, and to 0
otherwise.
Do not forget to use #define to give evocative names to special values (in particular, the minimum
and maximum values that a variable can take in a loop).
solution:
#include <stdio.h>
#include <stdlib.h>
/* The largest number whose square is a 3-digit number */
#define MAX 31
int nb_of_consecutive_squares(int);
/* For all n in [100, 999], if n is found to be of the form a^2 + b^2
* then sums_of_two_squares[n] will be set to a + b * 100,
* so a and b can be easily retrieved.
* Note that there might be other decompositions of n into a sum of 2 squares;
* we just recall the first decomposition we find.
* Also note that we waste the 100 first elements of the array;
* we can afford it and this choice makes the program simpler. */
int sums_of_two_squares[1000];
int main(void) {
for (int i = 0; i <= MAX; ++i)
for (int j = i; j <= MAX; ++j) {
int n = i * i + j * j;
if (n < 100)
continue;
if (n > 1000)
break;
sums_of_two_squares[n] = i + j * 100;//这条语句只是给这 下标为n的数组提供一个值而已 没有什么实际意义 相当于做个一个记号
}
for (int n = 100; n < 1000; ++n) {
int i = nb_of_consecutive_squares(n);
if (i < 3) {
/* There is no potential triple before n + i + 1; the loop will increment n by 1. */
n += i;//这句话加快了执行速度 如果去掉不影响程序进行
continue;
}
printf("(%d, %d, %d) "
"(equal to (%d^2+%d^2, %d^2+%d^2, %d^2+%d^2)) is a solution./n",
n, n + 1, n + 2,
sums_of_two_squares[n] % 100, sums_of_two_squares[n] / 100,
sums_of_two_squares[n + 1] % 100, sums_of_two_squares[n + 1] / 100,
sums_of_two_squares[n + 2] % 100, sums_of_two_squares[n + 2] / 100);
/* We assume we could have two solutions of the form
* (n, n + 1, n + 2) and (n + 1, n + 2, n + 3)
* (but as the solution shows, this never happens...).
* We could avoid checking that sums_of_two_squares[n + 1] and
* sums_of_two_squares[n + 2] are not equal to 0, but why make the program
* more complicated for no significant gain? */
}
return EXIT_SUCCESS;
}
int nb_of_consecutive_squares(int n) {
if (sums_of_two_squares[n] == 0)//如果等于零则表明 在下标为n的数组元素中不存在值
return 0;
if (sums_of_two_squares[++n] == 0)//n的下标存在值 但是n+1的元素不存在值
return 1;
if (sums_of_two_squares[++n] == 0)//n 和 n+1的下表存在值 但是n+2的下标不存在值
return 2;
return 3;
}
附上的代码怎么都没有颜色提示 算里 就那样把
- C语言2011年3月 实验题
- 2011年3月计算机二级C语言真题【附答案】
- 2012年3月20日C语言学习笔记
- C语言实验题-倒三角形
- CSP考试 2013年12月第3题 最大矩形 C语言实现
- CSP考试 2015年09月第3题 C语言实现
- CSP考试 2015年03月第3题 节日 C语言实现
- CSP考试 2014年09月第3题 字符串匹配 C语言实现
- 2010年3月计算机二级C语言真题【附答案】
- 2010年3月计算机二级C语言真题答案
- c语言实验
- C语言 枚举实验
- c语言实验
- C语言递归实验
- C语言实验-素数
- C语言枚举实验
- 2011年11月编程语言排行榜:Objective-C有望成为2011年年度编程语言。
- 2011年7月编程语言排行榜,Objective-C将成为年度语言
- string跨程序域驻留
- 转minicom移植ARM9
- 读静态页面传的参数
- JNDI 是什么
- Python 读取键盘输入字符,多线程操作,文件操作随机处理 开发范例
- C语言2011年3月 实验题
- 相对轻松
- exec
- 如何学好C语言
- FireBug实用指南
- Ajax 使用(json)
- 如何学好C++语言
- 在转华为EC122在HiSi3110E上移植
- 程序员是这样炼成的(9)- 每天有目标的编程