题记:若立志投身算法研究,可精研理论算法:动态规划、递归、深度搜索等;若以解决问题为目的,主要为了工作内容,当尝试快而简单的方法,这该是学习的本意。
1.计算日期到天数转换
#include <iostream>
using namespace std;
int main()
{
int year,mon,day;
int data[]={31,28,31,30,31,30,31,31,30,31,30,31};
while(cin>>year>>mon>>day)
{
int sum;
for(int i=0;i<mon-1;i++)
{
sum=sum+data[i];
}
if((year%400==0||(year%4==0&&year%100!=0))&&mon>2)
sum=sum+1+day;
else
sum=sum+day;
cout<<sum<<endl;
}
return 0;
}
2.尼科彻斯定理
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int first;
while (cin >> n) {
first = n*n - n + 1;
cout << first;
for (int i = 1; i < n; ++i) {
cout << "+" << first + i * 2;
}
cout << endl;
}
return 0;
}
3.整型数组合并
#include <bits/stdc++.h>
using namespace std;
vector<int> arr;
int n, m;
int main() {
while (cin >> n) { //多组测试数据
arr.clear();
//输入与合并两个数组
for (int i = 1; i <= n; i ++ ) {
int x; cin >> x;
arr.push_back(x);
}
cin >> m;
for (int i = 1; i <= m; i ++ ) {
int x; cin >> x;
arr.push_back(x);
}
//对合并数组升序排序
sort(arr.begin(), arr.end());
map<int, int> st;
vector<int> ans;
//遍历并保存未标记元素
for (int i = 0; i < arr.size(); i ++ ) {
if (!st[arr[i]]) {
st[arr[i]] = 1; //更新标记
ans.push_back(arr[i]);
}
}
for (int i = 0; i < ans.size(); i ++ ) cout << ans[i];
cout << "\n";
}
return 0;
}
//set容器
#include<iostream>
#include <set>
using namespace std;
int main() {
int n1 = 0;
set<int> s1;
while (cin >> n1) { // 输入第一个数组的长度
int tmp = 0;
for (int i = 0; i < n1; i++) {
cin >> tmp;
s1.insert(tmp);
}
int n2 = 0;
cin >> n2; // 输入第二个数组的长度
for(int i = 0; i < n2; i++) {
cin >> tmp;
s1.insert(tmp);
}
for(auto it : s1) { // 输出容器set中的元素
cout << it;
}
cout<<endl;
s1.clear();
}
return 0;
}
4.字符串字符匹配
//C++实现,字符串匹配
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1,str2;
while(cin>>str1>>str2)
{
int len=str1.length();
int index=0;
for(int i=0;i<len;++i)
{
if(str2.find(str1[i])!=str2.npos) index++;
}
if(index==len) cout<<"true"<<endl;
else cout<<"false"<<endl;
}
return 0;
}
5.二维数组操作
#include<iostream>
using namespace std;
int main()
{
//1、shuru n,m;
int m,n,x1,y1,x2,y2,x,y,a,b;
while(cin>>m>>n>>x1>>y1>>x2>>y2>>x>>y>>a>>b)
{
if(m>0 && m<10 && n>0 && n<10)
{
cout<<"0"<<endl;
}
else
{
cout<<"-1"<<endl;
}
//2、swap
if(x1>=0 && x1<m && y1>=0 && y1<n && x2>=0 && x2<m && y2>=0 && y2<n )
{
cout<<"0"<<endl;
}
else
{
cout<<"-1"<<endl;
}
//3 insert x
if(m<9 && x<m)
{
cout<<"0"<<endl;
}
else
{
cout<<"-1"<<endl;
}
//insert y
if(n<9 && y<n)
{
cout<<"0"<<endl;
}
else
{
cout<<"-1"<<endl;
}
//find (x,y)
if(a >= 0 && a<m && b>=0 && b<n)
{
cout << "0" <<endl;
}
else
{
cout<<"-1"<<endl;
}
}
//return
return 0;
}
6.最长回文子串
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string s){
for(int i=0,j=s.length()-1;i<=j;i++,j--)
if(s[i]!=s[j]) return false;
return true;
}
int main(void){
string str;
int len,j,i,max=0;
cin >> str;
len=str.size();
for(i=0;i<len-1;i++){
for(j=len-i;j>0;j--){//截取的字符长度
if(isPalindrome(str.substr(i,j))){
if(max<j){
max=j;
}
}
}
}
cout<<max;
return 0;
}
7.走方格的方案数
#include<bits/stdc++.h>
using namespace std;
int dp[10][10];
int main(){
int n,m;
dp[0][1] = 1; // 保证dp[1][1] 为 1
for(int i = 1;i < 10;i++){
for(int j = 1;j < 10;j++){
dp[i][j] = dp[i-1][j] + dp[i][j-1]; // 递推式
}
}
while(~scanf("%d %d",&n,&m)){
printf("%d\n",dp[n+1][m+1]); // 直接输出答案
}
return 0;
}
8.记票统计
#include<vector>
#include<unordered_map>
using namespace std;
int main(){
int n;
while(cin >> n){
string str;
vector<string> people;
unordered_map<string, int> mp;
for(int i = 0 ; i < n; i ++ ){
cin >> str;
people.emplace_back(str);
mp[str] = 0;
}
int n2;
cin >> n2;
int Invalid = 0;
for(int i = 0 ; i < n2; i ++ ){
cin >> str;
if(mp.find(str) != mp.end()){
mp[str] ++;
}else{
Invalid ++ ;
}
}
for(int i = 0 ; i < n; i ++ ){
cout << people[i] <<" : " << mp[people[i]] <<endl;
}
cout << "Invalid" <<" : " << Invalid <<endl;
}
return 0;
}
9.自守数
#include<iostream>
using namespace std;
int main(){
int n;
while(cin>>n){
int ans=0;
for(int i=1;i<=n;i++){
if(i%10==1||i%10==5||i%10==6){ //找到可能成为自守数的数
int temp1=i, temp2=i*i;
while(temp1){//验证后几位
if(temp2%10!=temp1%10) break;
temp1/=10;
temp2/=10;
}
if(temp1==0) ans++;
}
}
cout<<ans+1<<endl;//带上0
}
return 0;
}
10.字符统计
#include <map>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main(){
string s;
map<char,int> mp;
while(cin>>s){
//每次循环都要清一下map
mp.clear();
for(char c:s){
++mp[c];
}
//用迭代器范围初始化一个vector
vector<pair<char,int>> vec(mp.begin(),mp.end());
//因为map已经依照键排序了,所以用稳定排序当次数一样时,会保持原来的先后关系,即ASCII码的升序
stable_sort(vec.begin(),vec.end(),[](const pair<char,int>& a,const pair<char,int>& b){
//用Lambda来写配合算法使用的小函数是非常方便的
return a.second>b.second;
});
for(auto iter=vec.begin();iter!=vec.end();++iter){
cout<<iter->first;
}
cout<<endl;
}
}
//map建立字符与次数之间的映射
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str;
map<char,int> mp;
while(getline(cin,str))
{
map<char,int> mp;
for(int i=str.size()-1;i>=0;i--){//统计出现次数
mp[str[i]]++;
}
string res;
for(int i=str.size();i>=0;i--)//按照大小排序
{
for(auto x:mp)//按照ASCII码的大小遍历一遍mp
{
if(x.second==i){//如果有字符次数为i的则把该字符添加到res中
res += x.first;
}
}
}
cout<<res<<endl;
}
return 0;
}
11.最小公倍数
#include<iostream>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
//最小公倍数一定是较大者的整数倍
//a*b一定是a、b的公倍数,但不一定是最小的
//所以我们从i=1,到i=min(a,b)一一遍历去寻找最小公倍数
for(int i=1;i<=min(a,b);i++)
{
int beishu=i*max(a,b);
if(beishu%a==0&&beishu%b==0)
{
cout<<beishu<<endl;
break;
}
}
return 0;
}