0:
普通的字符串替换
代码:
#include<stdio.h> #include<string.h> char unit[9][5]={"I","II","IIII","IV","V","VI","VII","VIII","IX"}; char ten[9][5]={"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}; char hun[9][5]={"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; int main(){ int n; char num[10]; // for(int k=0;k<9;k++) // printf("%s\n",ten[k]); scanf("%d",&n); while(n--){ scanf("%s",num); int de; int len=strlen(num); de=len; // printf("%d\n",len); for(int i=0;i<len;i++,de--){ if(de==4) { for(int j=0;j<num[i]-'0';j++){ printf("M"); } } else if(de==3){ printf("%s",hun[num[i]-'0'-1]); } else if(de==2){ // printf("%d %s",de,ten[8]); printf("%s",ten[num[i]-'0'-1]); // printf("%d\n",num[i]-'0'-1); } else if(de==1){ printf("%s",unit[num[i]-'0'-1]); } } puts(""); } return 0; }
1:
二维数组的运用
代码:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<vector> using namespace std; #define LL long long #define STOP system("pause") #define print(x) printf("%d\n",x); #define eps 1e-8 #define PI acos(-1.0) #define LMT 35 int mat[LMT][LMT]; int main(){ int i,j,T,im,ij,ii,n,m; int yes; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); yes=0; for(i=0;i<n;i++) for(j=0;j<m;j++) scanf("%d",&mat[i][j]); for(i=0;i<n;i++) { im=mat[i][0];ij=0; for(j=1;j<m;j++) if(im<mat[i][j]) { im=mat[i][j]; ij=j; } for(j=0;j<m;j++) if(ij!=j&&mat[i][j]==im) break; if(j<m)continue; for(ii=0;ii<n;ii++) if(mat[ii][ij]>im) break; else if(mat[ii][ij]==im&&ii!=i) break; if(ii==n) { if(yes)printf(" "); yes=1; printf("%d",im); } } if(0==yes)printf("NO\n"); else printf("\n"); } return 0; }
2:
约瑟夫环的利用,公式如下也可以用链模拟
代码:
#include<stdio.h> int work(int n,int m) { int i=0,c; for(c=1;c<=n;c++) i=(i+m-1)%c+1; return i; } int main(void) { int T,n; scanf("%d",&T); int k=T; while(T--) { scanf("%d",&n); printf("%d\n",work(n,3)); } return 0; }
3:
字符串的处理,比较麻烦,代码还没验证过,不过也差不多了。
#include<iostream> #include<vector> #include<string> #include<stdio.h> #include<string.h> using namespace std; #define N 5005 #define M 555 char str[N]; char words[M][M]; int x,y; char aim[M][M]; void getwords(){ int len=strlen(str); int i,j,k; x=0; y=0; for(i=0;i<len;i++){ if(str[i]=='.'||(str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')){ words[x][y++]=str[i]; } else { words[x][y]='\0'; x++; y=0; } } } void check(){ int i; for(i=0;i<=x;i++) printf("%s\n",words[i]); } void writeln(){ int a,b,i,j; int l=0; int num=0,k=0,ff=0; for(a=0;a<=x;){ int len =strlen(words[a]); // printf("%d\n",len); if(l+len<=20){ memcpy(aim[k],words[a],strlen(words[a])); // system("pause"); // printf("aim : %s words: %s\n",aim[k],words[a]); a++; k++; l+=len; num++; } else { int space=20-l,nspace; if(num==1) nspace=0; else { if(space%(num-1)!=0) ff=1; nspace=space/(num-1); } for(i=0;i<k-1;i++){ printf("%s",aim[i]); for(j=0;j<nspace;j++){ printf(" "); } } if(ff==1) printf(" "); ff==0; printf("%s\n",aim[k-1]); num=0; l=0; k=0; memset(aim,0,sizeof(aim)); } } ff=0; if(l!=0){ int space=20-l,nspace; if(num==1) nspace=0; else { if(space%(num-1)!=0) ff=1; nspace=space/(num-1); } for(i=0;i<k-1;i++){ printf("%s",aim[i]); for(j=0;j<nspace;j++){ printf(" "); } } // system("pause"); // printf("num: %d ff: %d k: %d",num,ff,k); if(ff==1) printf(" "); ff==0; printf("%s\n",aim[k-1]); num=0; l=0; k=0; memset(aim,0,sizeof(aim)); } } int main(){ int T; scanf("%d",&T); getchar(); while(T--){ gets(str); getwords(); // check(); writeln(); } return 0; }
4:dp.
代码:
#include<stdio.h> #include<string.h> #define LMT 25 double sum,rest[LMT],dis[LMT],cons[LMT],mon[LMT]; double v,ks,sd; int main(void) { int T,i,j,d,n; scanf("%d",&T); while(T--) { scanf("%d",&n); scanf("%lf%lf%lf",&v,&rest[0],&ks); for(i=0;i<=n+2;i++) mon[i]=-9999; for(i=1;i<=n;i++) rest[i]=v; mon[0]=0; for(i=1;i<=n;i++) { scanf("%lf%lf",&dis[i],&cons[i]); sum+=dis[i]; } scanf("%lf",&dis[n+1]); cons[n+1]=0; for(i=1;i<=n+1;i++) { sd=dis[i]; for(j=i-1;j>=0;j--) { if(sd*ks<=rest[j]) { if(mon[i]>mon[j]+(v-(rest[j]-sd*ks))*cons[i]||mon[i]<0) mon[i]=mon[j]+(v-(rest[j]-sd*ks))*cons[i]; } sd+=dis[j]; } } if(mon[n+1]>=0)printf("%.2lf\n",mon[n+1]); else printf("NO\n"); } return 0; }
5:
已知后序中序求前序.
dfs
代码:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<vector> using namespace std; #define LMT 35 #define LL long long #define STOP system("pause") #define print(x) printf("%d\n",x); #define eps 1e-8 #define PI acos(-1.0) char hou[LMT],mid[LMT],ans[LMT]; void dfs(int begin,int a,int b,int l,int r) { if(l>r||a>b)return; int sign,i; char x=hou[b]; for(i=l;i<=r&&x!=mid[i];i++); sign=i; ans[begin]=x; dfs(begin+1,a,sign-l+a-1,l,sign-1); dfs(begin+sign-l+1,sign-l+a,b-1,sign+1,r); } int main() { int T; scanf("%d",&T); while(scanf("%s%s",hou,mid)!=EOF) { memset(ans,0,sizeof(ans)); dfs(0,0,strlen(hou)-1,0,strlen(mid)-1); printf("%s\n",ans); } return 0; }
6:麻将考虑三种情况吧,实在是麻烦
尚无