您的位置:首页 > 其它

循环15~17

2015-08-27 21:20 260 查看
循环15

#include<iostream>

bool just(long n);

int main()

{

 using namespace std;

 long a, b; int count = 0; long sum = 0;

 cout << "Enter 2 integer between 1 and 500 :";

 cin >> a >> b;

 for (int i = a; i < b + 1; i++)

 {

  if (just(i))

  {

   count++;

   sum += i;

  }

 }

 cout << count << ' ' << sum << endl;

 return 0;

}

bool just(long n)

{

 if (n == 2)

  return true;

 else if (n % 2 == 0)

  return false;

 int mark = 0;

 for (int i = 3; i < n / 2 + 1; i += 2)

 {

  if (n % i == 0)

   mark = 1;

 }

 if (mark == 0)

  return true;

 else

  return false;

}

 

循环16

#include<iostream>

int main()

{

 using namespace std;

 int n; int sum = 1;

 cout << "Enter eat day ,between 2 to 10:"; cin >> n;

 for (int i = 2; i < n + 1; i++)

 {

  sum = 2 * (sum + 1);

 }

 cout << sum << endl;

 return 0;

}

 

循环17

#include<iostream>

int main()

{

 using namespace std;

 int one; char ch; int two;

 cout << "Enter your quation :";

 cin >> one;

 while (cin >> ch &&ch!='=')

 {

  cin >> two;

  switch (ch)

  {

  case'+':one = one + two; break;

  case'-':one = one - two; break;

  case'*':one = one*two; break;

  case'/':

  {if (two == 0)

   break;

  else

   one = one / two; break; }

  default:

   cout << "Error .\n";

  }

 }

 cout << one << endl;

 return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT