【C/C++題目】基礎題目合集——楊輝三角;字符串大小寫轉換;日期及星期打印;

一、楊輝三角打印

楊輝三角中第n行第k個數的值,也就是
在這裏插入圖片描述

1、實現代碼

#include<iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::setw;
using std::endl;

//求階乘的函數
long long factorial(int num)
{
	if (num == 0)	//第一行
		return 1;
	else
	{
		long long  result = 1;
		for (int i = 1; i <= num; ++i)
			result *= i;
		return result;
	}
}

//打印楊輝三角的函數
void function(int rowNum)
{
	long long value;
	for (int n = 1; n <= rowNum; ++n) //每一行
	{
		for (int i = 0; i <= rowNum - n; ++i)  //打印每行前的空格
			cout << setw(3 * i);
		for (int k = 1; k <= n; ++k)  //打印每行的每個數
		{
			value = factorial(n - 1) / (factorial(k - 1) * factorial(n - k));	//利用階乘求得
			cout << value << setw(6);
		}
		cout << endl;
	}
}

int main()
{
	int rowNum;
	cout << "請輸入要打印的行數:";
	cin >> rowNum;
	function(rowNum);
	system("pause");
	return 0;
}

2、運行結果

在這裏插入圖片描述
 

二、輸入任意長度字符串, 將其中的小寫字母替換爲大寫字母, 大寫字母替換爲小寫字母, 其他不變

利用ASCII碼進行功能實現

1、實現代碼

#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::string;
int main()
{
	cout << "樣例輸入:";
	string str;
	getline(cin, str);

	for (int i = 0; i < str.length(); i++)
	{
		if (str[i] > 65 && str[i] < 90)
			str[i] += 32;	//大寫轉小寫
		else if (str[i] > 97 && str[i] < 122)
			str[i] -= 32;	//小寫轉大寫
	}

	cout << "樣例輸出:";
	for (auto it = str.cbegin(); it != str.cend(); ++it)	//打印
		cout << *it;

	return 0;
}

2、運行結果

在這裏插入圖片描述
 

三、獲取當前的年月日並打印出來

使用系統的時間函數

1、實現代碼

#include <iostream>
#include <time.h>
using namespace std;
#pragma warning( disable : 4996 )//去掉4996類型報錯,解決localtime函數的問題
struct NowDate//創建類
{
    char tmp0[16]; //年月日
    char tmp1[16]; //時分秒
};
NowDate getTime()//創建類的方法
{
    time_t timep;//time_t,存儲從1970年到現在經過了多少秒
    time(&timep); //time函數會返回自1970年1月1日0點走過的秒數,同時把這個返回值保存在傳進來的那個time_t* 指向的變量timep中
    NowDate date;//NowDate的一個實例化date
    //strftime()函數將時間格式化爲我們想要的格式           //localtime()將時間數值變換成本地時間
    strftime(date.tmp0, sizeof(date.tmp0), "%Y-%m-%d", localtime(&timep));//格式%Y-%m-%d年月日
    strftime(date.tmp1, sizeof(date.tmp1), "%H:%M:%S", localtime(&timep));//格式"%H:%M:%S"時分秒
    return date;
}
int main()
{
    NowDate time = getTime();//實例化
    cout <<"年月日:"<< time.tmp0 << endl;
    cout <<"時分秒:"<< time.tmp1 << endl;
    return 0;
}

2、運行結果

在這裏插入圖片描述
 

四、已知1970/1/1是週四, 求2020429是周幾( 禁止使用語言本身提供的日期函數)

計算出迄今爲止的總天數,在除以7,求餘數得星期幾。

1、實現代碼

#include <iostream>
using namespace std;
int main()
{
	int year, month, day, n;
	int a[13], sum1, sum2, sum;
	cout << "輸入當前年月日:";
	cin >> year >> month >> day;
	sum1 = 0; sum2 = day; sum = 0;

	for (int i = 1970; i < year; i++)
	{
		if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)     //閏年天數366
			n = 366;
		else n = 365;
		sum1 = sum1 + n;    //每年天數和
	}

	//利用數組給每一個月確定天數
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)        //閏年二月29
		a[2] = 29;
	else a[2] = 28;
	for (int i = 1; i <= 12; i++)
	{
		if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
			a[i] = 31;
		else if (i == 4 || i == 6 || i == 9 || i == 11)
			a[i] = 30;
	}

	for (int i = 1; i < month; i++)     //每月天數和
		sum2 = sum2 + a[i];
	sum = sum1 + sum2;      //1970至今的總天數

	cout << "計算結果:";
	switch (sum % 7)        //因爲1970/1/1爲星期四
	{
	case 0:cout << "星期三\n"; break;
	case 1:cout << "星期四\n"; break;
	case 2:cout << "星期五\n"; break;
	case 3:cout << "星期六\n"; break;
	case 4:cout << "星期日\n"; break;
	case 5:cout << "星期一\n"; break;
	case 6:cout << "星期二\n"; break;
	default:break;
	}
	return 0;
}

2、運行結果

在這裏插入圖片描述
 

如有不足之處,還望指正 1


  1. 如果對您有幫助可以點贊、收藏、關注,將會是我最大的動力 ↩︎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章