C++ Work 8

《C++程序与设计》WORK EIGHT

T 1

//写一个判断素数的函数prime。
//在主函数中输入一个整数n,输出是否为素数的信息。
//思路 :使  n 除以 2 ~ 根号 n 中所有元素 , 若能整除 , 则为 非 ; 否则 , 则为 是 。

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
    bool prime (int n);
    int n = 0;
    int a = 0;
    while(cin >> n)
    {
    a = prime (n);
    if (a == 1)
    {
        cout << "素数" << endl;
    }
    else if (a == 0)
    {
        cout << "不是素数" << endl;
    }
    else
    {
        cout << "ERROR" << endl;
    }
    }
    system ("pause");
}
bool prime (int n)
{
   for (int i = 2 ; ; )
   {
       while (n % i != 0)
       {
           if (i <= sqrt(n))
           {
               i += 1;                       //   while 循环中,退出 循环时 i 再自增一次
           }
           else 
           {
               break;
           }
       }
       if (i == (int (sqrt(n)) + 1))               // 此处强制转换类型
       {
           return true;
       }
       else
       {
           return false;
       }
       
   }
}

T 2

//写一个函数验证哥德巴赫猜想:一个不小于6的偶数可以表示为两个素数之和,
//如6 = 3 + 3, 8 = 3 + 5, 10 = 3 + 7,...,
//在主函数中输入一个不小于6的偶数n,然后调用函数gotbaha,
//在gotbaha函数中再调用prime函数,prime函数的作用是判别一个数是否为素数。
//在gotbaha函数中输出以下形式的结果:34 = 3 + 31

#include <iostream>
using namespace std;
int main ()
{
    int gotbaha (int n);
    int prime (int n);
    int n = 0;
    cin >> n;
    if (n > 6 && (n % 2) == 0)
    {
        if (gotbaha(n) == -1)
        {
            cout << "ERROR" << endl;
        }
        else
        {
            cout << "3 + " << gotbaha (n) << endl;
        }
        
    }
    else
    {
        cout << "请输入一个大于 6 的偶数";
    }
    system ("pause");
}
int gotbaha (int n)
{
    int prime (int n);
    n = n - 3;
    if (prime(n) == 1)
    {
        return n;
    }
    else
    {
        return -1;
    }  
}
int prime (int n)
{
	for (int i = 2; i <= (n / 2); )
	{
		while ((n % i) != 0)
		{
			i++;
		}
		if (i = (n / 2))
		{
			return 1;
			break;
		}
		else                                
		{
			return -1;
			break;
		}
	}
}

T 3

//定义函数f(x)计算(x + 1)2,函数返回值类型是double。编写主函数,输出一张函数表(如下表所示)
//x的取值范围是[-1, +1],每次增加0.1,y = (x + 1)^2。要求调用函数f(x)计算(x + 1)^2 。
//            x            	y
//           - 1	       0.00
//           - 0.9	       0.01
//                   …
//             0.9	       3.61
//             1	       4.00

#include <iostream>
using namespace std;
int main ()
{
    double f (double x);
    for (double i = -1 ; i < 1 ; i += 0.1)
    {
        int k = 0;
        while ((k % 5) == 0)
        {
            cout << "\n\t";
            break;
        }
        cout << i << "           \t";
        cout << f(i);
        k ++;
    }
    cout << "\n\t";
    system ("pause");

}
double f (double x)
{
        return ((x + 1) * (x + 1));
}

T 4

//编写函数计算公式e = 1 + 1 / 1!+ 1 / 2!+ 1 / 3!+ ... + 1 / n!的近似值,
//在主函数中输入n的值,调用子函数计算阶乘,输出结果。
//两种写法 :  调用一次函数    调用两次函数

/*#include <iostream>
using namespace std;
int main ()
{
      double f (int n);
      int n = 0;
      cin >> n;
      cout << f (n) << "\n";
      system ("pause");
}
double f (int n)
{
      double sum = 0;
      for (int i = 1 ; i <= n ; i ++)
      {
                 int temp = 1;
           for (int j = 1; j <= i ; j ++)
           {  
                 temp = temp * j;
           }
           sum = sum + 1.0 / temp;
      }
      return (sum + 1) ;
}*/



/*#include <iostream>
using namespace std;
int main ()
{
      double f (int n);
      int n = 0;
      cin >> n;
      cout << f (n) << "\n";
      system ("pause");  
}
double f (int n)
{
      double fac (int n);
      double sum = 0;
      for (int i = 1 ; i <= n ; i++)
      {
            sum = sum + 1.0 / fac (i);
      }
      return sum + 1;

}
double fac (int n)
{
      int temp = 1;
      for (int i = 1; i <= n; i ++)
      {
            temp = temp * i;
      }
      return temp;
}*/

T 5

//猴子吃桃问题。猴子第1天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。
//第2天早上又将剩下的吃掉一半,又多吃了一个。
//以后每天早上都吃了前一天剩下的一半另加一个。到第10天早上想再吃时,就只剩一个桃子了。
//求第1天共摘了多少个桃子。(递归)

#include <iostream>
using namespace std;
int main ()
{
    int day_peach (int day);
    int day = 1;
    cout << day_peach (day) << '\n';
    system ("pause");
}
int day_peach (int day)
{
    if (day == 10)
    {
        return 1;
    }
    else
    {
        int yesterday_peach = 0;
        yesterday_peach = (day_peach (day + 1) + 1) * 2;          //   Y / 2 - 1 = D → Y = (D + 1) * 2
        return yesterday_peach;
    }
}

T 6

//编写函数对给出的年、月、日,计算该日是这一年的第n天。
//考虑闰年的情况

#include <iostream>
using namespace std;
int main ()
{
    int year = 0 , month = 0 ,day = 0;
    cout << "年   ";cin >> year;
    cout << "月   ";cin >> month;
    cout << "日   ";cin >> day;
    int sum_days = 0;
    int Feb_days = 0;
    int array_month_day [12] = {31,0,31,30,31,30,31,31,30,31,30,31};
    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
    {
        cout << "闰年\n";
        Feb_days = 29;
        array_month_day [1] = Feb_days;
    }
    else 
    {
        Feb_days = 28;
        array_month_day [1] = Feb_days;
    }
    
    for (int i = 1; i <= month; i ++)
    {
        sum_days = sum_days + array_month_day[i];
    }
    cout << sum_days << "\n";
    system ("pause");
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

os.system

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值