算法笔记学习
入门模拟
简单模拟
1.剩下的树
#include <algorithm> #include<iostream> #include<cstring> #include<cstdlib> using namespace std; int main() { int m,n; while(scanf("%d %d",&n,&m)!=EOF&&(n!=0)){ int a[n+1]; int count = 0; for(int i = 0 ;i <= n ;i++){ a[i] = 1; } int l,r,tmp; for(int i = 0;i < m;i++){ scanf("%d %d",&l,&r); if(r < l){ tmp = l; l = r; r = tmp; } for(int i = l;i <= r;i++){ a[i] = 0; } } for(int i = 0;i <= n;i++){ count += a[i]; } cout<<count<<endl; } return 0; }
2.A+B
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); using namespace std; int main(){ char a[15],b[15]; while(scanf("%s %s",a,b)!=EOF){ long long int c,d,tmp; c = d = 0; tmp = 1; for(int i = strlen(a) - 1;i > 0;i--){ if(a[i] != ','){ c += tmp*(a[i] - '0'); tmp *= 10; } } if(a[0] != '-'){ c += tmp*(a[0] - '0'); tmp *= 10; } else{ c = -c; } tmp = 1; for(int i = strlen(b) - 1;i > 0;i--){ if(b[i] != ','){ d += tmp*(b[i] - '0'); tmp *= 10; } } if(b[0] != '-'){ d += tmp*(b[0] - '0'); tmp *= 10; } else{ d = -d; } cout<<c+d<<endl; } return 0; }
3.特殊乘法
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); using namespace std; int main(){ char a[15],b[15]; int la,lb; while(scanf("%s %s",a,b)!=EOF){ la = strlen(a); lb = strlen(b); long long int count = 0; for(int i = 0;i < la;i++){ for(int j = 0;j < lb;j++){ count += (a[i] - '0') * (b[j] - '0'); } } cout<<count<<endl; } return 0; }
4.比较奇偶数
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); using namespace std; int main(){ int n,c1,c2,a; while(scanf("%d",&n)!=EOF){ c1 = c2 = 0; while(n--){ scanf("%d",&a); (a%2==0)?(c2++):(c1++); } (c2>c1)?(cout<<"NO"<<endl):(cout<<"YES"<<endl); } }
5.Shortest Distance
//Shortest Distance #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); using namespace std; int main(){ int m,n; scanf("%d",&n); int a[n+1]; a[0] = 0; int sum = 0; for(int i = 1;i <= n;i++){ scanf("%d",&a[i]); sum += a[i]; a[i] = a[i] + a[i-1]; } scanf("%d",&m); int l,r,tmp,count; while(m--){ scanf("%d %d",&l,&r); count = 0; if(l>r){ tmp = l; l = r; r = tmp; } count = a[r-1] - a[l-1]; (sum-count<count)?(cout<<sum-count<<endl):(cout<<count<<endl); } return 0; }
6.A+B和C
#include<iostream> #include<cstring> #include<cstdlib> using namespace std; int main(){ int n; long long int a,b,c; int count = 0; scanf("%d",&n); while(n--){ count++; scanf("%lld %lld %lld",&a,&b,&c); if(a+b>c){ cout<<"Case #"<<count<<": true"<<endl; } else{ cout<<"Case #"<<count<<": false"<<endl; } } }
7.部分A+B
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); using namespace std; int main(){ long long int a,aa,b,bb,sum,tmp; while(scanf("%lld %lld %lld %lld",&a,&aa,&b,&bb)!=EOF){ tmp = 1; sum = 0; while(a>0){ if(a%10==aa){ sum += tmp*aa; tmp *= 10; } a /= 10; } tmp = 1; while(b>0){ if(b%10==bb){ sum += tmp*bb; tmp *= 10; } b /= 10; } cout<<sum<<endl; } return 0; }
8.锤子剪刀布
#include<algorithm> #include<cmath> #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; int bcj(char a,char b){ if(a==b){ return 2; } if(a-b>0){ if(a=='C'||b=='C'){ return 0; } else{ return 1; } } else{ if(a=='C'||b=='C'){ return 1; } else{ return 0; } } } char cmp3(int a,int b,int c){ if(a>=b&&a>=c){ return 'B'; } else if(b>=a&&b>=c){ return 'C'; } else{ return 'J'; } } int main(){ long long int n,an,bn,ab,bcj1[3],bcj2[3]; char a,b; memset(bcj1,0,sizeof(bcj1)); memset(bcj2,0,sizeof(bcj2)); ab = an = bn = 0; scanf("%d\n",&n); int tmp; while(n--){ scanf("%c %c\n",&a,&b); tmp = bcj(a,b); if(tmp==2){ ab++; } else if(tmp==1){ an++; bcj1[(a-'B')%3] += 1; } else{ bn++; bcj2[(b-'B')%3] += 1; } } cout<<an<<" "<<ab<<" "<<bn<<endl; cout<<bn<<" "<<ab<<" "<<an<<endl; cout<<cmp3(bcj1[0],bcj1[1],bcj1[2])<<" "<<cmp3(bcj2[0],bcj2[1],bcj2[2])<<endl; return 0; }
查找元素
1.学生人数
#include<algorithm> #include<cmath> #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; int main(){ int n,tmp; int a[101]; while(scanf("%d",&n)!=EOF,n!=0){ memset(a,0,sizeof(a)); while(n--){ scanf("%d",&tmp); a[tmp] += 1; } scanf("%d",&tmp); cout<<a[tmp]<<endl; } return 0; }
2.找X
#include <cstdio> const int maxn = 210; int a[maxn]; //存放n个数 int main() { int n, x; while(scanf("%d", &n) != EOF){ for(int i=0;i < n;i++){ scanf("%d",&a[i]); } scanf("%d", &x); int k; for(k = 0;k< n; k++) { if(a[k]==x) { printf("%d\n", k); break; } } if(k == n){ printf("-1\n"); } } return 0; }
3.学生信息
#include<algorithm> #include<cmath> #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; char a[1000][100]; int main(){ int n,m,i,j; char b[10],tmp[100],tmp1[100]; scanf("%d\n",&n); int count = 1; while(count<=n){ cin.getline(tmp,100); sscanf(tmp,"%d %s %s %d",&i,tmp1,tmp1,&j); a[i] = tmp; count++; } cout<<a[4]<<endl; scanf("%d",&m); while(m--){ scanf("%s",b); sscanf(b,"%d",&count); if(count>n||count<1){ cout<<"No Answer!"<<endl; } else{ cout<<a[count]<<endl; } } return 0; } #include <cstdio> #include <string.h> const int N = 1010; struct student{ char id[100]; char name[200]; char sex[20]; int age; }stu[N]; int main(){ int n, m, flag; char temp[1000]; while(scanf("%d", &n) != EOF){ for(int i = 0; i < n; i++){ scanf("%s%s%s%d", stu[i].id, stu[i].name,stu[i].sex, &(stu[i].age)); } scanf("%d", &m); for(int i = 0; i < m; i++){ scanf("%s", temp); flag = -1; for(int j = 0; j < n; j++){ if(strcmp(stu[j].id, temp) == 0) flag = j; } if(flag == -1) printf("No Answer!\n"); else printf("%s %s %s %d\n", stu[flag].id, stu[flag].name, stu[flag].sex, stu[flag].age); } } }
4.查找
#include <cstdio> const int maxn=110; int a[maxn]; int main(){ int n, m, f; while(scanf("%d", &n) !=EOF){ for (int i=0; i<n; i++){ scanf("%d", &a[i]); } scanf("%d", &m); for (int i=0; i<m; i++){ scanf("%d", &f); for(int j=0; j<n; j++){ if(f==a[j]){ printf("YES\n"); break; } else if (f!=a[j] && j==n-1)printf("NO\n"); } } } return 0; }
5.查询
#include<stdio.h> #include<string.h> struct Aha{ char xuehao[20]; char xingmaing[100]; char xingbie[10]; int age; }stu[1001]; int main() { int n,nn; scanf("%d",&nn); while(nn--){ scanf("%d",&n); char aa[100]; for(int i=0;i<n;i++) { scanf("%s%s%s%d",stu[i].xuehao,stu[i].xingmaing,stu[i].xingbie,&stu[i].age); } scanf("%s",&aa); int ming; for(int i=0;i<n;i++) { if(strcmp(aa,stu[i].xuehao)==0){ printf("%s %s %s %d\n",stu[i].xuehao,stu[i].xingmaing,stu[i].xingbie,stu[i].age); break; } else if(strcmp(aa,stu[i].xuehao)!=0&&i==n-1)printf("No Answer!\n"); } } }
图形输出
1.梯形
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); using namespace std; int main(){ int n; while(scanf("%d",&n)!=EOF){ int a,b; a=2*(n-1); b=n; for(int i=0;i<n;i++){ for(int j=0;j<a;j++){ cout<<" "; } a -= 2; for(int j=0;j<b;j++){ cout<<"*"; } b += 2; cout<<endl; } } return 0; }
2.Hello World for U
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); using namespace std; int main(){ char a[81]; scanf("%s",a); int N,n1,n2,n3,l,k; N = strlen(a); n1=n3=floor((N+2)/3); n2 = N + 2 - 2*n1; l = 0; k = N - 1; for(int i=0;i<n1-1;i++){ cout<<a[l]; for(int j=0;j<n2-2;j++){ cout<<" "; } cout<<a[k]<<endl; l += 1; k -= 1; } for(int i=l;i<=k;i++){ cout<<a[i]; } cout<<endl; return 0; }
3.等腰梯形
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); using namespace std; int main(){ int n,m; scanf("%d",&m); while(m--){ scanf("%d",&n); int a,b; a=(n-1); b=n; for(int i=0;i<n;i++){ for(int j=0;j<a;j++){ cout<<" "; } for(int j=0;j<b;j++){ cout<<"*"; } for(int j=0;j<a;j++){ cout<<" "; } a -= 1; b += 2; cout<<endl; } } return 0; }
4.沙漏图形 tri2str [1*+]
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); using namespace std; int main(){ int n,a,b; scanf("%d",&n); a = 0; b = n-1; for(int i=0;i<n;i++){ for(int j=0;j<i;j++){ cout<<" "; } cout<<"*"; for(int j=0;j<b;j++){ cout<<" *"; } for(int j=0;j<i;j++){ cout<<" "; } cout<<endl; b--; } b = 1; for(int i=n-1;i>0;i--){ for(int j=1;j<i;j++){ cout<<" "; } cout<<"*"; for(int j=0;j<b;j++){ cout<<" *"; } for(int j=1;j<i;j++){ cout<<" "; } cout<<endl; b++; } return 0; }
日期处理
1.日期差值
#include<stdio.h> int isprime(int n){ if((n%4==0&&n%100!=0)||n%400==0) return 1; else return 0; } int main(){ int num[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31}, {30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}}; int a,b,t; int y1,m1,d1,y2,m2,d2; while(scanf("%d %d",&a,&b)!=EOF){ if(a>b){ t=a; a=b; b=t; } y1=a/10000;m1=a%10000/100;d1=a%100; y2=b/10000;m2=b%10000/100;d2=b%100; int count=0; while(y1!=y2||m1!=m2||d1!=d2){ d1++; if(d1==num[m1][isprime(y1)]+1){ d1=1; m1++; } if(m1==13){ m1=1; y1++; } count++; } printf("%d\n",count+1); } return 0; } #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); int month[13][2] = {{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}}; int days[2] = {365,366}; bool isLeap(int y){ return (y%4==0&&y%100!=0)||(y%400==0); } using namespace std; int main(){ long int a,b,tmp,count; while(scanf("%ld%ld",&a,&b)!=EOF){ count = 0; if(a<b){ tmp = a; a = b; b = tmp; } if(a==b){ cout<<2<<endl; break; } int ya,yb,ma,mb,da,db; da = a%100; ma = (int(a/100))%100; ya = int(a/10000); db = b%100; mb = (int(b/100))%100; yb = int(b/10000); int flag = 0; int m,d; if(ya==yb){ if(isLeap(ya)){ flag = 1; } else{ flag = 0; } for(int j=mb;j<ma;j++){ count += month[j][flag]; } cout<<count+da-db+1<<endl; continue; } for(int i = yb;i<ya;i++){ if(isLeap(i)){ flag = 1; } else{ flag = 0; } count += days[flag]; } if(isLeap(ya)){ flag = 1; } else{ flag = 0; } for(int j = 1;j<mb;j++){ count -= month[j][flag]; } for(int j = 1;j<ma;j++){ count += month[j][flag]; } cout<<count+da-db+1<<endl; } return 0; }
2.Day of Week
#include<stdio.h> #include<string.h> int month[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}}; int islyear(int a) { if((a%4==0&&a%100!=0)||a%400==0) { return 1; } else return 0; } int sum(int y,int m,int d) { int i,day=0; for(i=0;i<y;i++) { if(islyear(i)) { day+=366; }else { day+=365; } } for(i=0;i<m;i++) { day+=month[i][islyear(y)]; } for(i=1;i<d;i++) { day++; } return day; } int findmonth(char *month) { //printf("input_month=%s\n",month); if(strcmp(month,"January")==0)return 1; if(strcmp(month,"January")==0)return 1; if(strcmp(month,"February")==0)return 2; if(strcmp(month,"March")==0)return 3; if(strcmp(month,"April")==0)return 4; if(strcmp(month,"May")==0) return 5; if(strcmp(month,"June")==0)return 6; if(strcmp(month,"July")==0)return 7; if(strcmp(month,"August")==0)return 8; if(strcmp(month,"September")==0)return 9; if(strcmp(month,"October")==0)return 10; if(strcmp(month,"November")==0)return 11; if(strcmp(month,"December")==0)return 12; } int main() { int year,day; char input_month[20]; while(scanf("%d%s%d",&day,input_month,&year)!=EOF) { int m=findmonth(input_month); //printf("the input is %d %d %d\n",day,m,year); int n=sum(year,m,day)-sum(2019,2,24); //printf("n1=%d\n",n); n=((n%7)+7)%7; //printf("n2=%d\n",n); switch(n) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 0: printf("Sunday\n"); break; } } //printf("%d\n",-16%7); return 0; }
3.打印日期
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); int month[13][2] = {{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}}; int days[2] = {365,366}; bool isLeap(int y){ return (y%4==0&&y%100!=0)||(y%400==0); } using namespace std; int main(){ int y,m,d,n,days; while(scanf("%d %d",&y,&n)!=EOF){ days=0; for(m=0;days<n;m++) days+=month[m+1][isLeap(y)]; days-=month[m][isLeap(y)]; for(d=0;days<n;d++,days++); printf("%04d-%02d-%02d\n",y,m,d); } return 0; }
4.日期类
#include<cstdio> bool leap(int year){ return(year%4 == 0 && year%100 != 0 || year%400 == 0); } int main(){ int month[13][2]={{0,0}, {31,31}, {28,29}, {31,31}, {30,30}, {31,31}, {30,30}, {31,31}, {31,31}, {30,30}, {31,31}, {30,30}, {31,31}}; int y, m, d, n=1; int m1; scanf("%d", &m1); while(m1>0){ scanf("%d%d%d", &y, &m, &d); while(n>0){ d++; if(d==month[m][leap(y)]+1){ m++; d=1; } if(m==13){ y++; m=1; } n--; } printf("%04d-%02d-%02d\n", y, m, d); m=1, d=0, n=1; m1--; } return 0; }
进制转换
1.又一版 A+B
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); using namespace std; int main(){ long long int a,b,m,sum; int c[35],count; while(scanf("%lld",&m)!=EOF&&m){ memset(c,0,sizeof(c)); scanf("%lld %lld",&a,&b); sum = a+b; if(sum==0){ cout<<sum<<endl; continue; } count=0; while(sum>0){ c[count] = sum % m; sum /= m; count++; } for(int i = count-1;i>=0;i--){ cout<<c[i]; } cout<<endl; } return 0; }
2.数制转换
#include <iostream> #include<bits/stdc++.h> using namespace std; int main() { long int n; int a,b,l,k,i; char s1[110],s2[110]; while(scanf("%d %s %d",&a,s1,&b)!=EOF) { l=strlen(s1); memset(s2,0,sizeof(s2)); n=0; for(i=0; i<l; i++) { if(s1[i]<='9') n=n*a+s1[i]-'0'; else if(s1[i]>='A'&&s1[i]<='F') n=n*a+s1[i]-'A'+10; else if(s1[i]>='a'&&s1[i]<='f') n=n*a+s1[i]-'a'+10; } if(n==0)printf("0"); k=0; while(n) { if(n%b>9)s2[k++]='A'+n%b-10; else s2[k++]=n%b+'0'; n=n/b; } for(i=k-1; i>=0; i--)printf("%c",s2[i]); printf("\n"); } return 0; }
3.进制转换
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); using namespace std; int isclean(char a[]){ int l = strlen(a); while(l--){ if(a[l]!='0'){ return 0; } } return 1; } int main(){ char a[35]; int b[1000],count=0,l,flag,tmp; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); while(scanf("%s",a)!=EOF){ count = 0; l = strlen(a); if(isclean(a)){ cout<<"0"<<endl; continue; } while(!isclean(a)){ b[count] = (a[l-1]-'0')%2; flag = 0; for(int i = 0;i<l;i++){ tmp = a[i] - '0'; a[i] = (tmp + 10 * flag)/2 + '0'; flag = tmp % 2; } count++; } for(int i= count-1;i>=0;i--){ cout<<b[i]; } cout<<endl; } return 0; }
4.八进制
#include <stdio.h> int main(){ long long m; while(scanf("%lld",&m)!=EOF){ int a[100]; int i=0; while(m){ a[i++]=m%8; m/=8; } if(i==0){ printf("0"); } for(i=i-1;i>=0;i--){ printf("%d",a[i]); } printf("\n"); } return 0; }
字符串处理
1.字符拼接
#include <stdio.h> #include <string.h> int main() { int lena,lenb,i; char a[101]; char b[101]; char c[210]; while((scanf("%s %s",a,b))!=EOF) { lena=strlen(a); lenb=strlen(b); for(i=0; i<lena+lenb; i++) { if(i<lena) c[i]=a[i]; else c[i]=b[i-lena]; } c[lena+lenb]='\0'; printf("%s\n",c); } return 0; }
2.首字母大写
#include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> const double eps = 1e-8; const double Pi = acos(-1.0); using namespace std; int main(){ char a[110]; int l; while(cin.getline(a,100)!=NULL){ l = strlen(a); for(int i = 0;i<l-1;i++){ if(a[i] ==' '||a[i]==','||a[i]=='\t'||a[i]=='\r'||a[i]=='\t'){ if(a[i+1]-'a'>=0&&a[i+1]-'z'<=0){ a[i+1] = a[i+1] - 32; } } } if(a[0]-'a'>=0&&a[0]-'z'<=0){ a[0] = a[0] - 32; } cout<<a<<endl; } return 0; }