c++打印一個(gè)沙漏。輸入一個(gè)整數(shù)n,,符號(hào)的行數(shù)(n是大于1的奇數(shù))輸出使用“*”打印沙漏符號(hào)
請(qǐng)使用c++,,別看題目上的pascal,用c++
#include<iostream>using namespace std;int main(){int n;char symbol;cin >> n >> symbol;if (n < 1) return 0;n = n - 1;int max_row = 1;int row_num = 0;while(n >= 0){if (n - 2 * (max_row + 2) < 0) break;max_row += 2;++row_num;n -= max_row * 2;}for (int i = 0; i < row_num; i++){for (int j = 0; j < i; j++){cout << " ";}for (int j = 0; j < max_row - 2 * i; j++){cout << symbol;}cout << endl;}for (int i = row_num; i >= 0; i--){for (int j = 0; j < i; j++){cout << " ";}for (int j = 0; j < max_row - 2 * i; j++){cout << symbol;}cout << endl;}cout << n;return 0;}2、素?cái)?shù)對(duì)猜想思路:判斷素?cái)?shù)的函數(shù)直接默寫;因?yàn)榕袛嘞噜徦財(cái)?shù),,直接用一個(gè)變量記錄上一個(gè)素?cái)?shù),,再與新得到的素?cái)?shù)比較#include<iostream>using namespace std;bool isPrime(int num){if (num == 0 || num == 1) return false;for (int i = 2; i * i <= num; i++){if (num % i == 0) return false;}return true;}int main(){int n;cin >> n;int count = 0;int lastPrime = 2;for (int i = 3; i <= n; i++){if (isPrime(i)){if (i - lastPrime == 2) ++count;lastPrime = i;}}cout << count;return 0;}