T1——斐波那契程序员-2
代码如下——(不是最优解)
#include<iostream>
using namespace std;
int f(int a,int b,int c,int m)
{
if (m == 1)return a;
if (m == 2)return b;
if (m > 2)
{
int x = f(a,b,c,m - 2) + f(a,b,c,m - 1) - c;
if (x < 1)return 1;
else return x;
}
}
int main()
{
int n;cin >> n;
while (n--)
{
int sum = 0;
int a, b, c, m; cin >> a >> b >> c >> m;
for (int i = 1; i <= m; ++i)
{
sum += f(a, b, c, i);
}
cout << sum << endl;
}
return 0;
}
注释——
#include<iostream>
using namespace std;
int f(int a, int b, int c, int m)
{
if (m == 1)return a;//当为第一个数时
if (m == 2)return b;//当为第二个数时
if (m > 2)//m大于2时开始递归(递归肯定不是最优解)
{
int x = f(a, b, c, m - 2) + f(a, b, c, m - 1) - c;//第n项 与 第n-1项和第n-2项的关系
if (x < 1)return 1;//根据题目要求返回值
else return x;
}
}
int main()
{
int n; cin >> n;//n个案例
while (n--)
{
int sum = 0;//总和的初始化
int a, b, c, m; cin >> a >> b >> c >> m;//四个变量
for (int i = 1; i <= m; ++i)//1~m循环
{
sum += f(a, b, c, i);//累加
}
cout << sum << endl;//输出
}
return 0;
}
T2——字母表
代码如下——(不是最优解)
#include <iostream>
using namespace std;
int main() {
char input;
cin >> input;
for (char i = 'A'; i <= 'Z'; ++i) {
if (toupper(input) != i) {
cout << i;
}
}
cout << endl;
for (char i = 'a'; i <= 'z'; ++i) {
if (tolower(input) != i) {
cout << i;
}
}
cout << endl;
return 0;
}
注释——
#include <iostream>
using namespace std;
//其实这题可以用字符串去写 先删去字符串中指定的子序列 再用toupper()tolower()函数转换为大小写
int main() {
char input;
cin >> input;//删去的字母
for (char i = 'A'; i <= 'Z'; ++i) {
if (toupper(input) != i) {//字符串转换大写变小写小写变大写我在csdn上的可能考点里都发过
cout << i;
}
}
cout << endl;
for (char i = 'a'; i <= 'z'; ++i) {
if (tolower(input) != i) {
cout << i;
}
}
cout << endl;
return 0;
}
大小写的转换我之前都发过
大小写字母函数在之前的文里也有——
23-24C++(47)——字符串应用+大写isupper(a[i])+小写islower(a[i])+大转小tolower(a[i])+小转大toupper(a[i])+数字isdigit函数
T3——总天数
代码如下——(不是最优解)
#include <iostream>
using namespace std;
//bool isLeapYear(int year) {
// if ((year % 400 == 0 && year % 3200 != 0) || (year % 4 == 0 && year % 100 != 0)) {
// return true;
// }
// return false;
//}
int jinian(int year) {
int runnian = year / 4;runnian -= year / 100; runnian += year / 400;runnian -= year / 3200;
return runnian;
}
long long totalDays(int a, int b) {
int leapYears = jinian(b) - jinian(a - 1);
long long days = (long long)(b - a + 1) * 365 + leapYears;
return days;
}
int main() {
int n; cin >> n;
while (n--) {
int a, b;cin >> a >> b;
cout << totalDays(a, b) << endl;
}
return 0;
}
注释——
#include <iostream>
using namespace std;
//bool isLeapYear(int year) {//平常对闰年的判断方法
// if ((year % 400 == 0 && year % 3200 != 0) || (year % 4 == 0 && year % 100 != 0)) {
// return true;
// }
// return false;
//}
int countLeapYears(int year) {//下面放一张图就清楚了
int leapYears = year / 4;
leapYears -= year / 100;
leapYears += year / 400;
leapYears -= year / 3200;
return leapYears;//返回闰年天数
}
long long totalDays(int a, int b) {//一共多少天
int leapYears = countLeapYears(b) - countLeapYears(a - 1);//得到a到b之间闰年的个数
long long days = (long long)(b - a + 1) * 365 + leapYears;//直接加leapyears是因为366就比365多一天 直接加闰年个数就可以
return days;//返回总共天数
}
int main() {
int n;
cin >> n;//n个案例
while (n--) {
int a, b;
cin >> a >> b;//a~b
cout << totalDays(a, b) << endl;
}
return 0;
}
T4——几个0
代码如下——(不是最优解)
#include <iostream>
#include <vector>
using namespace std;
int two(int n) {
int count = 0;
while (n && n % 2 == 0) {
count++;
n /= 2;
}
return count;
}
int Five(int n) {
int count = 0;
while (n && n % 5 == 0) {
count++;
n /= 5;
}
return count;
}
int Zeros(const vector<int>& numbers) {
int twos = 0;
int fives = 0;
for (int number : numbers) {//这个就相当于auto ch:s这个一样!!!!!!!!!!!!我昨天晚上复习的时候才又打了一遍的!!
if (number == 0) {
return 1;
}
twos += two(number);
fives += Five(number);
}
return min(twos, fives);
}
int main() {
int n;cin >> n;
while (n--) {
int m; cin >> m;
vector<int> numbers(m);
for (int i = 0; i < m; ++i) {
cin >> numbers[i];
}
cout << Zeros(numbers) << endl;
}
}
注释——(带思路)
#include <iostream>//这题不能用相乘完的总和再去判断 (开个大的数组是可以的) 但是现在的longlongint包含不了好几个一亿相乘的数
#include <vector>//vector容器头文件
using namespace std;//思路:5和5的倍数有几个 2和2的倍数有几个 取两者最小的数量 就是一部分零的个数
int two(int n) {//思路:当然还要考虑有零的时候 再考虑一下个位是零的 全部个数加起来就是答案
int count = 0;
while (n && n % 2 == 0) {// 2和2的倍数有几个
count++;
n /= 2;
}
return count;
}
int Five(int n) {
int count = 0;
while (n && n % 5 == 0) {//5和5的倍数有几个
count++;
n /= 5;
}
return count;
}
int Zeros(const vector<int>& numbers) {//考虑有零的时候 再考虑一下个位是零的
int twos = 0;
int fives = 0;
for (int number : numbers) {//这个就相当于auto ch:s这个一样!!!!!!!!!!!!我昨天晚上复习的时候才又打了一遍的!!
if (number == 0) {
return 1;
}
twos += two(number);
fives += Five(number);
}
return min(twos, fives);//取两者数量最小的才可以
}
int main() {
int n; cin >> n;//n次循环
while (n--) {
int m; cin >> m;
vector<int> numbers(m);//开一个vector容器保存数字
for (int i = 0; i < m; ++i) {
cin >> numbers[i];//输入
}
cout << Zeros(numbers) << endl;//输出
}
}
T5——世纪
本来是对的了555555错判了好多次
代码如下——(不是最优解)
#include <iostream>
#include <string>
using namespace std;
string T(int century) {
if (century % 10 == 1) return "st";
else if (century % 10 == 2) return "nd";
else if (century % 10 == 3) return "rd";
else return "th";
}
string E(int year) {
int century = year/ 100 +1;
string S = T(century);
return to_string(century) + S + " century";
}
int main() {
int n;cin >> n;
while (n--) {
int year; cin >> year;
cout << E(year) << endl;
}
return 0;
}
注释——
#include <iostream>
#include <string>
using namespace std;
string T(int century) {//数字加th
if (century % 10 == 1) return "st";//1st
else if (century % 10 == 2) return "nd";//2nd
else if (century % 10 == 3) return "rd";//3rd
else return "th";//剩下都是th
}
string E(int year) {
int century = year / 100 + 1;//世纪的判断
string S = T(century);
return to_string(century) + S + " century";//to_string 函数:将数字常量转换为字符串,返回值为转换完毕的字符串
}
int main() {
int n; cin >> n;//n个循环
while (n--) {
int year; cin >> year;
cout << E(year) << endl;
}
return 0;
}
T6——强质数
代码如下——(不是最优解)
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
bool zhishu(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
bool qiangzhishu(int n) {
if (!zhishu(n)) return false;
string num = to_string(n);
for (size_t i = 1; i < num.size(); ++i) {
int part1 = stoi(num.substr(0, i));
int part2 = stoi(num.substr(i));
if (zhishu(part1) && zhishu(part2)) return true;
}
return false;
}
int main() {
int n;cin >> n;
while (n--) {
int a;cin >> a;
cout << (qiangzhishu(a) ? "Yes" : "No") << endl;
}
return 0;
}
注释——
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
// 检测一个整数n是否是质数的函数
bool zhishu(int n) {
// 1和更小的数不是质数
if (n <= 1) {
return false;
}
// 2和3是质数
if (n <= 3) {
return true;
}
// 能被2或3整除的数不是质数
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
// 用6k±1规则检测质数
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
// 通过所有测试,n是质数
return true;
}
// 检测一个整数n是否是强质数的函数
bool qiangzhishu(int n) {
// 如果n不是质数,则不是强质数
if (!zhishu(n)) {
return false;
}
// 将n转换为字符串
string num = to_string(n);
// 检查n是否可以分割为两个质数
for (size_t i = 1; i < num.size(); ++i) {
// 获取分割后的两部分
int part1 = stoi(num.substr(0, i));
int part2 = stoi(num.substr(i));
// 如果两部分都是质数,则n是强质数
if (zhishu(part1) && zhishu(part2)) {
return true;
}
}
// 无法分割为两个质数,n不是强质数
return false;
}
// 主函数
int main() {
int n;
cin >> n; // 读取要检测的数字数量
while (n--) {
int a;
cin >> a; // 读取一个待检测的数字
cout << (qiangzhishu(a) ? "Yes" : "No") << endl;
}
return 0;
}