🐱🚀3.2小节——入门模拟->查找元素
地址合集:3.2小节——入门模拟->查找元素
问题 A: 统计同成绩学生人数
解题思路
每次读入对应的数组,从前往后进行查找统计就完事了呗。
#include<cstdio> const int maxn=1000; int a[maxn]; int main(){ int n,x; while(scanf("%d",&n) != EOF && n != 0){ for(int i=0;i<n;++i){ scanf("%d",&a[i]); } scanf("%d",&x); int count=0; for(int i=0;i<n;i++) if(a[i]==x) count++; printf("%d\n",count); } return 0; }
问题 B: 找x
#include<cstdio> const int maxn=210; int a[maxn]; 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; }
问题 C: 查找学生信息
#include<cstdio> #include<cstring> using namespace std; const int max=1010; struct student{ char id[100]; char name[100]; char sex[2]; int age; }stu[max]; int main(){ int n,m,flag; char temp[4]; 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;} flag==-1?printf("No Answer!\n"):printf("%s %s %s %d\n",stu[flag].id,stu[flag].name,stu[flag].sex,stu[flag].age); } } return 0; }
问题 D: 查找
#include<cstdio> int main(){ int n,m,temp; while(scanf("%d",&n)!=EOF){ int a[n]; for(int i=0;i<n;++i) scanf("%d",&a[i]); scanf("%d",&m); for(int i=0;i<m;++i){ scanf("%d",&temp); for(int j=0;j<n;++j){ if(a[j]==temp){ printf("YES\n"); break; } else if(j==n-1) printf("NO\n"); } } } return 0; }
问题 E: 学生查询
#include<cstdio> #include<cstring> struct student{ char id[20]; char name[20]; char sex[10]; int age; }stu[20]; int main(){ int m,n; char temp[20]; scanf("%d",&m); for(int i=0;i<m;++i){ scanf("%d",&n); for(int j=0;j<n;++j){ scanf("%s %s %s %d",&stu[j].id,&stu[j].name,&stu[j].sex,&stu[j].age); } scanf("%s",&temp); for(int j=0;j<n;++j){ if(strcmp(temp,stu[j].id)==0){ printf("%s %s %s %d\n",stu[j].id,stu[j].name,stu[j].sex,stu[j].age); break; } } } return 0; }
1041 考试座位号 (15 分)
地址:1041 考试座位号 (15 分)
#include<cstdio> typedef struct { char id[16]; int jishi; int kaoshi; }student; int main(){ int n,chan,cha; scanf("%d",&n); student stu[n]; for(int i=0;i<n;++i) scanf("%s %d %d",&stu[i].id,&stu[i].jishi,&stu[i].kaoshi); scanf("%d",&chan); while(chan--){ scanf("%d",&cha); for(int i=0;i<n;++i) if(stu[i].jishi==cha) printf("%16.16s %d\n",stu[i].id,stu[i].kaoshi); } return 0; }
1004 成绩排名 (20 分)
地址: 1004 成绩排名 (20 分)
#include<cstdio> #include<cstring> int main(){ int n,high=-1,low=101,score; char h_name[11],h_id[11],l_name[11],l_id[11],name[11],id[11]; scanf("%d",&n); while(n--){ scanf("%s%s%d",&name,&id,&score); if(score>high) {strcpy(h_name,name);strcpy(h_id,id);high=score;} if(score<low) {strcpy(l_name,name);strcpy(l_id,id);low=score;} } printf("%s %s\n%s %s\n",h_name,h_id,l_name,l_id); return 0; }
1028 人口普查 (20 分)
地址:1028 人口普查 (20 分)
#include<cstdio> #include<cstring> int main(){ int n,youxiao=0; scanf("%d",&n); char minStr[12]="9",minName[6]="",maxStr[12]="0",maxName[6]=" ",start[12]="1814/09/06",end[12]="2014/09/06",name[6],str[12]; while(n--){ scanf("%s %s",&name,&str); if(strcmp(str,start)>=0&&strcmp(str,end)<=0){//判断边界 youxiao++; if(strcmp(minStr,str)>=0) {strcpy(minStr,str);strcpy(minName,name);} if(strcmp(maxStr,str)<=0) {strcpy(maxStr,str);strcpy(maxName,name);} } } youxiao>0?printf("%d %s %s\n",youxiao,minName,maxName):printf("0\n"); return 0; }
1032 挖掘机技术哪家强 (20 分)
地址:1032 挖掘机技术哪家强 (20 分)
#include<cstdio> const int maxn = 100010; int school[maxn] = {0}; int main(){ int n, schID, score; scanf("%d", &n); for(int i = 0;i < n;i++){ scanf("%d %d", &schID, &score); school[schID] += score; } int MAX = -1, k = 0; for(int i = 1;i <= n;i++){ if(school[i] > MAX){ MAX = school[i]; k = i; } } printf("%d %d\n", k, MAX); return 0; }
1006 Sign In and Sign Out (25 分)
地址:1006 Sign In and Sign Out (25 分)
#include<cstdio> #include<cstring> int main(){ char id[16],signin[9],signout[9],firstId[16],firstSignin[9]="9",lastId[16],lastSignout[9]="00:00:00"; int n; scanf("%d",&n); while(n--){ scanf("%s %s %s",&id,&signin,&signout); if(strcmp(signin,firstSignin)<=0) {strcpy(firstId,id);strcpy(firstSignin,signin);} if(strcmp(signout,lastSignout)>=0){strcpy(lastId,id);strcpy(lastSignout,signout);} } printf("%s %s\n",firstId,lastId); return 0; }
1011 World Cup Betting (20 分)
地址:1011 World Cup Betting (20 分)
#include<cstdio> const char name[3]={'W','T','L'}; int compy(double a,double b,double c){ if(a>b){ if(a>c) return 0; else return 2; } else{ if(b>c) return 1; else return 2; } } int main(){ double game[3],fine=1.0; for(int i=0;i<3;i++){ scanf("%lf%lf%lf",&game[0],&game[1],&game[2]); int win=compy(game[0],game[1],game[2]); printf("%c ",name[win]); fine*=game[win]; } fine=(fine*0.65-1)*2; printf("%.2f\n",fine); return 0; }
1036 Boys vs Girls (25 分)
地址:1036 Boys vs Girls (25 分)
#include<cstdio> #include<cstring> struct people{ char name[11]; char gender; char id[11]; int grade; }highf={" ",'F'," ",-1},lowm={" ",'M'," ",101},temp; int main(){ int n; scanf("%d",&n); while(n--){ scanf("%s %c %s %d",&temp.name,&temp.gender,&temp.id,&temp.grade); if(temp.gender=='F'){ if(temp.grade>=highf.grade) highf=temp; } else { if(temp.grade<=lowm.grade) lowm=temp; } } highf.grade>-1?printf("%s %s\n",highf.name,highf.id):printf("Absent\n"); lowm.grade<101?printf("%s %s\n",lowm.name,lowm.id):printf("Absent\n"); if(highf.grade>-1&&lowm.grade<101) printf("%d\n",highf.grade-lowm.grade); else printf("NA\n"); return 0; }
🐱🐉3.3小节——入门模拟->图形输出
地址合集:3.3小节——入门模拟->图形输出
问题 A: 输出梯形
#include<cstdio> int main(){ int h; while(scanf("%d",&h)!=EOF){ for(int i=0;i<h;++i){ for(int j=0;j<3*h-2;j++){ if(j<2*(h-i)-2) printf(" "); else printf("*"); } printf(" \n"); } } return 0; }
问题 B: Hello World for U
#include<cstdio> #include<cstring> int main(){ char s[82]; scanf("%s",s); int len=strlen(s),n1=(len+2)/3,n2=len-2*n1+2; for(int i=0;i<n1-1;++i){ printf("%c",s[i]); for(int j=0;j<n2-2;++j) printf(" "); printf("%c\n",s[len-i-1]); } for(int i=0;i<n2;++i) printf("%c",s[n1-1+i]); printf("\n"); return 0; }
问题 C: 等腰梯形
#include<cstdio> int main(){ int n; scanf("%d",&n); for(int k=0;k<n;k++){ int h; scanf("%d",&h); for(int i=0;i<h;++i){ for(int j=0;j<h-1-i;j++) printf(" "); for(int j=0;j<h+2*i;j++) printf("*"); for(int j=0;j<h-1-i;j++) printf(" "); printf("\n"); } } return 0; }
问题 D: 沙漏图形
#include<cstdio> int main(){ int n; while(scanf("%d",&n)!=EOF){ for(int i=0;i<n;i++){ for(int j=0;j<i;j++) printf(" "); for(int j=0;j<n-i-1;j++) printf("* "); printf("*\n"); } for(int i=1;i<n;i++){ for(int j=0;j<n-i-1;j++) printf(" "); for(int j=0;j<i;j++) printf("* "); printf("*\n"); } } return 0; }
1036 跟奥巴马一起编程 (15 分)
地址:1036 跟奥巴马一起编程 (15 分)
#include<cstdio> int main(){ int n,n2; char s; scanf("%d %c",&n,&s); n2=(n+1)/2; //printf("%d",n2); for(int i=0;i<n2;i++){ for(int j=0;j<n;j++){ if(i==0||i==n2-1) printf("%c",s); else{ if(j==0||j==n-1) printf("%c",s); else printf(" "); } } printf("\n"); } return 0; }
1027 打印沙漏 (20 分)
地址:1027 打印沙漏 (20 分)
#include<cstdio> int how(int a){ a=(a+1)/2; for(int i=0;i<a+2;i++) if((i*i)>a) return i-1; return 0; } int main(){ int h; char s; scanf("%d %c",&h,&s); int n=how(h); //printf("%d",n); for(int i=0;i<n;i++){ for(int j=0;j<i;j++) printf(" "); for(int j=0;j<2*(n-i)-1;j++) printf("%c",s); printf("\n"); } for(int i=1;i<n;i++){ for(int j=0;j<n-i-1;j++) printf(" "); for(int j=0;j<2*i+1;j++) printf("%c",s); printf("\n"); } printf("%d\n",h-2*n*n+1); return 0; }
1031 Hello World for U (20 分)
地址:1031 Hello World for U (20 分)
#include<cstdio> char M[11] = {'1','0','X','9','8','7','6','5','4','3','2'}; int quan[17] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; int main(){ int n; bool flag = true; while(scanf("%d",&n) != EOF){ while(n--){ bool flag1 = true; char num[19]; int sum = 0,i; scanf("%s", num); for(i = 0; i < 17; ++i) if(num[i] <= '9' &&num[i] >= '0') sum += quan[i]*(num[i] - '0'); else break; if(M[sum % 11] == num[17] && i == 17){ continue; printf("N"); } if(flag){ printf("%s",num); flag1 = false; } else printf("\n%s",num); flag = false; } if(flag) printf("All passed"); } return 0; }
🐱👤3.4小节——入门模拟->日期处理
地址合集:3.4小节——入门模拟->日期处理
问题 A: 日期差值
#include<cstdio> 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}}; bool isLeap(int year){ return (year%4==0&&year%100!=0)||(year%400==0); }; int main(){ int time1,y1,m1,d1; int time2,y2,m2,d2; while(scanf("%d %d",&time1,&time2)!=EOF){ if(time1>time2){ int temp=time1; time1=time2; time2=temp; } y1=time1/10000;m1=time1%10000/100;d1=time1%100; y2=time2/10000;m2=time2%10000/100;d2=time2%100; int ans=1; while(y1<y2||m1<m2||d1<d2){ d1++; if(d1==month[m1][isLeap(y1)]+1){ m1++; d1=1; } if(m1==13){ y1++; m1=1; } ans++; } printf("%d\n",ans); } return 0; }
问题 B: Day of Week
#include<cstdio> #include<cstring> char week[7][20]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}; int year[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}}; char month[12][20]={"January","February","March","April","May","June","July","August","September","October","November","December"}; bool isLeap(int year){ return (year%4==0&&year%100!=0)||(year%400==0); }; int toMonth(char m[20]){ for(int i=0;i<13;i++) if(strcmp(m,month[i])==0) return i+1; return 0; } int main(){ int time1,y1,m1,d1; char m[20]; int time2,y2,m2,d2; while(scanf("%d %s %d",&d1,&m,&y1)!=EOF){ m1=toMonth(m); int flag=0; time1=d1+m1*100+y1*10000; int ans=0; if(time1>20130107){ time2=time1; time1=20130107; flag=1; } else time2=20130107; y1=time1/10000;m1=time1%10000/100;d1=time1%100; y2=time2/10000;m2=time2%10000/100;d2=time2%100; //printf("%d %d %d",y1,m1,d1); //printf("%d %d %d",y2,m2,d2); while(y1<y2||m1<m2||d1<d2){ d1++; if(d1==year[m1][isLeap(y1)]+1){ m1++; d1=1; } if(m1==13){ y1++; m1=1; } ans++; } //printf("%d %d",ans,flag); flag==0?printf("%s\n",week[6-(ans-1)%7]):printf("%s\n",week[ans%7]); } return 0; }
问题 C: 打印日期
#include<cstdio> 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 isLeap(int year){ return (year%4==0&&year%100!=0)||(year%400==0); }; int main(){ int y,m,d; while(scanf("%d %d",&y,&d)!=EOF){ int le=isLeap(y); //printf("%d",le); int ans=1; m=1; while(d>month[m][le]){ d-=month[m][le]; m++; } printf("%04d-%02d-%02d\n",y,m,d); } return 0; }
问题 D: 日期类
#include<cstdio> 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 isLeap(int year){ return (year%4==0&&year%100!=0)||(year%400==0); }; int main(){ int y,m,d,n; scanf("%d",&n); while(n--){ scanf("%d %d %d",&y,&m,&d); int le=isLeap(y); d++; int ans=1; if(d>month[m][le]){ d-=month[m][le]; m++; } if(m>12){ y++; m-=12; } printf("%04d-%02d-%02d\n",y,m,d); } return 0; }
问题 E: 日期累加
#include<cstdio> 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 isLeap(int year){ return (year%4==0&&year%100!=0)||(year%400==0); }; int main(){ int y,m,d,n,sum; scanf("%d",&n); while(n--){ scanf("%d %d %d %d",&y,&m,&d,&sum); while(sum--){ int le=isLeap(y); d++; if(d>month[m][le]){ d-=month[m][le]; m++; } if(m>12){ y++; m-=12; } } printf("%04d-%02d-%02d\n",y,m,d); } return 0; }