#include <iostream>
#include <cmath>
using namespace std;
int main() {
int m,n;
cin >> m >> n;
for (int i = m + 1; i < n; ++i) {
int j = 2;
for (; j <= sqrt(i); ++j) {
if (i % j == 0) {
break;
}
}
if (j > sqrt(i)) {
cout << i << " ";
}
}
}
//编程思路:倒序这个数字,然后和正序的数字进行比较,如果两个值一样,说明这个数字是回文数
#include <iostream>
using namespace std;
int main() {
int n, m, s = 0;
cin >> n;
while (n != -1) {
m = n;
//倒序这个数字并且保存到m
while (m > 0) {
//如果输入的这个数字还大于0,说明没完全倒序完
s = s * 10 + m % 10;
m/=10;
}
if (s == n) {
cout << n << endl;
}
cin >> n;
s = 0;//重置数字
}
}
之前的方法我觉得逻辑略复杂了一些,自己重新改了一个v2.0,大家看这个逻辑应该比较简单。
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
int num = 0;
getline(cin, str);
bool isAWord = false;//上一个字符是不是英文
for(int i = 0; i < str.size(); i++) {
if(str[i]<='z'&&str[i]>='a'||str[i]<='Z'&&str[i]>='A'){
if(isAWord==false) {//上个不是英文字符,这个是
isAWord = true;
num++;
}
}else{//这个不是字符,但是前一个是字符。
if(isAWord==true) {
isAWord = false;
}
}
}
cout << num;
}
#include <iostream>
using namespace std;
int main() {
int N, sum = 0;
cin >> N;
int arr[11][11];
int *max = new int[N];
for (int l = 0; l < N; ++l) {
max[l] = 0;
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cin >> arr[i][j];
if (arr[i][j] > max[j]) {
max[j] = arr[i][j];
}
}
}
for (int k = 0; k < N; ++k) {
sum += max[k];
}
delete max;
cout << sum << endl;
}
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int main() {
char nine[50];
int ten = 0;
char nineteen[50];
cin >> nine;
//1.先将九进制转换成十进制
int len = strlen(nine) - 1;
for (int i = len, k = 0; i >= 0; --i, ++k) {
int tmp = nine[i] - '0';//从最后往前依次拿到每一位的数
ten = ten + tmp * pow(9, k);
}
cout << ten << endl;
//2.再将十进制转换成十九进制
int k = 0;
while (ten != 0) {
int u = ten % 19;//取余数
ten /= 19;//除以19取下一个被除数
char tmp;
if (u < 10) {
tmp = u + '0';
} else {
tmp = u + 'a' - 10;
}
nineteen[k++] = tmp;
}
for (int j = k - 1; j >= 0; --j) {
cout << nineteen[j];
}
}
#include <iostream>
using namespace std;
bool isLeap(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return true;
else
return false;
}
int main() {
int year, month, day;
int count = 0;
cin >> year >> month >> day;
for (int i = year; i <= 2018; ++i) {
if (isLeap(i))
count++;
}
if (isLeap(year) && month >= 3) {
//如果出生的月份大于等于三月并且出生这年是闰年,说明没有经历2/29,但是上一个for却加了1,所以这里必须减去
count--;
}
cout << count;
}
#include <iostream>
#include <string>
#define MAX_CAP 50
using namespace std;
int main() {
string name[MAX_CAP];
string newName[MAX_CAP];
string date[MAX_CAP];
string newDate[MAX_CAP];
for (int i = 0; i < MAX_CAP; ++i) {
string s;
getline(cin, s);
for (int j = 0; j < s.size(); ++j) {
//将学生和日期先输入进去
if (s[j] == ' ') {
name[i] = s.substr(0, j);
date[i] = s.substr(j + 1, j + 12);
break;
}
}
}
for (int k = 0; k < MAX_CAP; ++k) {
//将学生按照年龄进行排序
string maxYear = "2200/13/13";
int maxIndex;
for (int j = 0; j < MAX_CAP; ++j) {
if (std::stoi(date[j].substr(0, 4)) < std::stoi(maxYear.substr(0, 4))) {
//谁的出生年份小谁年龄大
maxYear = date[j].substr(0, 10);
maxIndex = j;
} else if (std::stoi(date[j].substr(0, 4)) <= std::stoi(maxYear.substr(0, 4))
&& std::stoi(date[j].substr(5, 7)) < std::stoi(maxYear.substr(5, 7))) {
maxYear = date[j].substr(0, 10);
maxIndex = j;
} else if (std::stoi(date[j].substr(8, 10)) < std::stoi(maxYear.substr(8, 10))
&& std::stoi(date[j].substr(0, 4)) <= std::stoi(maxYear.substr(0, 4))
&& std::stoi(date[j].substr(5, 7)) <= std::stoi(maxYear.substr(5, 7))) {
maxYear = date[j].substr(0, 10);
maxIndex = j;
}
}
date[maxIndex] = "2200/13/13";
newDate[k] = maxYear;
newName[k] = name[maxIndex];
}
for (int l = 0; l < MAX_CAP; ++l) {
cout << newName[l] << " " << newDate[l] << endl;
}
}
//汉诺塔问题
//基本思路:A柱有n个圆盘,前n-1个圆盘需要先移动到B柱(C柱当做中介柱),然后B柱上的n-1个圆盘全部移动到C柱(A柱作为中介柱)
//然后将A盘的最后一个圆盘移动到C盘(递归的出口)
#include <iostream>
using namespace std;
//通过递归实现
void Harold(int n, char A, char B, char C) {
if (n == 1) {
cout << A << "->" << C << endl;
return;
}
Harold(n - 1, A, C, B);
cout << A << "->" << C << endl;
Harold(n - 1, B, A, C);
return;
}
int main() {
int n;//第一根柱子上有n个圆盘
cin >> n;
char A = 'A', B = 'B', C = 'C';
Harold(n, A, B, C);
}
#include <iostream>
using namespace std;
int main() {
int n, flag = 1;//flag就是往二维数组里面输入的值,当flag = n的时候循环结束
int row = 0, col = 0;
bool direction = true;
int num = 1;//每次循环输入一个值,结束循环输出两个值
cin >> n;
int arr[30][30]={0};
while (flag != n+1) {
for (int i = 0; i < num && flag != n+1; ++i) {
if (i != num - 1 && direction) {
//true的其他位置col++,row--
arr[row--][col++] = flag++;
} else if (i == num - 1 && direction) {
//true的最后一个col++,row不变
arr[row][col++] = flag++;
} else if (i != num - 1 && !direction) {
//false的其他位置row++,col--
arr[row++][col--] = flag++;
} else if (i == num - 1 && !direction) {
//false的最后一个row++,col不变
arr[row++][col] = flag++;
}
}
direction = !direction;
num++;
}
//打印二维数组的内容
int x = 0;
for (int i = 0; i < num; ++i) {
for (int j = 0; j < num; ++j) {
if (arr[i][j] != 0) {
cout << arr[i][j] << " ";
x++;
}
}
cout << endl;
}
}