《算法笔记》3.1小节——入门模拟->简单模拟
C简单,但是代码有点冗余,优化后省掉一般代码,保持优化习惯
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) { int a1=a; int b1=b; int ans=0; while(a1/10 || a1%10) { int temp=a1%10; a1=a1/10; b1=b; while(b1/10) { int temp2=b1%10; b1=b1/10; ans=ans+temp*temp2; } ans=ans+b1*temp; } printf("%d ",ans); } return 0; }
D无难点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int n; int ji,ou; int temp; //int a[1005]; while(scanf("%d",&n)!=EOF) { ji=0; ou=0; while(n--) { scanf("%d",&temp); if(temp%2) ji++; else ou++; } if(ou>ji) printf("NO "); else printf("YES "); } return 0; }
E无难点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int n,m,left,right,ans; int len[100005]; scanf("%d",&n); ans=0; for(int i=1;i<=n;i++) { scanf("%d",&len[i]); ans+=len[i]; } scanf("%d",&m); while(m--) { scanf("%d%d",&left,&right); int ans1=0,ans2=0; for(int i=min(left,right);i<=max(left,right)-1;i++) { ans1+=len[i]; } ans2=ans-ans1; printf("%d ",min(ans1,ans2)); } return 0; }
F刚开始数据开小了,注意两个int相加是爆int的,不要解决那么点空间
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int n; long long a,b,c; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%lld%lld%lld",&a,&b,&c); if(a+b>c) printf("Case #%d: true ",i); else printf("Case #%d: false ",i); } return 0; }
G题判断加标记开始写错位置啦,认真读题,还有,一位小数是%.1f不是%.f,注意
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int n; int temp; int numa4; bool a2pd; int a[6]; bool pd[6]; double ans4; while(scanf("%d",&n)!=EOF) { numa4=0; memset(a,0,sizeof(a)); a2pd=false; memset(pd,0,sizeof(pd)); while(n--) { scanf("%d",&temp); if(temp%5==0) { if(temp%2==0) { a[1]+=temp; pd[1]=true; } } else if(temp%5==1) { pd[2]=true; if(!a2pd) a[2]+=temp; else a[2]-=temp; a2pd=!a2pd; } else if(temp%5==2) { pd[3]=true; a[3]++; } else if(temp%5==3) { pd[4]=true; a[4]+=temp; numa4++; } else { pd[5]=true; if(temp>a[5]) a[5]=temp; } } ans4=a[4]*1.0/numa4; // printf("%.1f",a[4]*1.0/numa4); for(int i=1;i<=5;i++) { if(pd[i]==false) printf("N "); else if(i!=4)printf("%d ",a[i]); else printf("%.1f ",ans4); }printf(" "); } return 0; }
H无难点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int a,b,da,db; int ansa,ansb; while(scanf("%d%d%d%d",&a,&da,&b,&db)!=EOF) { ansa=0; ansb=0; while(a) { if(a%10==da) { ansa=ansa*10; ansa+=da; } a=a/10; } while(b) { if(b%10==db) { ansb=ansb*10; ansb+=db; } b=b/10; } printf("%d ",ansa+ansb); } return 0; }
I是最有收获的一道题,if(a)在a不等于0的时候全部为真!!不仅仅是在大于!!!还有后面手误改了好久~题本身不难的啦;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int ans1[5]; int ans2[5]; int jishu1[30]; int jishu2[30]; int pd(char a1,char a2)//1大0平-1负 { if(a1=='C') { if(a2=='C') return 0; if(a2=='J') return 1; return -1; } if(a1=='J') { if(a2=='C') return -1; if(a2=='J') return 0; return 1; } if(a2=='C') return 1; if(a2=='J') return -1; return 0; } int main() { int n; scanf("%d",&n); while(n--) { char c1,c2; getchar(); scanf("%c",&c1); getchar(); scanf("%c",&c2); //printf("%d ",pd(c1,c2)); if(pd(c1,c2)>0) jishu1[c1-65]++; else if(pd(c1,c2)<0) jishu2[c2-65]++; //printf("%d %d %d ",jishu2['C'-65],jishu2['J'-65],jishu2['B'-65]); ans1[pd(c1,c2)+1]++; ans2[pd(c2,c1)+1]++; } printf("%d %d %d ",ans1[2],ans1[1],ans1[0]); printf("%d %d %d ",ans2[2],ans2[1],ans2[0]); char maxchar1='C';char maxchar2='C'; if(jishu1['B'-65]>=jishu1[maxchar1-65]) maxchar1='B'; if(jishu1['J'-65]>jishu1[maxchar1-65]) maxchar1='J'; if(jishu2['B'-65]>=jishu2[maxchar1-65]) maxchar2='B'; if(jishu2['J'-65]>jishu2[maxchar1-65]) maxchar2='J'; printf("%c %c ",maxchar1,maxchar2); return 0; }
《算法笔记》3.2小节——入门模拟->查找元素
A无难点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int n; int a[10000]; int ans,score; while(scanf("%d",&n) && n!=0) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); ans=0; scanf("%d",&score); for(int i=1;i<=n;i++) { if(a[i]==score) ans++; } printf("%d ",ans); } return 0; }