前言
又到了混合课堂的时间,今天布置了一道关于循环和控制语句的题目,比上次的题目更加简便了,接下来就一起来看看吧。
同上次,代码部分在截止后一天公布。
介绍
-
2.1) Define a function
sum_while
, which accept an integer parametern
and returns an integer with the value sum of1+2+...n
. Ifn
is negative or 0, return 0. This function uses awhile
statement to compute the sum. The type of the parameter and the return value can be any proper integer type which you can decide. -
2.2) Define a function
sum_for
, which is the same assum_while
, but the sum value is computed using afor
statement. -
2.3) Define a function
sum_do_while
, which is the same assum_while
, but the sum value is computed using ado-while
statement. -
2.4) Define a function
is_prime
, which accept an integern
as the parameter, returns 1 ifn
is a prime number (such as 2, 3, 5, 7, 11, ...), otherwise, returns 0. If can decide if 1 is a prime number or not. -
2.5 Put the prototypes of the above 4 functions somewhere above the
main
function.
简单来说就是定义四个函数,前三个用不同的循环方式累加,最后一个是判断质数,具体请看:https://files.hoyue.fun/vfm/download/0/sh/9a4598b5c641819ab3fb875e34285eef/share/38418729afa640c5ad008fd2ef6dd348
注意点
do...while循环的执行顺序
在sum_do_while循环里,我们应该先加合,再自增。不然可能会使结果少算或多算。因为在do...while循环中是先执行再判断的。
所以这一段我们应该像这样处理:
int i=1,sum=0;
do
{
sum+=i;
i++;
} while (i<=n);
return sum;
判断质数
这个是C语言的经典题目了。我们判断质数就是看从2到它本身有没有能被其整除的数,如果没有就是质数。但是每次判断质数都要从头比到尾,我们可以采用更加效率的方法。我们看例如15,√15<4,当我们在4之后时一定找不到一个数能被其整除。那么我们就可以优化我们的算法。
只需从2到√n之间找到一个数能被n整除,那么n就不是质数。
优化:
for(int i=2;i<=sqrt(n);i++)//If a number n is between 2 and the sqrt n, n%i!=0, the number is prime.
if(n%i==0)
return 0;//if a number is found, return 0
return 1;
在这里是如果发现一个能整除就直接返回不是质数了,如果也一直没有,if语句就一直不正确,最后就会return1.
当然我们还可以继续优化,毕竟当i是质(素)数的时候,i的所有的倍数必然是合数。
在这里就不写了。
代码
以下为代码部分,将在截止后公布。
/*
Name: ;
Class: ;
Student ID: ;
Homework: #3 Control;
*/
#include
#include //use the function sqrt
#include//use the const bool
char name[81]; //save your name
int sum_while(int n); /* task 2.1 */
int sum_for(int n); /* task 2.2 */
int sum_do_while(int n); /* task 2.3 */
bool is_prime(int n); /* task 2.4 */
int main()
{
printf("Good day! Could you tell me your name?\n");
scanf("%s",name);
printf("Ok %s. Could you input a positive integer. Anything else will be error. \n",name);
int n=0;//Initialize to 0
while(scanf("%d",&n)) //task 3.1
{
/* task 3.2 */
if(n<=0)
{
printf("Ooop! It's might be something error! Please reinput it!\n");
continue;//if input number is not a positive integer, continue to input it.
}
else
{
/* task 3.3 to task 3.6 */
printf("Thank you %s. Could you help me check the answer below?\n",name);
printf("sum_while(%d)=%d\n",n,sum_while(n));
printf("sum_for(%d)=%d\n",n,sum_for(n));
printf("sum_do_while(%d)=%d\n",n,sum_do_while(n));
if(is_prime(n))
printf("%d is a prime number.\n",n);
else
printf("%d is not a prime number\n",n);
printf("Thank you %s. Have a good day!",name);
return 0;//EOF
}
}
/* task 3.1 */
printf("Ooop %s. Your input is not a number! Have a nice day!",name);//if input not a number, shutdown the program.
return 0;
}
int sum_while(int n) /* task 2.1 */
{
if(n<=0) return 0;
int i=1,sum=0;
while(i<=n)
{
sum+=i;
i++;
}
return sum;
}
int sum_for(int n) /* task 2.2 */
{
if(n<=0) return 0;
int sum=0;
for(int i=1;i<=n;i++)
{
sum+=i;
}
return sum;
}
int sum_do_while(int n) /* task 2.3 */
{
if(n<=0) return 0;
int i=1,sum=0;
do
{
sum+=i;
i++;
} while (i<=n);
return sum;
}
bool is_prime(int n) /* task 2.4 */
{
if(n<=1) return 0;
for(int i=2;i<=sqrt(n);i++)//If a number n is between 2 and the sqrt n, n%i!=0, the number is prime.
if(n%i==0)
return 0;//if a number is found, return 0
return 1;//if not find, the number is a prime
}
Comments NOTHING