题解仅供学习参考使用
抄袭、复制题解,以达到刷 AC 率/AC 数量或其他目的的行为,在 WZOI 是严格禁止的。
WZOI 非常重视学术诚信 。此类行为将会导致您被老师物理攻击 。 具体细则以老师要求为准。
文章有亿点长,建议使用页内跳转快速找到题目 Top Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 9 Page 10 Page 11 Update
共有
个人访问了我的主页
Page 1 1.Hello World 1 2 3 int main () { cout<<"Hello World!" <<endl; }
2.打印very good 1 2 3 int main () { cout<<"**************************\n Very Good!\n**************************\n" <<endl; }
3.默写数字 1 2 3 4 5 int main () { int a; cin>>a; cout<<a; }
4.A+B Problem 1 2 3 4 5 6 int main () { int a,b; cin>>a>>b; cout<<a+b<<endl; return 0 ; }
5.输出第二个整数 1 2 3 4 5 6 int main () { int a,b; cin>>a>>a>>b; cout<<a; return 0 ; }
6.求一个数的前后两个数 1 2 3 4 5 6 int main () { int a,b; cin>>a>>b; cout<<a-b<<endl<<a+b; return 0 ; }
7.竖式计算 1 2 3 4 5 6 int main () { int a,b; cin>>a>>b; printf ("%10d\n +%6d\n-----------\n%10d\n" , a,b,a+b); return 0 ; }
8.植树造林 1 2 3 4 5 6 int main () { int a,b; cin>>a>>b; cout<<a*b<<endl; return 0 ; }
9.求自行车总价 1 2 3 4 5 6 int main () { int a,b; cin>>b; cout<<300 *b<<endl; return 0 ; }
10.正方形面积 1 2 3 4 5 6 7 int main () { int n; cin>>n; cout<<n*n<<endl; return 0 ; }
11.三位数 1 2 3 4 5 6 7 int main () { int a,b,c; cin>>b; a = b + 1 ; cout<<a<<b<<a * 2 <<endl; return 0 ; }
12.做蛋糕 1 2 3 4 5 int main () { int a1,a2,a3,b1,b2,b3; cin>>a1>>a2>>a3>>b1>>b2>>b3; cout<<a1*b1+a2*b2+a3*b3<<endl; }
13.计算(a+b)*c的值 1 2 3 4 5 int main () { int a,b,c; cin>>a>>b>>c; cout<<(a+b)*c; }
14.求余数 1 2 3 4 5 int main () { int a,b; cin>>a>>b; cout<<a%b; }
15.计算(a+b)/c的值 1 2 3 4 5 int main () { int a,b,c; cin>>a>>b>>c; cout<<(a+b)/c; }
16.小玉买文具 1 2 3 4 5 int main () { int a,b; cin>>a>>b; cout<<(a*10 +b)/19 ; }
17.买苹果 1 2 3 4 5 int main () { int a,b; cin>>a; cout<<(a/3 )*4 +a%3 ; }
18.买牛奶 1 2 3 4 5 int main () { int a,b,c,d; cin>>a>>b>>c>>d; cout<<d/a*a/b*c+(d/a); }
19.小鱼的游泳时间 1 2 3 4 5 int main () { int a,b,c,d; cin>>a>>b>>c>>d; cout<<((c-a)*60 +d-b)/60 <<" " <<((c-a)*60 +d-b)%60 ; }
20.周长与面积 1 2 3 4 5 int main () { int a,b; cin>>a>>b; cout<<"perimeter=" <<(a+b)*2 <<endl<<"area=" <<a*b; }
21.求筝形面积 1 2 3 4 5 int main () { long long a,b; cin>>a>>b; cout<<a*b; }
22.求正立方体表面积 1 2 3 4 5 int main () { int a; cin>>a; cout<<a*a*6 ; }
23.输出保留3位小数的浮点数 1 2 3 4 5 int main () { double x; cin>>x; cout<<fixed<<setprecision (3 )<<x; }
24.工资 1 2 3 4 5 6 int main () { int x,y; double z; cin>>x>>y>>z; cout<<"NUMBER = " <<x<<endl<<fixed<<setprecision (2 )<<"SALARY = U$ " <<y*z; }
25.四舍五入 1 2 3 4 5 6 int main () { int y; double x; cin>>x>>y; cout<<fixed<<setprecision (y)<<x; }
26.求梯形面积 1 2 3 4 5 int main () { double a,b,c; cin>>a>>b>>c; cout<<fixed<<setprecision (2 )<<(a+b)*c/2 ; }
27.梯形面积 1 2 3 4 int main () { double a; cout<<fixed<<setprecision (2 )<<160.0 *2 /15 *(15 +25 )/2 ; }
28.求圆柱体表面积 1 2 3 4 5 int main () { double r,h; cin>>r>>h; cout<<fixed<<setprecision (2 )<<2 *(3.14 *r*r)+2 *(3.14 *r*h); }
29.计算总成绩与平均成绩 1 2 3 4 5 int main () { double a,b,c,d; cin>>a>>b>>c>>d; cout<<fixed<<setprecision (2 )<<a+b+c+d<<endl<<(a+b+c+d)/4 ; }
30.新平均成绩 1 2 3 4 5 int main () { double x,score,n; cin>>x>>score>>n; cout<<fixed<<setprecision (3 )<<(x*n-score)/(n-1 ); }
31.歌手大奖赛 注意题目输出需要6.2lf或者setw(6) 1 2 3 4 int main () { double score; printf ("%6.2lf" ,((9.6 *6 )-((9.6 *12 )-(9.4 *5 )-(9.8 *5 )))/4 ); }
32.奇葩的饭店 1 2 3 4 5 int main () { double a,b; cin>>a>>b; cout<<fixed<<setprecision (4 )<<b/(a/100 ); }
33.猜数游戏 1 2 3 4 5 int main () { int x; cin>>x; cout<<(x*1000 +x)/7 /11 /13 ; }
34.求根号x的值 1 2 3 4 5 int main () { int x; cin>>x; cout<<fixed<<setprecision (3 )<<sqrt (x); }
35.求logx 1 2 3 4 5 int main () { double x; cin>>x; cout<<fixed<<setprecision (3 )<<log (x)<<endl<<log10 (x)<<endl<<log2 (x); }
36.求两者较大值的max函数的用法 1 2 3 4 5 6 int main () { char c; int a,b; cin>>a>>c>>b; cout<<"max=" <<max (a,b); }
37.求自行车和三轮车各是多少辆 1 2 3 4 5 int main () { int a,b; cin>>a>>b; cout<<(a*3 -b)/(3 -2 )<<" " <<(b-a*2 )/(3 -2 ); }
38.鸡兔同笼 1 2 3 4 5 int main () { int x,y,m,n; cin>>x>>y; cout<<(x-(y*2 ))/(4 -2 )<<" " <<((y*4 )-x)/(4 -2 ); }
39.计算路程 1 2 3 4 5 int main () { double v,t; cin>>v>>t; cout<<fixed<<setprecision (2 )<<v*t; }
40.带余除法 1 2 3 4 5 int main () { int a,b; cin>>a>>b; cout<<a/b<<" " <<a%b; }
41.交换门牌号 1 2 3 4 5 int main () { int s; cin>>s; cout<<s%10 <<s/10 ; }
42.密码 1 2 3 4 5 6 int main () { long long m,s=1 ,i; cin>>m; for (i=m;i>m-3 ;i--)s*=i; cout<<s; }
43.浮点数向零舍入 1 2 3 4 5 int main () { double n; cin>>n; cout<<int (n); }
44.计算浮点数相除的余数 1 2 3 4 5 int main () { double a,b; cin>>a>>b; cout<<fixed<<setprecision (4 )<<a-b*int (a/b) ; }
45.整型数据类型存储空间大小 1 2 3 4 5 6 int main () { long long a; int b; short c; cout<<sizeof (a)<<" " <<sizeof (b)<<" " <<sizeof (c); }
46.浮点型数据类型存储空间大小 1 2 3 4 5 int main () { float a; double b; cout<<sizeof (a)<<" " <<sizeof (b); }
47.其他基本数据类型存储空间大小 1 2 3 4 5 int main () { bool a; char c; cout<<sizeof (a)<<" " <<sizeof (c); }
48.空格分隔输出 1 2 3 4 5 int main () { char a; int b; float c; double d; cin>>a>>b>>c>>d; cout<<fixed<<setprecision (6 )<<a<<" " <<b<<" " <<c<<" " <<d<<endl; }
49.整型与布尔型的转换 1 2 3 4 5 int main () { int a; bool b; cin>>a; cout<<int (bool (a)); }
50.字符三角形 1 2 3 4 5 int main () { char a; cin>>a; cout<<" " <<a<<endl<<" " <<a<<a<<a<<endl<<a<<a<<a<<a<<a<<endl; }
51.字符菱形 1 2 3 4 5 int main () { char a; cin>>a; cout<<" " <<a<<endl<<" " <<a<<a<<a<<endl<<a<<a<<a<<a<<a<<endl<<" " <<a<<a<<a<<endl<<" " <<a<<endl; }
52.读取单个字符的getchar()函数 1 2 3 4 5 int main () { char c; c=getchar (); cout<<c<<endl; }
53.打印ASCII码 1 2 3 4 5 int main () { char a; cin>>a; cout<<int (a); }
54.打印字符 1 2 3 4 5 int main () { int b; cin>>b; cout<<char (b); }
55.大写字母转小写字母 1 2 3 4 5 int main () { char a; cin>>a; cout<<char (a+32 ); }
56.特殊输出 1 2 3 4 5 int main () { int a,b; cin>>a>>b; printf ("%*d\n%0*d\n%-*d\n" ,b,a,b,a,b,a); }
57.特殊输出2 1 2 3 4 5 6 int main () { double a; int b; cin>>a>>b; printf ("%*.3lf\n%0*.3lf\n%-*.3lf\n" ,b,a,b,a,b,a); }
58.字符变换 1 2 3 4 5 6 int main () { char c1,c2,c3; int a1,a2,a3; cin>>c1>>c2>>c3>>a1>>a2>>a3; cout<<char (c1+pow (2 ,a1))<<" " <<char (c2+pow (2 ,a2))<<" " <<char (c3+pow (2 ,a3))<<" " ; }
59.加密算法 1 2 3 4 int main () { char a; cin>>a; cout<<char (122 -a+97 ); }
60.甲流疫情死亡率 1 2 3 4 int main () { double a,b; cin>>a>>b; cout<<fixed<<setprecision (3 )<<b/(a/100 )<<"%" ; }
61.计算多项式的值 1 2 3 4 5 int main () { double a,b,c,d,x; cin>>x>>a>>b>>c>>d; cout<<fixed<<setprecision (7 )<<a*x*x*x+b*x*x+c*x+d; }
62.数列取数 1 2 3 4 5 int main () { long long a,b,c; cin>>a>>b>>c; cout<<a*2 +b*4 +c*8 ; }
63.填充矩形 注意要开 long long
1 2 3 4 5 int main () { int n,m,a,b,c; cin>>n>>m>>a; cout<<1ll *int (n/a)*int (m/a); }
64.大象喝水 1 2 3 4 5 int main () { int h,r; cin>>h>>r; cout<<ceil (20000 /(3.14159 *r*r*h)); }
65.求等差为3的数列 1 2 3 4 5 int main () { int a; cin>>a; cout<<(2 +(a-1 )*3 +2 )*a/2 ; }
66.等差数列末项计算 1 2 3 4 5 int main () { int a,b,c; cin>>a>>b>>c; cout<<(b-a)*(c-1 )+a; }
67.计算线段长度 1 2 3 4 5 6 int main () { int Xa,Ya,Xb,Yb; double a; cin>>Xa>>Ya>>Xb>>Yb; cout<<fixed<<setprecision (3 )<<double (abs (sqrt ((Xb-Xa)*(Xb-Xa)+(Yb-Ya)*(Yb-Ya)))); }
68.计算折线长度 1 2 3 4 5 6 7 8 int main () { double Xa,Ya,Xb,Yb,Xc,Yc,a=0 ; cin>>Xa>>Ya>>Xb>>Yb>>Xc>>Yc; a+=sqrt (abs ((0 -Xa)*(0 -Xa)+(0 -Ya)*(0 -Ya))); a+=sqrt (abs ((Xb-Xa)*(Xb-Xa)+(Yb-Ya)*(Yb-Ya))); a+=sqrt (abs ((Xc-Xb)*(Xc-Xb)+(Yc-Yb)*(Yc-Yb))); cout<<fixed<<setprecision (5 )<<a; }
69.送午饭 1 2 3 4 5 6 7 8 9 int main () { int x1,y1,x2,y2,h,v; double a=0 ; cin>>h>>v>>x1>>y1>>x2>>y2; a+=abs (sqrt ((x1-0 )*(x1-0 )+(y1-0 )*(y1-0 ))); a+=abs (sqrt ((x2-0 )*(x2-0 )+(y2-0 )*(y2-0 ))); a+=h;a/=v; cout<<fixed<<setprecision (2 )<<a; }
70.计算曼哈顿距离 1 2 3 4 5 6 int main () { double x1,y1,x2,y2,a=0 ; cin>>x1>>y1>>x2>>y2; a=abs (x1-x2)+abs (y1-y2); cout<<fixed<<setprecision (3 )<<abs (x1-x2)+abs (y1-y2); }
71.计算球的体积 1 2 3 4 5 int main () { double x; cin>>x; cout<<fixed<<setprecision (2 )<<4.0 /3 *3.14 *x*x*x; }
72.计算并联电阻的阻值 1 2 3 4 5 int main () { double r1,r2; cin>>r1>>r2; cout<<fixed<<setprecision (2 )<<1 /(1 /r1+1 /r2); }
73.求面积差 寻求老师的帮忙…… 1 2 3 int main () { cout<<0.1642 ; }
74.整数反转 第40题的加强版 1 2 3 4 5 int main () { char a,b,c; cin>>a>>b>>c; cout<<c<<b<<a; }
75.变量交换 swap()函数 1 2 3 4 5 6 int main () { int a,b; cin>>a>>b; swap (a,b); cout<<a<<" " <<b; }
76.交换三个字符并输出 1 2 3 4 5 int main () { char a,b,c; cin>>a>>b>>c; cout<<c<<b<<a; }
77.计算塔高 寻求老师的帮助…… 1 2 3 4 5 6 7 8 9 int main () { double l,a,b,m; const double PI=3.14159 ; cin>>l>>a>>b; a=a/180 *PI; b=b/180 *PI; m=l/(1 /tan (b)-1 /tan (a)); printf ("%.3f" , m); }
78.判断奇偶 1 2 3 4 5 6 int main () { int a; cin>>a; if (a%2 ==0 )cout<<"even" ; else cout<<"odd" ; }
79.判断是否被5整除 1 2 3 4 5 6 int main () { int a,b,c; cin>>a>>b>>c; if (a%5 ==0 && b%5 ==0 && c%5 ==0 )cout<<"Y es." ; else cout<<"No_" ; }
80.判断一个数能否同时被3和5整除 1 2 3 4 5 6 int main () { int n; cin>>n; if (n%5 ==0 &&n%3 ==0 )cout<<"YES" ; else cout<<"NO" ; }
81.打车费用 1 2 3 4 5 6 7 8 9 int main () { double lucheng,shijian,feiyong=0 ; cin>>lucheng>>shijian; if (lucheng>10 )feiyong=11 +(10 -2 )*1.8 +(lucheng-10 )*1.8 *1.5 ; else if (lucheng>2 )feiyong=11 +(lucheng-2 )*1.8 ; else feiyong=11 ; feiyong+=int (shijian/3 ); cout<<fixed<<setprecision (1 )<<feiyong; }
82.登录程序 1 2 3 4 5 6 7 8 int main () { int username=0 ,password=0 ; cin>>username>>password; if (username==202201 ) if (password==123456 )cout<<"welcome!" ; else cout<<"wrong password!" ; else cout<<"wrong user!" ; }
83.旅游景点 1 2 3 4 5 6 7 int main () { double m; cin>>m; if (m<1.2 ) cout<<"free" ; else if (m>=1.2 && m<=1.5 ) cout<<"half-price ticket" ; else if (m>1.5 ) cout<<"full ticket" ; }
84.小明学编程 1 2 3 4 5 6 7 int main () { int day; char s; cin>>day>>s; if (day==3 &&s=='N' ||day==6 &&s=='N' )puts ("Happy" ); else puts ("Sad" ); return 0 ; }
85.字符判断 1 2 3 4 5 6 7 8 9 char c;signed main () { scanf ("%c" ,&c); if (isupper (c))puts ("capital" ); else if (islower (c))puts ("lowercase" ); else if (isdigit (c))puts ("number" ); else puts ("others" ); return 0 ; }
86.判断超速 1 2 3 4 5 6 int main () { double s,t; cin>>s>>t; if (3600 /t*s>=100 ) cout<<fixed<<setprecision (2 )<<"yes" <<endl<<3600 /t*s; else cout<<"no" ; }
87.区间测速 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { double s,t; cin>>s>>t; if (3600 /t*s>=100 ){ cout<<fixed<<setprecision (2 ); if (3600 /t*s>100 && 3600 /t*s<120 ) cout<<"<20%" ; else if (3600 /t*s>=120 && 3600 /t*s<150 ) cout<<"<50%" ; else if (3600 /t*s>=150 && 3600 /t*s<170 ) cout<<"<70%" ; else if (3600 /t*s>=170 ) cout<<">=70%" ; cout<<endl<<3600 /t*s; } else cout<<"no" ; }
88.收集瓶盖赢大奖 1 2 3 4 5 6 int main () { int a,b; cin>>a>>b; cout<<(a>=10 ||b>=20 ); return 0 ; }
89.判断闰年 1 2 3 4 5 6 int main () { int nian; cin>>nian; if (nian%4 ==0 &&nian%100 ||nian%400 ==0 ) cout<<"Y" ; else cout<<"N" ; }
90.奇偶ASCII值判断 1 2 3 4 5 6 int main () { char a; cin>>a; if (int (a)%2 ==0 )cout<<"NO" ; else cout<<"YES" ; }
91.整数大小比较 1 2 3 4 5 6 7 int main () { long long a,b; cin>>a>>b; if (a>b) cout<<">" ; else if (a==b) cout<<"=" ; else if (a<b) cout<<"<" ; }
88.判断是否为两位数 1 2 3 4 5 int main () { long long a; cin>>a; cout<<a>=10 &&a<=99 ; }
92.判断是否为两位数 1 2 3 4 5 6 int main () { long long a; cin>>a; if (a>=10 && a<=99 )cout<<"1" ; else cout<<"0" ; }
93.测量体温 1 2 3 4 5 6 int main () { double t; cin>>t; if (t>=36.5 && t<=37.5 ) cout<<"Yes" ; else cout<<"No" ; }
94.奇数偶数 1 2 3 4 5 6 int main () { int a; cin>>a; if (a%2 ==0 )cout<<a-1 ; else cout<<a*2 ; }
95.小明当考官 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { int a,c,d,sum; char b; cin>>a>>b>>c>>d; switch (b){ case '+' :sum=a+c;break ; case '-' :sum=a-c;break ; case '*' :sum=a*c;break ; case '/' :sum=a/c;break ; case '%' :sum=a%c;break ; } if (sum==d)cout<<"right" ; else cout<<sum; }
96.个税改革 1 2 3 4 5 6 7 8 9 10 11 12 13 14 double a,b,sum,tax;int main () { cin>>a>>b; sum=a-b-5000 ; if (sum<=0 )tax=0 ; else if (sum<=1500 )tax=sum*0.03 ; else if (sum<=4500 )tax=sum*0.1 -105 ; else if (sum<=9000 )tax=sum*0.2 -555 ; else if (sum<=35000 )tax=sum*0.25 -1005 ; else if (sum<=55000 )tax=sum*0.3 -2755 ; else if (sum<=80000 )tax=sum*0.35 -5505 ; else tax=sum*0.45 -13505 ; printf ("%.2lf" ,tax); }
97.计算BMI指数 1 2 3 4 5 6 7 8 9 int main () { int kg; double m,BMI; cin>>kg>>m; BMI=kg/m/m; if (BMI<18.5 ) cout<<"thin!" ; else if (BMI>=18.5 &&BMI<=25 ) cout<<"normal!" ; else cout<<"fat!" ; }
98.运动打卡 1 2 3 4 5 6 7 8 9 int main () { int h; cin>>h; if (h<0 ) cout<<"error!" ; else if (h<30 ) cout<<"0" ; else if (h<45 ) cout<<"300" ; else if (h<60 ) cout<<"450" ; else cout<<"500" ; }
99.刷电视剧 1 2 3 4 5 6 7 8 int n,m,num,sum=46 ,ans;int main () { cin>>n>>m,num=n*60 ; while (1 ){ if (sum<=0 )break ; ans++,sum-=num/m; } cout<<ans; return 0 ; }
100.21点扑克牌之策略 1 2 3 4 5 6 7 8 9 10 11 12 int a,sum;int main () { for (int i=0 ;i<5 ;i++){ cin>>a,sum+=a; if (sum>21 ){ cout<<"Bomb" ; break ; } else if (sum>=16 ){ cout<<sum; break ; } } if (sum<16 )cout<<sum; return 0 ; }
Page 2 101.星期几 1 2 3 4 5 6 7 8 9 10 11 12 int main () { int h; cin>>h; if (h<1 ||h>7 )cout<<"input error!" ; else if (h==1 ) cout<<"Monday" ; else if (h==2 ) cout<<"Tuesday" ; else if (h==3 ) cout<<"Wednesday" ; else if (h==4 ) cout<<"Thursday" ; else if (h==5 ) cout<<"Friday" ; else if (h==6 ) cout<<"Saturday" ; else cout<<"Sunday" ; }
102.晶晶赴约会 1 2 3 4 5 6 int main () { int d; cin>>d; if (d==1 ||d==3 ||d==5 )cout<<"NO" ; else cout<<"YES" ; }
103.买铅笔 1 2 3 4 5 6 7 8 9 int main () { int a1,a2,b1,b2,c1,c2,am,bm,cm; double n; cin>>n>>a1>>a2>>b1>>b2>>c1>>c2; am=ceil (n/a1)*a2; bm=ceil (n/b1)*b2; cm=ceil (n/c1)*c2; cout<<min (min (am,bm),cm); }
104.过山车 1 2 3 4 5 6 7 8 9 int main () { int h1,h2,a,b,c,x=0 ; cin>>h1>>h2>>a>>b>>c; if (a<h1 && a>h2)x++; if (b<h1 && b>h2)x++; if (c<h1 && c>h2)x++; if (x)cout<<x; else cout<<-1 ; }
105.拯救暴风城 1 2 3 4 5 6 int main () { int n,x,m; cin>>n>>x>>m; if (x*m>=n) cout<<"juruo pks!" ; else cout<<"ruoji cchuan!" ; }
106.次方比较 1 2 3 4 5 6 7 int main () { int a,b,c; cin>>a>>b>>c; if (pow (a,b)<c) cout<<"Pks dalao!" ; else if (pow (a,b)==c) cout<<"pKs dalao!!" ; else cout<<"pkS dalao!!!" ; }
107.托运价格 1 2 3 4 5 6 7 8 9 int main () { double w,a,b; scanf ("%lf,%lf" ,&w,&a); w*=1000 ; if (w==0 )b=0 ; else if (w<=10 )b=2.33 ; else if (w>10 )b=2.33 +(w-10 )*1.165 ; cout<<fixed<<setprecision (3 )<<b<<endl<<a-b; }
108.计算甜甜圈面积 1 2 3 4 5 6 int main () { long double a,R,r; cin>>R>>r; if (R<=r)cout<<0 ; else cout<<fixed<<setprecision (11 )<<3.14159265359 *(pow (R,2 )-pow (r,2 )); }
109.苹果和虫子 1 2 3 4 5 6 7 8 int main () { int n,x,y,b; cin>>n>>x>>y; b=y/x; if (y%x!=0 )b+=1 ; if (n-b<0 )b=n; cout<<n-b; }
110.计算比例 1 2 3 4 5 6 7 int main () { double a,na,b,nb,c,nc,x,y; cin>>a>>na>>b>>nb>>c>>nc; x=(na-a)/a,y=(na-a+nb/(1 +b)*b+c)/(a+nb/(1 +b)+nc-c); if (x>=y)printf ("%.2f" ,x-y); else cout<<"tan90" ; }
111.选举 1 2 3 4 5 6 7 int main () { int n,m,a=0 ,b=0 ; for (int i=0 ;i<4 ;i++) cin>>n>>m,a+=n,b+=m; if (a>b)cout<<"qindingla!" ; else cout<<"too naive!" ; }
112.说谎 1 2 3 4 5 6 7 8 9 10 11 int main () { bool a,b,c,d,a1,a2,a3,a4; cin>>a>>b>>c>>d,a4=1 ; if (a4==d)a3=1 ; else a3=0 ; if (a3==c)a2=1 ; else a2=0 ; if (a2==b)a1=1 ; else a1=0 ; cout<<a1<<" " <<a2<<" " <<a3<<" " <<a4; }
113.装修 1 2 3 4 5 6 int main () { int n,m,k; cin>>n>>m>>k; if (m<k)cout<<m*n; else cout<<k*n; }
114.英雄战士 1 2 3 4 5 6 int main () { int a,b,m,h; cin>>a>>b>>m>>h; if (min (h,m)>=min (a,b))cout<<"terrorist win" ; else cout<<"counter terrorist win" ; }
115.追赶问题 1 2 3 4 5 6 7 8 int main () { int v1,v2; double x,t; cin>>x>>v1>>v2>>t; cout<<fixed<<setprecision (2 ); if (x/(v1-v2)<t)cout<<"Yes" <<" " <<x/(v1-v2); else cout<<"No" ; }
116.撕票 1 2 3 4 5 6 int main () { long long time,ph,n,i; cin>>time>>ph>>n; if (n*time>ph)cout<<"\"PKS has been killed!\"" ; else cout<<"\"You have been killed by PKS!\"" ; }
117.四叶玫瑰 1 2 3 4 5 6 7 int main () { int ge,shi,bai,qian,shu; cin>>shu; qian=shu/1000 ,bai=(shu/10 )%10 ,shi=(shu/100 )%10 ,ge=shu%10 ; if (pow (qian,4 )+pow (bai,4 )+pow (shi,4 )+pow (ge,4 )==shu)cout<<"yes" ; else cout<<"no" ; }
118.判断分数 将 x,y 同时除以 m 可以得到两者分别通过的题目。容易发现 a,b 两者是互相独立的。 打勾的数量如果均与获得的分数相同则可能答案全都正确,此时不能获得分数,输出 2333。 否则答案为最少会错开的题目数量 × m
1 2 3 4 5 6 int main () { long long n,m,a,x,b,y; cin>>n>>m>>a>>x>>b>>y; if (a==x/m&&b==y/m)cout<<2333 ; else cout<<max (abs (a-x/m),abs (b-y/m))*m; }
119.天命数 1 2 3 4 5 6 7 8 9 int main () { long long k,n,m,shu; cin>>n>>m>>k; if (k==1 )shu=max (n,m)/min (n,m); if (k==2 )shu=n+m; if (k==3 )shu=(max (n,m)/min (n,m)*(n+m))/max (n,m)%min (n,m); if (shu==0 )cout<<"cangtianyisi,huangtiandangli" ; else cout<<shu; }
120.分段函数 1 2 3 4 5 6 7 int main () { int x; cin>>x; if (x>0 )cout<<1 <<endl; else if (x==0 )cout<<0 <<endl; else if (x<0 )cout<<-1 <<endl; }
121.商场折扣 1 2 3 4 5 6 7 8 9 10 int main () { double x; cin>>x; cout<<fixed<<setprecision (2 ); if (x<250 )cout<<x; if (x>=250 && x<500 )cout<<x*0.95 ; if (x>=500 && x<1000 )cout<<x*0.9 ; if (x>=1000 && x<2000 )cout<<x*0.85 ; if (x>=2000 )cout<<x*0.8 ; }
122.成绩评定 1 2 3 4 5 6 7 int main () { int a; cin>>a; if (a>=85 )cout<<"A" ; else if (a>=60 &&a<85 )cout<<"P" ; else cout<<"E" ; }
123.求方程的解(简化版) 1 2 3 4 5 6 7 8 9 int main () { double x1,x2,A,B,C,disc; cin>>A>>B>>C; disc=B*B-4 *A*C; if (disc>0 ) x1=(-B+sqrt (disc))/(2 *A),x2=(-B-sqrt (disc))/(2 *A), cout<<fixed<<setprecision (2 ), cout<<max (x1,x2)<<" " <<min (x1,x2)<<endl; }
124.求直角梯形最长对角线 1 2 3 4 5 6 int main () { long double a,b,c,d,e; cin>>a>>b>>c; d=max (a,b),e=sqrt (d*d+c*c); cout<<fixed<<setprecision (1 )<<e; }
125.重组最大数 1 2 3 4 5 6 7 8 9 int main () { int x,a,b,c; cin>>x; a=x/100 ,b=(x/10 )%10 ,c=x%10 ; for (x=0 ;x<2 ;x++){ if (b>a)swap (a,b); if (c>b)swap (b,c); } cout<<a<<b<<c; }
126.求三角形面积 1 2 3 4 5 6 7 8 9 int main () { double a,b,c,p,s; cin>>a>>b>>c; if (a+b>c&&a+c>b&&b+c>a) { p=(a+b+c)/2 , s=sqrt (p*(p-a)*(p-b)*(p-c)), cout<<fixed<<setprecision (2 )<<s; }else cout<<"Can't" ; }
127.计算邮件收费 1 2 3 4 5 6 7 int main () { double kg,sum=0 ; cin>>kg; if (kg>1 ) sum=1.5 +(kg-1 )*0.8 ; else sum=1.5 *kg; cout<<fixed<<setprecision (2 )<<sum; }
128.称号 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int main () { int n; cin>>n; if (n>=0 && n<3 )n=2 ; switch (n){ case 2 : cout<<"You are a Foolish Man." ; break ; case 3 : cout<<"You are on a Killing Spree." ; break ; case 4 : cout<<"You are Dominating." ; break ; case 5 : cout<<"You have a Mega-Kill." ; break ; case 6 : cout<<"You are Unstoppable." ; break ; case 7 : cout<<"You are Wicked Sick." ; break ; case 8 : cout<<"You have a M-m-m-m....Monster Kill." ; break ; case 9 : cout<<"You are Godlike." ; break ; default : cout<<"You are Beyond Godlike (Somebody kill him!)." ; } }
129.有一门课不及格的学生 1 2 3 4 5 int main () { int a,b; cin>>a>>b; cout<<int (a<60 &&b>=60 ||a>=60 &&b<60 ); }
130.计算火柴棒个数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int main () { int x; cin>>x; switch (x){ case 0 : cout<<"6" ; break ; case 1 : cout<<"2" ; break ; case 2 : cout<<"5" ; break ; case 3 : cout<<"5" ; break ; case 4 : cout<<"4" ; break ; case 5 : cout<<"5" ; break ; case 6 : cout<<"6" ; break ; case 7 : cout<<"3" ; break ; case 8 : cout<<"7" ; break ; default :cout<<"6" ; } }
131.最大数输出 1 2 3 4 5 6 7 int main () { int a,b,c,d; cin>>a>>b>>c; d=max (a,b); d=max (d,c); cout<<d; }
132.点和正方形的关系 1 2 3 4 5 6 int main () { int x,y; cin>>x>>y; if (x<=1 && x>=-1 && y<=1 && y>=-1 )cout<<"yes" ; else cout<<"no" ; }
133.三角形判断 1 2 3 4 5 6 int main () { int a,b,c,d; cin>>a>>b>>c; if (a+b>c && a+c>b && b+c>a)cout<<"yes" ; else cout<<"no" ; }
134.识别三角形 1 2 3 4 5 6 7 8 9 int main () { int a,b,c,d; cin>>a>>b>>c; if (a+b<=c || a+c<=b || b+c<=a)cout<<"NO" ; else if (a==b && b==c)cout<<"Equilateral" ; else if (a==b&&a!=c || a==c&&a!=b || b==c&&c!=a)cout<<"Isosceles" ; else if (pow (a,2 )+pow (b,2 )==pow (c,2 ) || pow (a,2 )+pow (c,2 )==pow (b,2 ) || pow (b,2 )+pow (c,2 )==pow (a,2 ))cout<<"Right" ; else cout<<"General" ; }
135.骑车与走路 1 2 3 4 5 6 7 8 int main () { double a,b,c; cin>>a; b=a/3.0 +27 +30 ; c=a/1.2 ; if (b>c)cout<<"Walk" ; else cout<<"Bike" ; }
136.分段函数 1 2 3 4 5 6 7 8 int main () { double x,y; cin>>x; if (x<5 && x>=0 )y=-x+2.5 ; else if (x>=5 && x<10 )y=2 -1.5 *(x-3 )*(x-3 ); else y=x/2 -1.5 ; cout<<fixed<<setprecision (3 )<<y; }
137.简单算术表达式求值 1 2 3 4 5 6 7 8 9 10 int main () { long long a,b; char c; cin>>a>>c>>b; if (c=='+' )cout<<a+b; if (c=='-' )cout<<a-b; if (c=='*' )cout<<a*b; if (c=='/' )cout<<a/b; if (c=='%' )cout<<a%b; }
138.计算器(简易版) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { long long a,b; char c; cin>>a>>b; cin>>c; if (c=='/' && b==0 ) cout<<"Error!" ; else { if (c=='+' )cout<<a+b; if (c=='-' )cout<<a-b; if (c=='*' )cout<<a*b; if (c=='/' )cout<<a/b; } }
139.贪吃的代价 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int main () { int n,a,b,a1=0 ,b1=0 ; cin>>n>>a>>b; if (n/min (a,b)==n/max (a,b)){ if (max (a,b)==a)a1=n/a; else b1=n/a; cout<<a1<<" " <<b1,exit (0 ); }else if (min (a,b)==a)a1=n/a; else b1=n/b; while (1 ) { if (n-(a1*a+b1*b)>=abs (a-b)){ if (min (a,b)==a && a1>0 ) a1--,b1++; else if (min (a,b)==b && b1>0 ) b1--,a1++; } else cout<<a1<<" " <<b1,exit (0 ); } }
140.求一元二次方程的根 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { double a,b,c,x,y; scanf ("%lf%lf%lf" ,&a,&b,&c); x=(-b+sqrt (b*b-4 *a*c))/(2 *a); y=(-b-sqrt (b*b-4 *a*c))/(2 *a); cout<<fixed<<setprecision (5 ); if (x==y)printf ("x1=x2=%.5lf" ,x); else if (x>y)printf ("x1=%.5lf;x2=%.5lf" ,x,y); else if (b==0 )cout<<"x1=" <<b/(2 *a)<<'+' <<sqrt (4 *a*c-b*b)/(2 *a), cout<<"i;x2=" <<b/(2 *a)<<"-" <<sqrt (4 *a*c-b*b)/(2 *a)<<"i" <<endl; else cout<<"x1=" <<-b/(2 *a)<<'+' <<sqrt (4 *a*c-b*b)/(2 *a), cout<<"i;x2=" <<-b/(2 *a)<<"-" <<sqrt (4 *a*c-b*b)/(2 *a)<<"i" <<endl; return 0 ; }
141.基本运算1 1 2 3 4 5 int main () { int a; cin>>a; cout<<a-2 <<endl; }
142.基本运算2 1 2 3 4 5 int main () { int a; cin>>a; cout<<a*(2 +3 )<<endl; }
143.基本运算3 1 2 3 4 5 int main () { int a,n; cin>>a>>n; cout<<a%(n%=2 )<<endl; }
144.基本运算4 1 2 3 4 5 6 int main () { int a; cin>>a; a+=a-=a*=a; cout<<a<<endl; }
145.老狼老狼几点了? 1 2 3 4 int main () { for (int i=1 ;i<=12 ;i++) cout<<"What time is it, old wolf? " <<i<<" o'clock." <<endl; }
146.叮叮当当 1 2 3 4 5 6 7 8 9 10 11 int main () { int a,b; cin>>a>>b; for (int i=a;i<=b;i++){ if (i%2 ==0 &&i%3 ==0 )cout<<i<<": ding ding,dang dang" <<endl; else { if (i%2 ==0 )cout<<i<<": ding ding" <<endl; if (i%3 ==0 )cout<<i<<": dang dang" <<endl; } } }
147.计算运动消耗量 1 2 3 4 5 6 7 int main () { int a,b,c; cin>>c; for (int i=0 ;i<c;i++) cin>>a,b+=a; cout<<b; }
148.ZZQ的难题 1 2 3 4 5 6 int main () { long long n; cin>>n; n=pow (2 ,n); cout<<n; }
149.1到n的累加和 1 2 3 4 5 6 int main () { int n,a; cin>>n; for (int i=1 ;i<=n;i++)a+=i; cout<<a; }
150.求阶乘n! 1 2 3 4 5 6 7 int main () { long long n,a=1 ; cin>>n; for (int i=1 ;i<=n;i++)a*=i; if (n==0 )a=1 ; cout<<a; }
151.小明存钱 1 2 3 4 5 6 7 int main () { int a,b,c; cin>>c; for (int i=0 ;i<c;++i) cin>>a,b+=a; cout<<b; }
152.求平均年龄 1 2 3 4 5 6 7 8 int main () { int a,c; double b; cin>>c; for (int i=0 ; i<c; i++) { cin>>a; b+=a; } cout<<fixed<<setprecision (2 )<<b/c; }
153.财务管理 1 2 3 4 5 6 int main () { double a,b; for (int i=0 ;i<12 ;i++){ cin>>a; b+=a; } cout<<fixed<<setprecision (2 )<<"$" <<b/12 ; }
154.均值 1 2 3 4 5 6 7 8 int main () { int c; double a,b; cin>>c; for (int i=0 ;i<c;i++) { cin>>a; b+=a; } cout<<fixed<<setprecision (4 )<<b/c; }
155.求整数的和与均值 1 2 3 4 5 6 7 8 int main () { int a,c; double b; cin>>c; for (int i=0 ;i<c;i++) { cin>>a; b+=a; } cout<<b<<" " <<fixed<<setprecision (5 )<<b/c; }
156.温度转换 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { double a,b; cin>>a>>b; if (a<b) { for (int i=a;i<=b;i+=2 ){ cout<<i<<" " <<fixed<<setprecision (1 )<<9 *i/5.0 +32 <<endl; } } else { for (int i=b; i<=a; i=i+2 ) { cout<<i<<" " <<fixed<<setprecision (1 )<<9 *i/5.0 +32 <<endl; } } }
157.最高的分数 1 2 3 4 5 6 7 int main () { int a,b,c; cin>>c; for (int i=0 ;i<c;i++){ cin>>a; b=max (a,b); } cout<<b; }
158.求最大值和最小值 1 2 3 4 5 6 7 8 9 int main () { int a,b,c,d; cin>>c; for (int i=0 ;i<c;i++){ cin>>a; b=max (a,b); d=min (a,d); } cout<<b<<" " <<d; }
159.整数序列的元素最大跨度值 1 2 3 4 5 6 7 8 9 int main () { int a,b,c,d=100000000 ; cin>>c; for (int i=0 ;i<c;i++){ cin>>a; b=max (a,b); d=min (a,d); } cout<<b-d; }
160.一个字符三角形 1 2 3 4 5 6 7 8 char c;int main () { scanf ("%c" ,&c); for (int i=6 ;i+1 ;--i){ for (int j=1 ;j<=(7 -i)*2 -1 ;++j)printf ("%c" ,c); puts ("" ); } return 0 ; }
161.小明买香蕉 1 2 3 4 5 6 7 8 9 int a,b,sum;int main () { cin>>a; for (int i=0 ;i<a;i++){ cin>>b; if (b<20 &&b%6 ==0 ||b<20 &&b%7 ==0 )sum++; } cout<<sum; }
162.多边形内角和 1 2 3 4 5 6 7 int main () { int a,b,c; cin>>c; for (int i=1 ;i<c;i++) cin>>a, b+=a; cout<<(c-2 )*180 -b; }
163.奇数求和 1 2 3 4 5 6 7 int main () { int a,b,c=0 ; cin>>a>>b; for (int i=a;i<=b;i++) if (i%2 ==1 )c+=i; cout<<c; }
164.满足条件的数累加 1 2 3 4 5 6 7 int main () { int a,b,c=0 ; cin>>a>>b; for (int i=a;i<=b;i++) if (i%17 ==0 )c+=i; cout<<c; }
165.统计数字 1 2 3 4 5 6 7 8 int main () { char x; int a,b,c=0 ; cin>>x>>a>>x>>b>>x; for (int i=a;i<=b;i++) if (i%10 ==6 &&i%3 ==0 )c++; cout<<c; }
166.整数的个数 1 2 3 4 5 6 7 8 9 10 int main () { int a,b,c,s1=0 ,s5=0 ,s10=0 ; cin>>c; for (int i=0 ;i<c;i++) { cin>>a; if (a==1 )s1++; if (a==5 )s5++; if (a==10 )s10++; } cout<<s1<<endl<<s5<<endl<<s10; }
167.与指定数字相同的数的个数 1 2 3 4 5 6 7 8 int main () { int n,m,a,m1=0 ; cin>>n>>m; for (int i=0 ;i<n;i++) { cin>>a; if (a==m)m1++; } cout<<m1; }
168.与众不同的那个数 1 2 3 4 5 6 7 8 9 10 int n,a[10005 ],one[2 ],zero[2 ];int main () { cin>>n; for (int i=1 ;i<=n;i++){ cin>>a[i]; if (a[i]&1 )one[0 ]++,one[1 ]=i; else zero[0 ]++,zero[1 ]=i; } if (one[0 ]==1 )cout<<one[1 ]; else cout<<zero[1 ]; }
169.统计期中成绩 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;int m,n,x;int main () { cin>>m; while (m--) { cin>>n; int cnt=0 ,s=0 ; for (int i=1 ; i<=n; i++) { cin>>x; s+=x; if (x>=60 ) cnt++; } printf ("%.2lf %.2lf\n" ,1.0 *s/n,1.0 *cnt/n); } return 0 ; }
170.乘方计算 1 2 3 4 5 6 7 int main () { int a,n; long long b=1 ; cin>>a>>n; for (int i=0 ;i<n;i++)b*=a; cout<<b; }
171.完全平方数 1 2 3 4 5 6 7 8 9 int main () { int x,y,n; for (n=-100 ; n<10000 ; n++) { x=int (sqrt (100 +n)); y=int (sqrt (100 +168 +n)); if (100 +n==x*x && 100 +168 +n==y*y) cout<<n<<endl; } }
172.计算组合数 1 2 3 4 5 6 7 int main () { long double a=1 ,b=1 ,c=1 ,d,n,m; cin>>n>>m; for (int i=n;i>n-m;i--)a*=i; for (int i=1 ;i<=m;i++)b*=i; cout<<fixed<<setprecision (0 )<<a/b; }
173.求西格玛 $(\sum)$ 累加和 1 2 3 4 5 6 7 int main () { double a=0 ; for (int i=1 ;i<=100 ;++i)a+=i; for (int i=1 ;i<=50 ;++i)a+=i*i; for (int i=1 ;i<=10 ;++i)a+=1.0 /i; printf ("%.4lf" ,a); }
174.奥运奖牌计数 1 2 3 4 5 6 7 8 int main () { int a,b,c,d,a1=0 ,b1=0 ,c1=0 ; cin>>d; for (int i=0 ;i<d;i++) { cin>>a>>b>>c; a1+=a; b1+=b; c1+=c; } cout<<a1<<" " <<b1<<" " <<c1<<" " <<a1+b1+c1; }
175.地狱飞龙 1 2 3 4 5 6 int main () { long long x,y; char a; cin>>x>>a>>y; for (int i=0 ;i<x;i++) y*=2 ; cout<<y; }
176.人口增长问题 1 2 3 4 5 6 7 int main () { int x,n; double c; cin>>x>>n,c=x; for (int i=0 ;i<n;i++)c*=1.001 ; cout<<fixed<<setprecision (4 )<<c; }
177.银行利息 1 2 3 4 5 6 7 8 9 int main () { double R,M,Y,yuan; int a; cin>>R>>M>>Y; yuan=M; for (int i=0 ;i<Y;i++) yuan+=yuan*(R/100 ); cout<<int (yuan); }
178.小明买口罩 1 2 3 4 5 6 7 8 9 10 int a,b,c,ans;int main () { scanf ("%d%d%d" ,&a,&b,&c); for (int i=1 ;a*i<=100 ;++i){ for (int j=1 ;a*i+b*j<=100 ;++j){ int k=100 -a*i-b*j,l=k/c; if (k%c==0 &&l>0 )ans=max (ans,i+j+l); } } printf ("%d" ,ans); }
179.菲波那契数列 1 2 3 4 5 6 7 int main () { int a,b,c,k; cin>>k,a=1 ,b=1 ; for (int i=0 ;i<k-2 ;i++) c=a+b,a=b,b=c; cout<<c; }
180.鸡尾酒疗法 1 2 3 4 5 6 7 8 9 10 11 12 13 int main () { int n,a,b; double s1,sn; cin>>n>>a>>b; s1=b/(a*1.00 ); for (int i=1 ;i<n;i++){ cin>>a>>b; sn=b/(a*1.00 ); if ((sn-0.05 )>s1)cout<<"better" <<endl; else if ((s1-0.05 )>sn)cout<<"worse" <<endl; else cout<<"same" <<endl; } return 0 ; }
181.救援 1 2 3 4 5 6 7 8 9 10 11 double t,x,y;int main () { int a,r; cin>>a; for (int i=0 ;i<a;i++){ cin>>x>>y>>r; t+=sqrt (y*y+x*x)/50.0 *2 ; for (int i=0 ;i<r;i++) t+=1 +0.5 ; } cout<<ceil (t); }
182.买房子 1 2 3 4 5 6 7 8 int main () { double n,k,m,f=200 ; cin>>n>>k,m=n; for (int i=1 ;i<=20 ;i++){ f*=k/100 +1 ,m+=n; if (m>f)cout<<i+1 ,exit (0 ); } cout<<"Impossible" ; }
183.球弹跳高度的计算 1 2 3 4 5 6 7 8 int main () { double a,b,m; cin>>m,b=m; for (int i=0 ;i<10 ;i++) a+=b*2 ,b=b/2 ; a-=m; cout<<fixed<<setprecision (6 )<<a<<endl<<b; }
184.津津的储蓄计划 1 2 3 4 5 6 7 8 9 10 11 int c,s,h;int main () { for (int i=1 ;i<=12 ;i++){ cin>>c; s-=c-300 ; if (s<0 ){ cout<<-1 *i; return 0 ; } else h+=s/100 ;s%=100 ; } cout<<120 *h+s; }
185.药房管理 1 2 3 4 5 6 7 8 9 int n,m,s,sum;int main () { cin>>m>>n; for (int i=1 ;i<=n;i++){ cin>>s; if (s>m)sum++; else m-=s; } cout<<sum; }
186.正常血压 1 2 3 4 5 6 7 8 9 10 int n,g,d,sum,maxn;int main () { cin>>n; for (int i=0 ;i<n;i++){ cin>>g>>d; if (g<=140 &&g>=90 &&d>=60 &&d<=90 )sum++; else sum=0 ; maxn=max (maxn,sum); } cout<<maxn; }
187.求特殊自然数 1 2 3 int main () { cout<<248 <<endl<<503 <<endl<<305 ; }
188.统计满足条件的4位数个数 1 2 3 4 5 6 7 int main () { int a,b,c; cin>>a; for (int i=0 ;i<a;i++) cin>>b,c+=(b%10 -b/1000 -b/100 %10 -b/10 %10 >0 ); cout<<c; }
189.分类求和 1 2 3 4 5 6 7 int x,a,b,c,d;int main () { while (cin>>x){ if (x>0 )a++,c+=x; if (x<0 )b++,d+=x; } cout<<a<<" " <<b<<" " <<c<<" " <<d; }
190.分离整数的各个数位 1 2 3 4 5 6 7 char a[10 ];int i;int main () { cin.get (a,9 ); while (a[i])i++; i--; for (;i>=0 ;i--)cout<<a[i]<<" " ; }
191.数字之和 1 2 3 4 5 6 7 8 char a[12 ];int main () { int sum; cin.get (a,11 ); for (int i=0 ;a[i];i++) if (isdigit (a[i]))sum+=a[i]-'0' ; cout<<sum; }
192.数字反转 1 2 3 4 5 int n,s;int main () { for (cin>>n;n;n/=10 )s*=10 ,s+=n%10 ; cout<<s; }
193.含k个3的数 1 2 3 4 5 6 7 int main () { long long m,a,k,sum=0 ; cin>>m>>k,a=m; for (;a;a/=10 )if (a%10 ==3 )sum++; if (m%19 ==0 &&sum==k)cout<<"YES" ; else cout<<"NO" ; }
194.判断回文数 朴素的将字符进行拆位再依次判断是否相等
1 2 3 4 5 6 7 8 9 int a,sum,n[10 ];int main () { bool x=1 ; cin>>a; for (int i=0 ;a;a/=10 ,i++)n[i]=a%10 ; for (int i=0 ;n[i];i++,sum++); for (int i=0 ;i<sum/2 ;i++)if (n[i]!=n[sum-i-1 ])x=0 ; if (x&&sum>1 )cout<<"yes" ; else cout<<"no" ; }
或者可以将整个数字直接翻转判断是否和原来相等
1 2 3 4 5 6 7 int n,s,m;int main () { cin>>n,s=n; while (n)m=m*10 +n%10 ,n/=10 ; if (m==s)cout<<"yes" ; else cout<<"no" ; }
195.迭代法求值 1 2 3 4 int main () { int a; cin>>a; cout<<fixed<<setprecision (4 )<<sqrt (a)<<endl; }
196.级数求和 1 2 3 4 5 6 7 int main () { int k,i=0 ; double n=0 ; cin>>k; while (n<=k)i++,n+=1.0 /i; cout<<i; }
197.角谷猜想 1 2 3 4 5 6 7 8 9 int main () { int a; cin>>a; while (a>1 ) { if (a%2 ==1 ) cout<<a<<"*3+1=" <<a*3 +1 ,a=a*3 +1 ; else cout<<a<<"/2=" <<a/2 ,a>>=1 ; cout<<"\n" ; } cout<<"End" ; }
198.对对交换 1 2 3 4 int main () { int n; cin>>n; for (int i=1 ;i<=n/2 ;i++)cout<<i*2 <<" " ,cout<<i*2 -1 <<" " ; }
199.蜗牛历险记 考虑暴力枚举,每组数据复杂度 O(T),数据水可以通过
1 2 3 4 5 6 7 8 9 10 11 12 int a,b,c,d,ans;int main () { while (cin>>a>>b>>c>>d){ ans=0 ; for (int i=1 ;i<=d;i++){ if (i&1 )ans+=b; else ans-=c; if (ans>=a)break ; } if (ans>=a)cout<<"yes\n" ; else cout<<"no\n" ; } }
对于每个 2s 的周期,蜗牛只会上升 a-b 米,最后 1s 最多可以上升 a 米,可以将这部分的次数算出来,最后判断是否需要再加一秒即可。注意特判 a==b 的情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 int h,a,b,T,t;int main () { while (cin>>h>>a>>b>>T){ if (a==b){ if (h<=a)puts ("yes" ); else puts ("no" ); } else { t=(h-b-1 )/(a-b); t=t*2 +(t*(a-b)<h); if (t<=T)puts ("yes" ); else puts ("no" ); } } return 0 ; }
200.找规律填数字 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 int main () { int a,b,c,d,e; while (cin>>a>>b>>c>>d>>e){ if (a==0 &b==0 &&c==0 &&d==0 &&e==0 ) break ; if ((b-a)==(c-b)&&(d-c)==(c-b)&&(d-c)==(e-d)){ int t=b-a; for (int i=1 ;i<=5 ;i++) cout<<e+i*t<<" " ; } if ((b/a)==(c/b)&&(c/b)==(d/c)&&(d/c)==(e/d)&&(b/a)!=1 ){ int t=b/a; for (int i=1 ;i<=5 ;i++) cout<<e*int (pow(t,i)) <<" " ; } if (a+b==c&&b+c==d&&c+d==e){ int t1=d,t2=e,s=0 ; for (int i=6 ;i<11 ;i++){ s=t1+t2,t1=t2,t2=s; cout<<s<<" " ; } } cout<<endl; } }
Page 3 201.求分数序列和 1 2 3 4 5 6 7 8 9 int main () { double n,sum=0 ,p=1 ,q=2 ,p1,q1; cin>>n; for (int i=0 ;i<n;i++){ sum+=q/p; q1=q+p,p1=q; q=q1,p=p1; } cout<<fixed<<setprecision (4 )<<sum; }
202.计算分数加减表达式的值 1 2 3 4 5 6 7 8 9 int main () { int n,a=1 ; double sum=0 ; cin>>n; for (;a<=n;a++){ if (a%2 ==1 )sum+=1.0 /a; else sum-=1.0 /a; } cout<<fixed<<setprecision (4 )<<sum; }
203.求阶乘的和 1 2 3 4 5 6 7 8 int main () { int a,b=1 ,c=0 ; cin>>a; for (int x=1 ;x<=a;x++) { for (int i=1 ;i<=x;i++)b*=i; c+=b,b=1 ; } cout<<c; }
204.求出e的值 1 2 3 4 5 6 7 8 9 int main () { long double n,a=1 ,sum=1 ,w=0 ; cin>>n; for (;a<=n;a++){ w=1 ; for (int j=1 ;j<=a;j++)w*=j; sum+=1.0 /w; } cout<<fixed<<setprecision (10 )<<sum; }
205.计算多项式的值 1 2 3 4 5 6 7 8 9 10 int main () { int n; long double a=0 ,x; cin>>x>>n; for (int i=n;i>=1 ;i--) a+=pow (x,i); if (n==0 )a=1 ; a+=1 ; cout<<fixed<<setprecision (2 )<<a; }
206.年龄与疾病 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int x,n,a,b,c,d;int main () { cin>>x; for (int i=0 ;i<x;i++){ cin>>n; if (n>=0 && n<=18 )a++; if (n>=19 && n<=35 )b++; if (n>=36 && n<=60 )c++; if (n>=61 )d++; } cout<<fixed<<setprecision (2 ); cout<<float (a*100.0 /x)<<"%" <<endl; cout<<float (b*100.0 /x)<<"%" <<endl; cout<<float (c*100.0 /x)<<"%" <<endl; cout<<float (d*100.0 /x)<<"%" <<endl; }
207.不高兴的津津 1 2 3 4 5 6 7 8 9 10 11 12 13 int a[8 ][2 ],maxn=0 ;int main () { for (int i=0 ; i<7 ; i++) { cin>>a[i][0 ]>>a[i][1 ]; maxn=max (maxn,a[i][0 ]+a[i][1 ]); } if (maxn<=8 ) cout<<0 ; else { for (int i=0 ; i<7 ; i++) if (a[i][0 ]+a[i][1 ]==maxn) cout<<i+1 ,exit (0 ); } }
208.小明的绩点 1 2 3 4 5 6 7 double a,b,sum,sumx;int main () { int n; cin>>n; for (int i=0 ;i<n;i++) cin>>a>>b,sumx+=b,sum+=(a-50 )*b*(a>=50 ); printf ("%.2lf" ,sum/10 /sumx); }
209.最长平台 1 2 3 4 5 6 7 8 9 10 int main () { int n,x,y,a=1 ,m=0 ; cin>>n; for (int i=0 ;i<n;i++){ cin>>x; if (x==y)a++; else a=1 ; m=max (m,a),y=x; } cout<<m; }
210.雇佣兵 1 2 3 4 5 6 7 8 int main () { int m,t=0 ,n,x,maxn=0 ; cin>>m>>n>>x; for (;x>0 ;){ if (t>=m) n+=m/n,t=0 ; else x--,t+=n-1 ; } cout<<n; }
211.计算多项式的导函数 1 2 3 4 5 6 7 8 9 10 11 12 int n,m,in;string no="no" ,NO="NO" ,yes="yes" ,YES="YES" ; int main () { cin>>n; if (n==0 )cout<<0 ,exit (0 ); for (int i=n;i;--i){ cin>>m; if (in==0 )cout<<i*m; else cout<<" " <<i*m; in++; } return 0 ; }
212.与7无关的数 1 2 3 4 5 6 7 int n,sum;int main () { cin>>n; for (int i=1 ;i<=n;i++) if (i%10 !=7 &&i%7 !=0 &&i/10 !=7 )sum+=i*i; cout<<sum; }
213.数1的个数 1 2 3 4 5 6 7 8 int n,sum;int main () { cin>>n; for (int i=1 ;i<=n;i++) for (int j=i;j;j/=10 ) if (j%10 ==1 )sum++; cout<<sum; }
214.数字统计 1 2 3 4 5 6 7 8 9 10 11 int a,b,sum;int main () { cin>>a>>b; for (int i=a;i<=b;i++){ if (i/10000 ==2 ) sum++; if (i/1000 %10 ==2 )sum++; if (i/100 %10 ==2 ) sum++; if (i/10 %10 ==2 ) sum++; if (i%10 ==2 ) sum++; } cout<<sum; }
215.正整数分解 ps:wangyifan 大牛就是我 1 2 3 4 5 6 7 8 long long n,sum;int main () { cin>>n; for (long long x=1 ,y=1 ;x<n;x++,y=n-x) if (x%7 ==0 &&y%3 ==0 ) cout<<x<<' ' <<y<<"\n" ,sum++; cout<<sum; }
216.质因数分解 1 2 3 4 5 6 long long n,maxn=0 ;int main () { cin>>n; for (long long i=2 ;i*i<=n;++i) if (n%i==0 )return cout<<n/i,0 ; }
217.数数小木块 1 2 3 4 5 6 7 8 9 10 long long a,b,c;int main () { cin>>a; for (int i=0 ;i<a;i++){ cin>>b; for (int j=1 ;j<=b;j++) for (int k=1 ;k<=j;k++)c+=k; cout<<c<<"\n" ,c=0 ; } }
218.去除圆括号 1 2 3 4 5 6 7 8 9 10 char a[1001 ],len;int s=1 ;int main () { cin.getline (a,1001 ); for (int i=0 ;i<strlen (a)-1 ;i++){ if (a[i]=='(' )s=0 ; if (s)cout<<a[i]; if (a[i]==')' )s=1 ; } }
219.英文加密 1 2 3 4 5 6 7 8 9 char a[1001 ]; int s=1 ;int main () { cin.getline (a,1001 ); for (int i=0 ;i<strlen (a)-1 ;i++){ if (a[i]>='a' &&a[i]<='z' )a[i]=char (a[i]-32 +int ('z' -int (a[i])-(a[i]-'a' ))); else if (a[i]>='A' &&a[i]<='Z' )a[i]=char (a[i]+32 +int ('Z' -int (a[i])-(a[i]-'A' ))); cout<<a[i]; } }
220.从大到小列出因子 1 2 3 4 5 6 int main () { int n; cin>>n; for (int i=n;i;i--) if (n%i==0 )cout<<i<<" " ; }
221.判断质数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int main () { long long a, s=2 ; cin>>a; if (a>=2 ) { for (long long i=2 ; i*i<=a; i++) { if (a%i==0 ) { puts ("No" ); return 0 ; } } puts ("Yes" ); } else cout<<"No" ; return 0 ; }
222.第n小的质数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 long long a,b,d;int main () { cin>>a; for (int i=2 ;;i++){ d=0 ; for (int j=2 ;j*j<=i;j++) if (i%j==0 )d++; if (d==0 )b++; if (b==a){ cout<<i; break ; } } }
223.绝对素数 1 2 3 4 5 6 7 8 9 10 11 12 int s,s1;int main () { for (int i=10 ;i<=99 ;i++){ for (int j=2 ;j*j<i;j++) if (i%j==0 )s++; for (int j=2 ;j*j<i/10 +i%10 *10 ;j++) if ((i/10 +i%10 *10 )%j==0 )s1++; if (s1==0 &&s==0 )cout<<i<<"\n" ; s1=0 ,s=0 ; } }
224.双胞胎数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int a[1000 ],s,len,n,n1;int main () { for (int i=2 ;i<1000 ;i++){ for (int j=2 ;j<i;j++) if (i%j==0 )s++; if (s==0 )a[len++]=i; s=0 ; } cin>>n; for (int i=0 ;i<len-1 ;i++){ if (a[i+1 ]>n)break ; if (a[i]+2 ==a[i+1 ]) cout<<a[i]<<" " <<a[i+1 ]<<"\n" ,n1++; } cout<<n1; return 0 ; }
225.哥德巴赫猜想 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int ck (int n) { for (int i=2 ; i*i<=n; i++) if (n%i==0 ) return 0 ; return 1 ; } int n,s;int main () { while (cin>>n,n!=0 ){ s=0 ; for (int i=2 ; i<=(n>>1 ); i++) if (ck (i)&&ck (n-i)) s++; cout<<s<<"\n" ; } return 0 ; }
226.超越路人甲 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int x, y, z, xs, ys;int main () { cin>>x>>y>>z; for (int i=1 ; i<=50 ; i++) { xs+=x, ys+=y; cout<<"NO." <<i<<" " <<xs<<" " <<ys<<"\n" ; if (ys>xs) { cout<<i; break ; } y+=z; } if (xs>=ys)cout<<"NO" ; }
227.收集金币 1 2 3 4 5 6 7 8 int n,x=1 ,y=1 ,s;int main () { cin>>n; while (n){ n--,s+=y,x++; if (x>y)x=1 ,y++; } cout<<s; }
228.数0 1 2 3 4 5 6 int n,cnt; int main () { cin>>n; for (int i=5 ;n/i>=1 ;i*=5 )cnt+=n/i; cout<<cnt; }
229.素数判定 1 2 3 4 5 6 7 8 9 int a,b,c,s;int main () { cin>>a>>b; for (int i=a;i<=b;i++){ s=pow (i,2 )+i+41 ; for (int j=2 ;j<s;j++) if (s%j==0 )c++; } puts (c>0 ?"Sorry" :"OK" ); }
230.求因子和 1 2 3 4 5 6 7 8 int n,s;int main () { cin>>n; for (int i=1 ;i*i<=n;i++){ if (n%i==0 )s+=i+n/i; if (n==i*i)s-=i; } cout<<s-n; }
231.走台阶 1 2 3 4 5 6 7 8 9 10 11 12 long long a,b,c=1 ,d=2 ,e=4 ;int main () { cin>>a; if (a==1 )cout<<c; else if (a==2 )cout<<d; else if (a==3 )cout<<e; else { for (int i=4 ;i<=a;i++) b=c+d+e,c=d,d=e,e=b; cout<<b; } }
232.分数统计 1 2 3 4 5 6 7 8 9 10 11 12 13 double s,sum;int a,b,c,d,n;int main () { cin>>n; for (int i=0 ;i<n;i++){ cin>>s,sum+=s; if (s>=90 )a++; else if (s>=80 )b++; else if (s>=70 )c++; else if (s>=60 )d++; } printf ("%d\n%d\n%d\n%d\n%.2lf" ,a,b,c,d,sum/n); return 0 ; }
233.猴子吃桃 1 2 3 4 5 6 long long n,s=1 ;int main () { cin>>n; for (int i=1 ;i<n;i++)s=(1 +s)*2 %100007 ; cout<<s; }
234.棋盘中的米粒 1 2 3 4 5 6 long long n,s=1 ;int main () { cin>>n; for (int i=1 ;i<n;i++)(s<<=1 )%=10007 ; cout<<s; }
235.疯狂吃鸡腿1 1 2 3 4 5 6 7 long long n,a[10005 ];int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; sort (a+1 ,a+1 +n); cout<<a[n]+a[n-1 ]; }
236.短信计费 1 2 3 4 5 6 7 double n,a,s;int main () { cin>>n; for (int i=0 ;i<n;i++) cin>>a,s+=ceil (a/70 )*0.1 ; printf ("%.1lf" ,s); }
237.勇者买剑 1 2 3 4 5 6 7 8 9 10 11 12 13 int n,j[10005 ],m[10005 ],c;double mx,s;int main () { cin>>n; for (int i=0 ;i<n;i++)cin>>j[i]>>m[i]; for (int i=0 ;i<n;i++){ s=j[i]*1. /m[i]; printf ("%.3lf" ,s); if (i!=n-1 )putchar (' ' ); if (s>mx)mx=s,c=j[i]; } printf ("\n%.3lf %d" ,mx,c); }
238.判断互质 1 2 3 4 5 6 7 8 9 10 #include <bits/stdc++.h> using namespace std;int main () { long long a,b; cin>>a>>b; if (__gcd(a,b) == 1 ) puts ("Yes" ); else puts ("No" ); return 0 ; }
239.最小公倍数 1 2 3 4 5 6 long long a,b;int main () { cin>>a>>b; cout<<a*b/__gcd(a,b); }
240.最大公约数和最小公倍数问题 1 2 3 4 5 6 7 8 int n,m,s,cnt;int main () { cin>>n>>m,s=n*m; for (int i=n;i<=m;i++){ int k=__gcd(i,s/i); if (k==n&&s/k==m&&m%i==0 &&m%(s/i)==0 &&i%n==0 &&(s/i)%n==0 )cnt++; } cout<<cnt; }
241.求1数码 1 2 3 4 5 6 7 8 9 #include <bits/stdc++.h> using namespace std;int main () { long long x; cin>>x; cout<<__builtin_popcount(x); return 0 ; }
242.乘法口诀 1 2 3 4 5 6 7 8 long long a;int main () { cin>>a; for (int i=1 ;i<=a;i++){ for (int j=1 ;j<=i;j++)printf ("%d*%d=%d " ,j,i,i*j); puts ("" ); } }
243.Y三角形 1 2 3 4 5 6 7 8 9 10 int n;string s; int main () { cin>>n; for (int i=1 ;i<=n;i++){ for (int j=1 ;j<i;j++)cout<<" " ; for (int j=1 ;j<=(n-i+1 )*2 -1 ;j++)cout<<"Y" ; puts ("" ); } }
244.打印数字图形 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 void print (int a,int b,int c) { if (c>0 )for (int i=a;i<=b;i+=c)cout<<i; else for (int i=a;i>=b;i+=c)cout<<i; } int main () { int n; while (cin>>n){ for (int i=1 ;i<=n;i++){ for (int j=1 ;j<=n-i;j++)cout<<" " ; print (1 ,i,1 ),print (i-1 ,1 ,-1 ); cout<<"\n" ; } for (int i=n-1 ;i>=1 ;i--){ for (int j=1 ;j<=n-i;j++)cout<<" " ; print (1 ,i,1 ),print (i-1 ,1 ,-1 ); cout<<"\n" ; } } }
245.百钱百鸡 1 2 3 4 5 6 7 8 9 int n,s;int main () { cin>>n; for (int i=0 ;i<=n/5 ;i++) for (int j=0 ;j<=n/3 ;j++) if (i+j+(n-i*5 -j*3 )*3 ==n)s++; cout<<s; }
246.我家的门牌号 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { int n,s,x; cin>>n,s=sqrt (6 +2 *n)-1 ; for (int i=s;;i++) if ((i*i+i-2 *n)%6 ==0 ){ x=(i*i+i-2 *n)/6 ; if (x<=0 )continue ; if (x>0 ){ cout<<x<<" " <<i; break ; } } return 0 ; }
247.一个数学问题 1 2 3 4 5 6 7 8 9 10 11 12 double a,b,i=1 ,sum;int main () { while (1 ){ cin>>a>>b; if (!a&&!b)break ; for (int i=1 ;i<a;i++) for (int j=i;j<a;j++) if ((i*i+j*j+b)/(i*j)==int ((i*i+j*j+b)/(i*j))&&i!=j)sum++; cout<<"Case " <<i++<<": " <<sum<<'\n' ,sum=0 ; } }
248.水仙花数 1 2 3 4 5 6 7 8 9 int main () { long long m,n,b,s,g; char c; cin>>m>>c>>n; for (int i=m;i<=n;i++){ b=i/100 ,s=i/10 %10 ,g=i%10 ; if (b*b*b+s*s*s+g*g*g==i)cout<<i<<endl; } }
249.质因子表示 1 2 3 4 5 6 7 8 9 10 11 12 long long n,s,i=2 ;int main () { cin>>n; cout<<n<<"=" ; while (n!=1 ){ if (n%i==0 ){ cout<<i,n/=i; if (n!=1 )cout<<"*" ; i=2 ; } else i++; } }
250.解渴 1 2 3 4 5 6 7 8 long long n,m,a,b,max_water;int main () { cin>>n>>m; for (int i=0 ;i<n;i++) cin>>a>>b, max_water=max (max_water,m/a*b); cout<<max_water; }
251.开车 1 2 3 4 5 6 7 8 9 10 double s,x,y,a,b,m,t;int n;int main () { cin>>n>>m; for (int i=1 ;i<=n;i++) cin>>x>>y, s+=sqrt ((x-a)*(x-a)+(y-b)*(y-b)),a=x,b=y; t=sqrt (2 *s/m); printf ("%.0lf" ,t); }
252.亲和数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 long long n,a,b,cnt;int f (int x) { int sum=0 ; for (int i=1 ;i*i<=x;i++) if (x%i==0 )sum+=x/i,sum+=i; return sum-x; } int main () { cin>>n; for (int i=3 ;cnt<n;i++){ a=f (i),b=f (a); if (b==i)cnt++,cout<<i<<" " <<a<<"\n" ; } return 0 ; }
253.3.2.1 Factorials阶乘 1 2 3 4 5 6 7 8 9 int main () { long long n,ge=1 ; cin>>n; for (int i=1 ;i<=n;i++){ ge*=i; while (ge%10 ==0 )ge/=10 ; ge%=1000000 ; } cout<<ge%10 ; }
254.被涂抹的数字 1 2 3 4 5 6 7 8 int n=25006 ,len;int main () { for (int i=0 ;i<=9 ;i++) for (int j=0 ;j<=9 ;j++) if ((n+i*100 +j*10 )%37 ==0 ||(n+i*100 +j*10 )%67 ==0 ) len++,cout<<n+i*100 +j*10 <<endl; cout<<len; }
255. 最小长方形 1 2 3 4 5 6 7 8 9 10 11 12 13 14 long long maxx=-1e10 ,maxy=-1e10 ,minx=1e10 ,miny=1e10 ,x,y,flag;int main () { for (int i=0 ;;i++){ cin>>x>>y; if (!x&&!y&&!flag)break ; else if (x==0 &&y==0 ){ cout<<minx<<" " <<miny<<" " <<maxx<<" " <<maxy<<endl; maxx=-10e9 ,maxy=-10e9 ,minx=10e9 ,miny=10e9 ,flag=0 ; } if (x)minx=min (minx,x),maxx=max (maxx,x); if (y)miny=min (miny,y),maxy=max (maxy,y); if (x&&y)flag++; } }
256.收款收据 1 2 3 4 5 6 7 8 9 10 11 int main () { for (int k=0 ;k<10 ;k++){ int m=1401 +k*10 ; if (m%17 ==0 ){ cout<<"total is :" <<m<<"\n" ; for (int i=1 ;i<=m/13 ;i++) for (int j=1 ;j<=m/8 ;j++) if (i*13 +j*8 ==m)cout<<"No.1: " <<i<<", NO.2: " <<j<< "\n" ; } } }
257.寻找金蝉素数 1 2 3 int main () { puts ("Golden Cicada Prime: 13597 53791 79531 91573 95713\nThe sum is: 5" ); }
258.猜猜谁是冠军 1 2 3 int main () { cout<<"C" ; }
259.连续自然数之和 1 2 3 4 5 6 7 8 9 10 11 int n,sum;int main () { cin>>n; for (int i=1 ;i<=n/2 ;i++){ sum=i; for (int j=i+1 ;sum<=n;j++){ sum+=j; if (sum==n)cout<<i<<"--" <<j<<endl; } } }
260.输出成绩 1 2 3 4 5 6 7 8 9 10 11 12 int n,a[10000 ];double sum;int main () { cin>>n; for (int i=1 ;i<=n;i++){ cin>>a[i]; sum+=a[i]; } sum/=n; printf ("%.3lf\n" ,sum); for (int i=1 ;i<=n;i++) if (a[i]>sum)cout<<a[i]<<endl; }
261.数组逆序重放 1 2 3 4 5 6 7 int n,a[10000 ];int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; reverse (a.begin (),a.end ()); for (int i=1 ;i<=n;i++)cout<<a[i]<<" " ; }
262.数组间的比较大小 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int n=10 ,x,d,deng,a[11 ],b[11 ];int main () { for (int i=1 ;i<=n;i++)cin>>a[i]; for (int i=1 ;i<=n;i++){ cin>>b[i]; if (b[i]>a[i])x++; else if (b[i]<a[i])d++; else deng++; } cout<<d<<" " <<deng<<" " <<x<<"\n" <<"a" ; if (x>d)cout<<"<" ; else if (x<d)cout<<">" ; else if (x==d)cout<<"=" ; cout<<"b" ; }
263.数组练习1 1 2 3 4 5 6 int a[100001 ],n,m,s1;int main () { cin>>n>>m; for (int i=1 ;i<=n;i++)cin>>a[i]; for (int i=0 ;i<m;i++)cin>>s1,cout<<a[s1]<<endl; }
264.数组练习2 1 2 3 4 5 6 7 8 9 10 11 int a[100001 ],n,m,s1;int main () { cin>>n>>m; for (int i=1 ;i<=n;i++)cin>>a[i]; for (int i=0 ;i<m;i++){ cin>>s1; int maxn=-1e9 ; for (int j=1 ;j<=s1;j++)maxn=max (maxn,a[j]); cout<<maxn<<endl; } }
265.选题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 long long b[1000001 ],n,a,s,s1,s2;int main () { cin>>n>>a; for (int i=0 ;i<n;i++){ cin>>b[i]; if (b[i]==a)s++; if (b[i]>a)s1++; if (b[i]<a)s2++; } if (s+s2==n)return cout<<"all" ,0 ; else if (s==n)return cout<<"all" ,0 ; else if (s1+s==n)return cout<<-1 ,0 ; cout<<s2<<"\n" ; for (int i=0 ;i<n;i++){ if (b[i]<a)cout<<i+1 <<" " <<b[i]<<endl; } }
266.计算书费 1 2 3 4 5 6 7 8 double a[100 ]={28.9 ,32.7 ,45.6 ,78 ,35 ,86.2 ,27.8 ,43 ,56 ,65 },sum,n;int main () { for (int i=0 ;i<10 ;i++){ cin>>n; sum+=n*a[i]; } printf ("%.1lf" ,sum); }
267.查找特定的值 1 2 3 4 5 6 7 8 9 10 int a[100000 ],sum,n,fd,flag;int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; cin>>fd; for (int i=1 ;i<=n;i++){ if (a[i]==fd)return cout<<i,0 ; } if (!flag)cout<<-1 ; }
268.不与最大数相同的数字之和 1 2 3 4 5 6 7 8 9 10 int n,a[101 ],maxn,sum;int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; for (int i=1 ;i<=n;i++)maxn=max (maxn,a[i]); for (int i=1 ;i<=n;i++){ if (a[i]!=maxn)sum+=a[i]; } cout<<sum; }
269.陶陶摘苹果 1 2 3 4 5 6 7 8 int n,a[10 ],s;int main () { for (int i=0 ;i<10 ;i++)cin>>a[i]; cin>>n; for (int i=0 ;i<10 ;i++) if (n+30 >=a[i])s++; cout<<s; }
270.珠心算测验 1 2 3 4 5 6 7 8 9 10 11 12 13 int n,sum,a[101 ];int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; for (int i=1 ;i<=n;i++){ int flag=0 ; for (int j=1 ;j<=n;j++) for (int k=1 ;k<=n;k++) if (j!=k) if (a[i]==a[j]+a[k])flag=1 ; if (flag)sum++; }cout<<sum; }
271.平移数据 1 2 3 4 5 6 7 long long n,a[1000001 ];int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; a[0 ]=a[n]; for (int i=0 ;i<n;i++)cout<<a[i]<<" " ; }
272.向量点积计算 1 2 3 4 5 6 7 8 9 10 long long a[100001 ],b,n,sum;int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; for (int i=1 ;i<=n;i++){ cin>>b; sum+=a[i]*b; } cout<<sum; }
273.疯狂求和 1 2 3 4 5 6 7 8 9 10 long long a[100001 ],n,m,a1,b1;int main () { cin>>n>>m; for (int i=1 ;i<=m;i++)cin>>a[i]; for (int i=1 ;i<n;i++) for (int j=i+1 ;j<=n;j++) if (a[i]+a[j]==n)a1=a[i],b1=a[j]; if (a1==0 &&b1==0 )cout<<"No Answer" ; else cout<<a1<<" " <<b1; }
274.买铅笔 1 2 3 4 5 6 7 8 9 10 long long a[100001 ],n,m,mina=10e9 ;int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; for (int i=1 ;i<n;i++) for (int j=i+1 ;j<=n;j++) if (i+j==n)mina=min (mina,a[i]+a[j]); mina=min (mina,a[n]); cout<<mina; }
275.插队 1 2 3 4 5 6 7 8 9 10 11 int n,a[201 ],c,b,s=-1 ;int main () { cin>>n; for (int i=0 ;i<n;i++)cin>>a[i]; cin>>c>>b; for (int i=n-1 ;i>=c;i--)a[i+1 ]=a[i]; n++; a[c]=b; for (int i=0 ;i<n;i++)cout<<a[i]<<" " ; }
276.奇怪的数列 1 2 3 4 5 6 7 8 9 10 11 12 long long n,sum;string a1,a,b="123456789" ; int main () { cin>>n; for (int i=1 ;i<=n;i++){ cin>>a1; a+=a1; } for (int i=0 ;i<9 ;i++) if (int (a.find (b[i]))==-1 )sum+=b[i]-'0' ; cout<<sum; }
277.小明吃奶糖 1 2 3 4 5 6 7 8 int a[1000001 ],n,maxn=-10000 ,o,r,e;int main () { cin>>n; for (int i=0 ;i<n;i++)cin>>a[i]; for (int i=0 ;i<n-2 ;i++) if (a[i]+a[i+1 ]+a[i+2 ]>maxn)o=i+1 ,r=i+2 ,e=i+3 ,maxn=a[i]+a[i+1 ]+a[i+2 ]; cout<<o<<" " <<r<<" " <<e; }
278.校门外的树 1 2 3 4 5 6 7 8 9 10 11 12 13 int n,m,a[10005 ],x,y,s;int main () { cin>>n>>m; for (int i=0 ;i<=n;i++)a[i]=1 ; for (int j=0 ;j<m;j++){ cin>>x>>y; for (int i=x;i<=y;i++)a[i]=0 ; } for (int i=0 ;i<=n;i++) if (a[i]==1 )s++; cout<<s; }
279.找最小的数 1 2 3 4 5 6 7 8 9 10 11 12 13 long long n,a[10001 ],ming=10e9 ,num;int main () { cin>>n; for (int i=1 ;i<=n;i++){ cin>>a[i]; if (a[i]<ming){ ming=a[i]; num=i; } } swap (a[1 ],a[num]); for (int i=1 ;i<=n;i++)cout<<a[i]<<" " ; }
280.近在咫尺 1 2 3 4 5 6 7 8 9 int n,k;long long a[10005 ],ans=1e9 ;int main () { cin>>n; for (int i=0 ;i<n;i++) cin>>a[i]; for (int i=0 ;i<n-1 ;i++) if (a[i+1 ]-a[i]<ans) ans=a[i+1 ]-a[i], k=i; cout<<a[k]<<" " <<a[k+1 ]<<" " <<a[k+1 ]-a[k]<<"\n" <<a[0 ]<<" " <<a[n-1 ]<<" " <<a[n-1 ]-a[0 ]; }
281.疯狂吃鸡腿2 1 2 3 4 5 6 7 8 9 10 double n,a[10001 ],b[10001 ],sum;int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; for (int i=1 ;i<=n;i++){ cin>>b[i]; if (b[i]==a[i])sum++; } printf ("%.2lf" ,sum/n); }
282.时间之流 1 2 3 4 5 6 7 8 9 10 11 12 13 long long n,a[1001 ],s1=1 ,s,flag;int main () { cin>>n; for (int i=0 ;i<n;i++){ cin>>a[i]; if (a[i]<0 )s=i; if (a[i]>0 )s1=s1*a[i],flag++; } if (n==1 )return cout<<a[0 ],0 ; for (int i=0 ;i<s;i+=2 )s1*=a[i]*a[i+1 ],flag++; if (flag==0 )cout<<0 ; else cout<<s1; }
283.细菌繁殖 1 2 3 4 5 6 7 8 long long d,m1,d1,m2,d2,n,m[12 ]={31 ,28 ,31 ,30 ,31 ,30 ,31 ,31 ,30 ,31 ,30 ,31 };int main () { cin>>m1>>d1>>n>>m2>>d2; for (int i=m1-1 ;i<m2-1 ;i++)d+=m[i]; d=d-d1+d2; for (int i=0 ;i<d;i++)n*=2 ; cout<<n; }
284.合唱队形 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <bits/stdc++.h> using namespace std;int maxj,n,a[100001 ];int main () { cin>>n; for (int i=1 ;i<=n;i++){ scanf ("%d" ,&a[i]); if (a[i]>a[maxj])maxj=i; } for (int i=1 ;i<=n;i++) if (i>=maxj&&a[i]<a[i+1 ]||i<maxj&&a[i]>a[i+1 ])return cout<<"NoAnsweR!" ,0 ; cout<<maxj; }
285.合并有序数列 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <bits/stdc++.h> using namespace std;int a[1500001 ],b[1500001 ],c[1500001 ],m,n,x,s,s1,z;int main () { cin>>m>>n; for (int i=0 ;i<m;i++)scanf ("%d" ,&a[i]); for (int i=0 ;i<n;i++)scanf ("%d" ,&b[i]); for (int i=0 ;i<m+n;i++) if (s>=m)c[z]=b[s1],s1++,z++; else if (s1>=n)c[z]=a[s],s++,z++; else if (a[s]<=b[s1])c[z]=a[s],s++,z++; else if (a[s]>b[s1])c[z]=b[s1],s1++,z++; for (int i=0 ;i<m+n;i++)printf ("%d " ,c[i]); }
286.石头剪刀布 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 int n,na,nb,a[101 ],b[101 ],a1,b1;int main () { cin>>n>>na>>nb; for (int i=1 ;i<=na;i++)cin>>a[i]; for (int i=1 ;i<=nb;i++)cin>>b[i]; for (int i=1 ;i<=n;i++){ int tmp=i%na,tmp2=i%nb; if (!tmp)tmp=na; if (!tmp2)tmp2=nb; if (a[tmp]==2 &&b[tmp2]==5 ||a[tmp]==5 &&b[tmp2]==0 ||a[tmp]==0 &&b[tmp2]==2 )a1++; else if (a[tmp]==b[tmp2])continue ; else b1++; } if (a1>b1)cout<<"A" ; else if (a1<b1)cout<<"B" ; else if (a1==b1)cout<<"draw" ; }
287.插入数值 主打一个不听劝
1 2 3 4 5 6 7 8 int n,k,a[100002 ];int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; cin>>a[n+1 ]; sort (a+1 ,a+n+2 ); for (int i=1 ;i<=n+1 ;i++)cout<<a[i]<<" " ; }
288.数组操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 int n,m,a[100002 ],k,x,y,l;int main () { cin>>n>>m; for (int i=1 ;i<=n;i++)a[i]=i,cout<<a[i]<<" " ; cout<<endl; for (int i=1 ;i<=m;i++){ cin>>k; if (k==1 ){ cin>>x>>y; swap (a[x],a[y]); for (int i=1 ;i<=n;i++)cout<<a[i]<<" " ; cout<<"\n" ; } else if (k==2 ){ cin>>l; a[l]=0 ; for (int i=l;i<n;i++)swap (a[i],a[i+1 ]); n--; for (int i=1 ;i<=n;i++)cout<<a[i]<<" " ; cout<<"\n" ; } } }
289.Zyn的数列 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int n,m,a[100002 ],k,x,y,l,r;int main () { cin>>n>>m; for (int i=1 ;i<=n;i++)cin>>a[i]; for (int i=1 ;i<=m;i++){ cin>>k; if (k==1 )cin>>x>>y,a[x]+=y; else if (k==2 )cin>>x>>y,swap (a[x],a[y]); else if (k==3 ){ cin>>l>>r>>x; for (int j=l;j<=r;j++)swap (a[j+x],a[j]); } } for (int i=1 ;i<=n;i++)cout<<a[i]<<" " ; }
290.做作业 1 2 3 4 5 6 7 int n,a[1000001 ];int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; sort (a+1 ,a+1 +n,greater<>()); for (int i=1 ;i<=n;i++)cout<<a[i]<<" " ; }
291.勇者的冒险 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 double x,y,s;int n,xun;struct cunzhuang { double x; double y; int xu; }a[10005 ]; double f (double x1,double y1,double x2,double y2) { return sqrt ((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } int main () { cin>>n; for (int i=1 ;i<=n;i++){ cin>>a[i].x>>a[i].y; a[i].xu=i; } for (int i=1 ;i<=n;i++){ cin>>xun; s+=f (x,y,a[xun].x,a[xun].y); x=a[xun].x; y=a[xun].y; } printf ("%.3lf" ,s); return 0 ; }
292.友谊 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int n,a[10001 ],b[10001 ],c[10001 ],d[10001 ],e[10001 ],f[10001 ],wmk,cc;int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; for (int i=1 ;i<=n;i++)cin>>b[i]; for (int i=1 ;i<=n;i++)cin>>c[i]; for (int i=1 ;i<=n;i++)cin>>d[i]; for (int i=1 ;i<=n;i++)cin>>e[i]; for (int i=1 ;i<=n;i++){ if (b[i]>=a[i]&&d[i]>=80 )wmk=1 ; if (c[i]>=a[i]&&e[i]>=80 )cc=1 ; } if (wmk&&cc)cout<<"fire" ; else if (wmk)cout<<"wmk" ; else if (cc)cout<<"cc" ; else cout<<"chinese" ; return 0 ; }
293.坐标统计 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int n,x[100001 ],y[100001 ],cnt,maxn=-1 ,maxj;int main () { cin>>n; for (int i=1 ;i<=n;i++)scanf ("%d %d" ,&x[i],&y[i]); for (int i=1 ;i<=n;i++){ cnt=0 ; for (int j=1 ;j<=n;j++) if (x[i]>x[j]&&y[i]>y[j])cnt++; printf ("%d\n" ,cnt); if (cnt>=maxn)maxn=cnt,maxj=i; } cout<<maxj; return 0 ; }
294.数组去重 1 2 3 4 5 6 7 8 long long n,m,maxn=-10e9 ,a[200005 ],b[100005 ];int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; for (int i=1 ;i<=n;i++) if (b[a[i]]==0 )cout<<a[i]<<" " ,b[a[i]]=1 ; return 0 ; }
295.开关灯 1 2 3 4 5 6 7 8 9 10 11 12 13 long long n,m,maxn=-10e9 ,a[100005 ];int main () { cin>>n>>m; for (int i=1 ;i<=m;i++) for (int j=1 ;j<=n;j++) if (j%i==0 )a[j]++,a[j]%=2 ; for (int i=1 ;i<=n;i++) if (a[i]){ if (i!=1 )cout<<"," ; cout<<i; } return 0 ; }
296.张灯结彩 1 2 3 4 5 6 7 8 9 long long a[100001 ],n,m,k,s;int main () { cin>>n>>m; for (int i=1 ;i<=m;i++)cin>>k,a[k]++,a[k]%=3 ; for (int i=1 ;i<=n-2 ;i++) if (a[i]!=a[i+1 ]&&a[i+1 ]!=a[i+2 ]&&a[i]!=a[i+2 ])s++; cout<<s; return 0 ; }
297.直方图 1 2 3 4 5 6 7 long long n,maxn=-10e9 ,a[100005 ],b[100005 ];int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i],b[a[i]]++,maxn=max (maxn,a[i]); for (int i=0 ;i<=maxn;i++)cout<<b[i]<<"\n" ; return 0 ; }
298.白细胞计数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 double n,a[100001 ],sum=0 ,maxn,minn=10e9 ;int maxj,minj;int main () { cin>>n; for (int i=1 ;i<=n;i++){ scanf ("%lf" ,&a[i]); if (a[i]>maxn)maxn=a[i],maxj=i; if (a[i]<minn)minn=a[i],minj=i; sum=sum+a[i]; } maxn=0 ; sum=(sum-a[minj]-a[maxj])/(n-2 ); printf ("%.2lf " ,sum); for (int i=1 ;i<=n;i++) if (i!=maxj&&i!=minj) if (maxn<abs (a[i]-sum))maxn=abs (a[i]-sum); printf ("%.2lf" ,maxn); return 0 ; }
299.01数字串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 string s; int n,ans,cnt,sum;void max (int &a,int b,int i) { if (b>a)a=b,sum=i; } int main () { cin>>s; for (int i=0 ;i<s.size ();++i){ int a[2 ]; a[0 ]=a[1 ]=0 ; for (int j=0 ;i+j<s.size ();++j){ a[s[i+j]-'0' ]++; if (a[0 ]==a[1 ])max (ans,j+1 ,i); } } cout<<ans<<"\n" <<sum<<" " <<sum+ans-1 ; return 0 ; }
300.批量求素数 1 2 3 4 5 6 7 8 9 10 11 12 13 int n,prime[1000001 ],len;bool vis[1000001 ];int main () { cin>>n; for (int i=2 ;i<=n;i++) if (!vis[i]) for (int j=i+i;j<=n;j+=i)vis[j]=1 ; for (int i=2 ;i<=n;i++) if (!vis[i])prime[++len]=i; cout<<len<<'\n' ; for (int i=1 ;i<=len;i++)cout<<prime[i]<<' ' ; return 0 ; }
Page 4 301.进制转换1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;long long n;int st[105 ],top;int main () { cin>>n; if (n == 0 ) { cout<<0 ; return 0 ; } while (n) { st[++top]=n&1 ; n>>=1 ; } while (top) cout<<st[top--]; }
303.进制转换2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <bits/stdc++.h> using namespace std;long long n;int st[105 ],top;int main () { cin>>n; if (n == 0 ) st[++top]=0 ; while (n) { st[++top]=n&1 ; n>>=1 ; } int ans=0 ,k=0 ; for (int i=1 ; i<=top; i++) { if (st[i]==0 ) ans=max (ans, ++k); else k=0 ; } while (top) cout<<st[top--]; cout<<endl<<ans; }
303.进制转换3 1 2 3 4 5 6 7 8 9 #include <bits/stdc++.h> using namespace std;string s; int main () { cin>>s; int x=0 ; for (int i=0 ; i<s.size (); i++) x=(x<<1 )|(s[i]^48 ); cout<<x; }
304.高低位交换 1 2 3 4 5 6 7 unsigned int n;int main () { cin>>n; cout<<(n>>16 )+(n<<16 ); return 0 ; }
305.二进制数简单操作 1 2 3 4 5 6 7 #include <bits/stdc++.h> using namespace std;int main () { unsigned x; cin>>x; cout<<(((x>>3 )&1 )<<3 | ((x>>4 )&1 )<<2 | ((x>>5 )&1 )<<1 | ((x>>6 )&1 )); }
306.十六进制转十进制 1 2 3 4 5 6 7 8 9 10 11 12 #include <bits/stdc++.h> using namespace std;char c;int ans;int main () { while (cin >> c) { if (c > 'a' ) ans=ans*16 +(c-'a' +10 ); else ans=ans*16 +(c-'0' ); } cout<<ans; return 0 ; }
307.二进制加法 反正数据很水,常数大就大吧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <bits/stdc++.h> using namespace std;string a,b,c; int main () { cin>>a>>b; reverse (a.begin (), a.end ()); reverse (b.begin (), b.end ()); int t=0 ; for (int i=0 ; i<a.size () || i<b.size () || t; i++) { if (i<a.size ()) t+=(a[i]-48 ); if (i<b.size ()) t+=(b[i]-48 ); c+=char (48 +(t&1 )); t>>=1 ; } reverse (c.begin (), c.end ()); cout<<c; return 0 ; }
308.“5421BCD”码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <bits/stdc++.h> using namespace std;int n, w[]={5 ,4 ,2 ,1 }, st[10 ], top;int main () { cin>>n; if (n==0 ) st[++top]=0 ; while (n) { st[++top]=n%10 ; n/=10 ; } for (int i=top; i>=1 ; i--) { int x=st[i]; for (int j=0 ; j<4 ; j++) { if (x>=w[j]) x-=w[j], putchar ('1' ); else putchar ('0' ); } } return 0 ; }
309.求一个数的反码与补码 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <bits/stdc++.h> using namespace std;void fm (int n, bool f) { for (int i=7 ; i>=0 ; i--) cout<<(((n>>i)&1 )^f); cout<<'\n' ; } int main () { int n; cin >> n; if (n < 0 ) fm (-n, 1 ), fm (-n-1 , 1 ); else fm (n, 0 ), fm (n, 0 ); return 0 ; }
310. 转八进制和十六进制数 printf 自带的功能
1 2 3 4 5 6 7 8 9 10 #include <bits/stdc++.h> using namespace std;int main () { for (int i=1 ,x; i<=8 ; i++) { scanf ("%d" ,&x); if (x>=0 ) printf ("%o %x\n" ,x,x); else printf ("-%o -%x\n" ,-x,-x); } return 0 ; }
311. 十六进制转换 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include<bits/stdc++.h> using namespace std; string s, s2; int a[1000005], top; int main(){ cin>>s; for(int i=s.size()-1; i>=0; i--) { int val = s[i]>='A' ? s[i]-'A'+10 : s[i]-'0'; for(int j=0; j<4; j++) a[++top]=val&1, val>>=1; } while(top>1 && a[top]==0) top--; for(int i=1; i<=top; i+=3) { int val = a[i]|a[i+1]<<1|a[i+2]<<2; s2+=(val+'0'); } reverse(s2.begin(), s2.end()); cout<<s2; return 0; }//cbs
312.火星A+B 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 int prime[101 ],a[101 ],b[101 ],c[101 ],lena,lenb,lenc,k=1 ,vis[1001 ];string s1,s2,ans; int E_sieve (int n) { for (int i=2 ;i*i<=n;i++) if (!vis[i])for (int j=i*i;j<=n;j+=i)vis[j]=1 ; for (int i=2 ;i<=n;i++) if (!vis[i])prime[k++]=i; return k; } int main () { E_sieve (100 ); while (1 ){ cin>>s1>>s2; if (s1=="0" &&s2=="0" )return 0 ; s1+="," ,s2+="," ; memset (a,0 ,sizeof a); memset (b,0 ,sizeof b); memset (c,0 ,sizeof c); lenc=lenb=lena=0 ; int num=0 ; for (int i=0 ;i<s1. size ();i++){ if (s1[i]==',' )a[++lena]=num,num=0 ; else num*=10 ,num+=s1[i]-'0' ; } num=0 ; for (int i=0 ;i<s2. size ();i++){ if (s2[i]==',' )b[++lenb]=num,num=0 ; else num*=10 ,num+=s2[i]-'0' ; } for (int i=1 ;i<=ceil (lena/2.0 );i++)swap (a[i],a[lena+1 -i]); for (int i=1 ;i<=ceil (lenb/2.0 );i++)swap (b[i],b[lenb+1 -i]); lenc=max (lena,lenb); for (int i=1 ;i<=lenc;i++){ c[i]+=a[i]+b[i]; if (c[i]>=prime[i])c[i+1 ]=c[i]/prime[i],c[i]%=prime[i]; } if (c[lenc+1 ]==1 )lenc++; for (int i=lenc;i>=1 ;i--){ if (i!=lenc)printf ("," ); printf ("%d" ,c[i]); } puts ("" ); } return 0 ; }
313.斜二进制数(shu) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;string s; long long x,b=2 ;int main () { cin>>s; for (int i=s.size ()-1 ; i>=0 ; i--, b<<=1 ) { x+=(s[i]^48 )*(b-1 ); if (x>2147483647 ) { cout<<"too long!" ; return 0 ; } } cout<<x; return 0 ; }
314.巧填九宫格 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <bits/stdc++.h> using namespace std;int main () { printf (R"(192 384 576 219 438 657 273 546 819 327 654 981 4 )" ); return 0 ; }
315.数组练习2–加强版 1 2 3 4 5 6 7 8 9 10 11 12 long long a[50001 ],n,m,k,q;int main () { cin>>n>>m; for (int i=1 ;i<=n;i++){ scanf ("%lld" ,&k); a[i]=max (a[i-1 ],k); } for (int i=1 ;i<=m;i++){ scanf ("%lld" ,&q); cout<<a[q]<<"\n" ; } return 0 ; }
316.和值最大的连续子序列 l 到 r 的和相当于 s[r]-s[l-1],所以只需要枚举 r,顺便处理前面最小的 s[l-1]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;int n,a[100005 ],s[100005 ],ans,sum,ansl,ansr;int main () { cin>>n; for (int i=1 ; i<=n; i++) cin>>a[i], s[i]=s[i-1 ]+a[i]; int tmp=0 ; for (int i=1 ; i<=n; i++) { if (s[i]<s[tmp]) tmp=i; if (s[i]-s[tmp]>ans) ans=s[i]-s[tmp], ansl=tmp+1 , ansr=i; } cout<<ans<<'\n' <<ansl<<' ' <<ansr<<'\n' ; for (int i=ansl; i<=ansr; i++) cout<<a[i]<<' ' ; cout<<'\n' ; return 0 ; }
317.表演 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <bits/stdc++.h> using namespace std;int n,m,p,a,cnt,ans;int main () { cin>>n>>m>>p; for (int i=1 ; i<=n; i++) { cin>>a; if (a>=p) cnt++; else ans+=max (0 ,cnt-m+1 ), cnt=0 ; } ans+=max (0 ,cnt-m+1 ), cnt=0 ; cout<<ans; return 0 ; }
318.二分查找Ⅱ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;int n,a[100005 ],x,c;int main () { cin>>n; for (int i=1 ; i<=n; i++) cin>>a[i]; cin>>x; int l=1 ,r=n; while (l<=r) { int mid=l+r>>1 ; ++c; if (a[mid]==x) { printf ("%d %d\n" , mid, c); return 0 ; } else if (a[mid]<x) { l=mid+1 ; } else { r=mid-1 ; } } return 0 ; }
319.二分找第K大数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;int n, k, a[200005 ], x;string s; int main () { cin>>n>>s; n=0 ; s=s+',' ; for (int i=0 ; i<s.length (); i++) { if (s[i]!=',' ) x=x*10 +(s[i]^48 ); else a[++n]=x, x=0 ; } cin>>k; sort (a+1 , a+1 +n); cout<<a[n+1 -k]; return 0 ; }
320.游戏闯关 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int n,t,a[1000005 ],b[10000005 ],tmp,ans;int main () { cin>>n>>t; for (int i=1 ;i<=n;i++)cin>>a[i],a[i]+=a[i-1 ]; for (int j=1 ;j<=n;j++)cin>>b[j],b[j]+=b[j-1 ]; for (int i=1 ;i<=n;i++) if (a[i]<=t) tmp=upper_bound (b,b+1 +n,t-a[i])-b-1 , ans=max (ans,tmp+i); for (int i=1 ;i<=n;i++){ if (b[i]<=t) tmp=upper_bound (a,a+1 +n,t-b[i])-a-1 , ans=max (ans,tmp+i); } return cout<<ans,0 ; }
321.数的三次方根 1 2 3 4 5 6 7 8 9 10 #include <bits/stdc++.h> using namespace std;int main () { double x; cin>>x; if (x >= 0 ) printf ("%lf" ,pow (x,1.0 / 3 )); else printf ("-%lf" ,pow (-x,1.0 / 3 )); return 0 ; }
322.数的范围 本题还有一种二分的做法,但是线性预处理更快
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;int n,q,a[100005 ],l[10005 ],r[10005 ];int main () { cin>>n>>q; for (int i=0 ; i<n; i++) cin>>a[i]; memset (l,-1 ,sizeof l), memset (r,-1 ,sizeof r); for (int i=0 ; i<n; i++) r[a[i]]=i; for (int i=n-1 ; i>=0 ; i--) l[a[i]]=i; while (q--) { int x; cin>>x; cout<<l[x]<<' ' <<r[x]<<'\n' ; } return 0 ; }
323.康托展开 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include <bits/stdc++.h> using namespace std;typedef long long LL;const int N = 110 ;LL tr[N]; int n;LL fact[N] = {1 ,1 }; int a[N];int lowbit (int x) { return x & (-x); } void add (LL x,int y) { while (x <= n) { tr[x] += y; x += lowbit (x); } } LL query (int x) { LL res = 0 ; while (x) { res += tr[x]; x -= lowbit (x); } return res; } int main () { cin>>n; for (int i = 1 ; i <= n; i ++) { fact[i] = fact[i - 1 ] * i; add (i,1 ); } LL ans = 0 ; for (int i = 1 ; i <= n; i ++) { cin>>a[i]; ans += (query (a[i]) - 1 ) * fact[n - i]; add (a[i],-1 ); } cout<<ans + 1 <<endl; return 0 ; }
324.日历问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include <bits/stdc++.h> using namespace std;const char * M[7 ] = {"Sunday" ,"Monday" ,"Tuesday" ,"Wednesday" ,"Thursday" ,"Friday" ,"Saturday" };int y=2000 ,m=1 ,d=1 ;int getday () { if (m == 2 ){ if ((y%4 == 0 && y%100 != 0 ) || y%400 == 0 ) return 29 ; else return 28 ; }else if (m == 4 || m == 6 || m == 9 || m == 11 ){ return 30 ; }else { return 31 ; } } int main () { int x; cin >> x; d += x; while (d > getday ()){ d -= getday (); m++; if (m > 12 ){ m = 1 ; y++; } } printf ("%d-%02d-%02d %s" ,y,m,d,M[(x+6 )%7 ]); return 0 ; }
325.车牌摇号系统 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;int n,m,input,ans=0 ;long long a[5005 ];bool c[5005 ];int main () { cin>>n; for (int i=0 ; i<n; i++) cin>>a[i]; cin>>m; for (int i=0 ; i<m; i++){ cin>>input; if (c[input] == 0 ){ cout<<a[input-1 ]<<'\n' ; c[input] = 1 ; ans++; } } cout<<ans; return 0 ; }
326.猴子选大王 比较麻烦的模拟题,本题解使用了三目运算符减少码量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;int n;int a[1010 ],i,j,k,m=0 ,t;int main () { cin >> n; if (n==1 ) cout << 1 ; for (i=0 ; i<n; i++) a[i]=i+1 ; for (i=0 ; i<n; i++) { if (m==n-2 ) for (j=(i&1 ? n-1 : 0 ); (i&1 ? j>=0 : j<n); (i&1 ? j-- : j++)) if (a[j]!=-1 ) return cout<<a[j], 0 ; t=0 ; for (j=(i&1 ? n-1 : 0 ); (i&1 ? j>=0 : j<n); (i&1 ? j-- : j++)) { if (a[j]!=-1 ) { t++; if (t==3 ) t=0 , a[j]=-1 , m++; } } } return 0 ; }
327.求补码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;string s; int main () { cin>>s; if (s[0 ]=='0' ) cout<<s; else { for (int i=15 ; i>=0 ; i--) { if (s[i]=='1' ) { s[i]='0' ; break ; } else { s[i]='1' ; } } for (int i=15 ; i>0 ; i--) s[i]=(s[i]^1 ); cout<<s; } return 0 ; }
328.统计数字字符个数 1 2 3 4 5 6 7 8 9 10 #include <bits/stdc++.h> using namespace std;string s; int n=0 ;int main () { getline (cin,s); for (auto i : s) n+=(i>='0' &&i<='9' ); cout<<n; return 0 ; }
329.统计字符 max_element 返回最大的值的指针,十分方便。
1 2 3 4 5 6 7 8 9 10 #include <bits/stdc++.h> using namespace std;char c;int n,ans,cnt[128 ];int main () { while ( cin>>c ) cnt[c]++; int * p = max_element (cnt, cnt+128 ); cout << char (p-cnt) << " " << *p; return 0 ; }
330.找第一个只出现一次的字符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;string s; int a[26 ];int main () { cin >> s; for (auto i : s) a[i-'a' ]++; for (auto i : s){ if (a[i-'a' ] == 1 ){ cout << i; return 0 ; } } cout << "no" ; return 0 ; }
331.提取身份证信息 1 2 3 4 5 6 7 8 #include <bits/stdc++.h> using namespace std;string a; int main () { cin >> a; printf ("Birthday is %d/%d" , (a[10 ]^48 )*10 +(a[11 ]^48 ), (a[12 ]^48 )*10 +(a[13 ]^48 )); return 0 ; }
332.分割数字 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <bits/stdc++.h> using namespace std;string s; int main () { cin >> s; int n = s.size (); for (int i=0 ; i<n; i++){ cout << s[i]; if ((i-(n%3 )+1 )%3 ==0 && i!=n-1 ) cout << "," ; } return 0 ; }
333.密码强度检测 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;string s; int a, b, c;int main () { cin >> s; if (s.size () < 8 ) { cout << "no" ; return 0 ; } for (int i=0 ; i<s.size (); i++) { if ((s[i] >= 69 && s[i] <= 90 )||(s[i] >= 97 && s[i] <= 122 )) a=1 ; else if (s[i] >= 48 && s[i] <= 57 ) b=1 ; else c=1 ; } if (a+b+c == 1 ) cout << "weak" ; else if (a+b+c == 2 ) cout << "moderate" ; else cout << "strong" ; return 0 ; }
334.压缩算法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <bits/stdc++.h> using namespace std;string s; char lx;int lx2;int main () { cin >> s; lx = s[0 ]; for (int i=0 ; i<s.size (); i++) { if (s[i] == lx) { lx2++; } else { cout<<lx2<<lx; lx=s[i], lx2=1 ; } } cout<<lx2<<lx; return 0 ; }
335.基因相关性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <bits/stdc++.h> using namespace std;string s1,s2; double n;int a;int main () { cin>>n>>s1>>s2; for (int i=0 ; i<s1.l ength(); i++) if (s1[i] == s2[i]) a++; if (double (a) / s1.l ength() > n) cout << "yes" ; else cout << "no" ; return 0 ; }
336.输出最高分数的学生姓名 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <bits/stdc++.h> using namespace std;int s,maxs,n2;string n,maxn; int main () { cin >> n2; for (int i=0 ; i<n2; i++){ cin >> s >> n; if (s>maxs){ maxs = s; maxn = n; } } cout << maxn; return 0 ; }
337.甲流病人初筛 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;string a; double b;int c,n,s;int main () { cin >> n; while (n--) { cin >> a >> b >> c; if (b >= 37.5 && c == 1 ){ cout << a << endl; s++; } } cout << s; return 0 ; }
338.石头剪子布 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <bits/stdc++.h> using namespace std;void calc (string a, string b) { if ((a == "Rock" && b == "Scissors" )||(a == "Scissors" && b == "Paper" )||(a == "Paper" && b == "Rock" )) cout << "Player1" ; else if (a==b) cout << "Tie" ; else cout << "Player2" ; } string a,b; int n;int main () { cin >> n; for (int i=0 ; i<n; i++){ cin >> a >> b; calc (a,b); cout << '\n' ; } return 0 ; }
339.复制字符串中的元音字母 1 2 3 4 5 6 7 8 9 10 #include <bits/stdc++.h> using namespace std;int main () { string s; getline (cin,s); for (int i=0 ; i<s.length (); i++) if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U' ) cout << s[i]; return 0 ; }
340.凯撒密码 1 2 3 4 5 6 7 8 9 10 11 12 #include <bits/stdc++.h> using namespace std;int n,k;char s[100005 ];int main () { scanf ("%d%s" ,&k,s); n=strlen (s); for (int i=0 ; i<n; i++) s[i]=(s[i]-'a' +k)%26 +'a' ; printf ("%s" ,s); return 0 ; }
341.回音体 注:直接使用字符指针减少码量
1 2 3 4 5 6 7 #include <bits/stdc++.h> char s[25 ];int main () { scanf ("%s" ,s); for (char * i=s; *i; i++) printf ("%s\n" ,i); return 0 ; }
342.背单词 1 2 3 4 5 6 7 8 9 10 #include <bits/stdc++.h> const char * a[] = {"January" ,"February" ,"March" ,"April" ,"May" ,"June" ,"July" ,"August" ,"September" ,"October" ,"November" ,"December" };char s[10 ];int main () { scanf ("%s" ,s); for (int i=0 ; i<12 ; i++) if (a[i][0 ]==s[0 ] && a[i][1 ]==s[1 ] && a[i][2 ]==s[2 ]) printf ("%s" , a[i]); return 0 ; }
343.输出亲朋字符串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <bits/stdc++.h> using namespace std;int main () { string s; getline (cin,s); for (int i=1 ; i<s.length (); i++){ cout << char (s[i-1 ] + s[i]); } cout << char (s[0 ] + s[s.length ()-1 ]); return 0 ; }
344.合法 C 标识符 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <bits/stdc++.h> using namespace std;int main () { int flag = 0 ,v; char ch[110 ]; cin>>ch; v = strlen (ch); if (ch[0 ] == '_' || isalpha (ch[0 ])) { for (int i = 1 ; i < v; i ++) { if (ch[i] == '_' || isalnum (ch[i])) continue ; else { flag = 1 ; break ; } } if (!flag) puts ("yes" ); else puts ("no" ); } else puts ("no" ); return 0 ; }
345.配对碱基链 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;string a; int main () { cin>>a; for (int i = 0 ; i < a.size (); i ++) { if (a[i] == 'A' ) cout<<'T' ; else if (a[i] == 'T' ) cout<<'A' ; else if (a[i] == 'G' ) cout<<'C' ; else cout<<'G' ; } puts ("" ); return 0 ; }
346.字符替换 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;string a; char b[100 ];int main () { char s1,s2; cin>>a>>s1>>s2; for (int i = 0 ; i < a.size (); i ++) { if (a[i] == s1) b[i] = s2; else b[i] = a[i]; } cout<<b<<endl; return 0 ; }
347.密码翻译 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;string a; char b[100 ];int main () { getline (cin,a); for (int i = 0 ; i < a.size (); i ++) { if (a[i] >= 'A' && a[i] < 'Z' || a[i] >= 'a' && a[i] < 'z' ) b[i] = a[i] + 1 ; else if (a[i] == 'Z' ) b[i] = 'A' ; else if (a[i] == 'z' ) b[i] = 'a' ; else b[i] = a[i]; } cout<<b<<endl; return 0 ; }
348.简单密码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <bits/stdc++.h> using namespace std;string s; int main () { getline (cin,s); for (int i=0 ; i<s.length (); i++){ if (s[i] >= 70 && s[i] <= 90 ) cout << char (s[i] - 5 ); else if (s[i] >= 65 && s[i] <= 69 ) cout << char (s[i] + 21 ); else cout << char (s[i]); } return 0 ; }
349.加密算法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;string s1,s2; int main () { getline ( cin , s1); s2="" ; for (int i=0 ; i<s1.l ength(); i++){ char s; if ('a' <= s1[i] && s1[i] <= 'z' ){ s = char ( int ( s1[i] ) - 32 ); s = char ( ( int ( s )- int ('A' ) +4 ) % 26 + int ('A' ) ); } else if ( 'A' <= s1[i] && s1[i]<= 'Z' ) s = char ( ( int (s1[i])- int ('A' ) +4 ) % 26 + int ('A' ) ); else if ( '0' <= s1[i] && s1[i]<= '9' ) s = char ( ( int (s1[i])- int ('0' ) +8 ) % 10 + int ('0' ) ); else s = s1[i]; s2 = s2 + s; } cout << s2; }
350.加密的病历单 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include <bits/stdc++.h> using namespace std;int main () { string s; getline (cin,s); for (int i=0 ; i<s.size (); i++){ if (islower (s[i])) s[i] -= 32 ; else s[i] += 32 ; } string s2 = "" ; for (int i=s.size ()-1 ; i>=0 ; i--) s2 += s[i]; s = s2; for (int i=0 ; i<s.size (); i++){ if (islower (s[i])) s[i] = (s[i]+3 -'a' )%26 +'a' ; else s[i] = (s[i]+3 -'A' )%26 +'A' ; } cout << s; return 0 ; }
351.将字符串中的小写字母转换成大写字母 1 2 3 4 5 6 7 8 #include <bits/stdc++.h> using namespace std;string s; int main () { getline (cin,s); for (auto &i : s) if (i>='a' && i<='z' ) i-=32 ; cout<<s; }
352.大小写字母互换 1 2 3 4 5 6 7 8 9 10 11 #include <bits/stdc++.h> using namespace std;string s; int main () { getline (cin,s); for (auto &i : s) { if (i>='a' && i<='z' ) i-=32 ; else if (i>='A' && i<='Z' ) i+=32 ; } cout<<s; }
353. 整理药名 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;int main () { string s; int n; cin>>n; for (int i=0 ;i<n;i++) { cin>>s; if (s[0 ]>='a' &&s[0 ]<='z' ) s[0 ]-=32 ; for (int j=1 ;j<s.size ();j++) if (s[j]>='A' &&s[j]<='Z' ) s[j]+=32 ; cout<<s<<endl; } return 0 ; }
354.忽略大小写的字符串比较 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <bits/stdc++.h> using namespace std;string s1,s2; int main () { getline (cin,s1); getline (cin,s2); for (int i=0 ; i<s1. size (); i++) if (islower (s1[i])) s1[i] -= 32 ; for (int i=0 ; i<s2. size (); i++) if (islower (s2[i])) s2[i] -= 32 ; if (s1 > s2) cout << ">" ; else if (s1 < s2) cout << "<" ; else cout << "=" ; return 0 ; }
355.字符串判等 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <bits/stdc++.h> using namespace std;string calc (string s) { string r="" ; for (int i=0 ; i<s.size (); i++) if (s[i]!=' ' ) r+=(s[i]>='a' &&s[i]<='z' ? s[i]-32 : s[i]); return r; } string s1,s2; int main () { getline (cin,s1); getline (cin,s2); cout<<(calc (s1)==calc (s2)?"YES" :"NO" ); }
356.验证子串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;int main () { string s1,s2; cin >> s1 >> s2; if (s2.f ind(s1) != -1 ) { cout << s1 << " is substring of " << s2; } else if (s1.f ind(s2) != -1 ) { cout << s2 << " is substring of " << s1; } else { cout << "No substring" ; } return 0 ; }
357.邮箱格式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include <bits/stdc++.h> using namespace std;int a=0 ,b=0 ;string s; int main () { cin >> s; for (int i=0 ; i<=s.size ()-1 ; i++){ if (s[i] == 64 ){ if (b || s[i+1 ] == 46 || i==0 || i==s.size ()-1 ){ cout << "Wrong" ; return 0 ; } a++; } if (s[i] == 46 ){ if (!a || s[i-1 ] == 64 || i==0 || i==s.size ()-1 ){ cout << "Wrong" ; return 0 ; } b++; } } if (!(a&&b)) cout << "Wrong" ; else cout << "OK" ; return 0 ; }
358.字符串移位包含问题 1 2 3 4 5 6 7 8 9 10 #include <bits/stdc++.h> using namespace std;int main () { string s,s2; cin >> s >> s2; s+=s; if (s.find (s2) != -1 ) cout << "true" ; else cout << "false" ; return 0 ; }
359.删除单词后缀 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;string s; int main () { cin >> s; if ((s.substr (s.size ()-2 )) == "er" ) cout << s.substr (0 ,s.size ()-2 ); if ((s.substr (s.size ()-2 )) == "ly" ) cout << s.substr (0 ,s.size ()-2 ); if ((s.substr (s.size ()-3 )) == "ing" ) cout << s.substr (0 ,s.size ()-3 ); return 0 ; }
360.单词替换 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;string s1,s2,s3,w; int p;int main () { getline (cin,s1); getline (cin,s2); getline (cin,s3); s1+=' ' ; for (int i=0 ; i<s1. size (); i++) { if (s1[i]==' ' ) { if (w==s2) cout<<s3<<' ' ; else cout<<w<<' ' ; w="" ; } else w+=s1[i]; } }
361.紧急措施 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;string e,x,y,z; int n,m;int main () { cin>>e>>n; for (int i=1 ; i<=n; i++) { cin>>x>>y>>z; if (z==e) { for (int j=0 ; j<y.size (); j++) { if (y[j]>='a' && y[j]<='z' ) y[j]-=32 ; else if (y[j]>='A' && y[j]<='Z' ) y[j]+=32 ; } ++m; cout<<x<<" " <<y<<'\n' ; } } if (m==0 ) cout<<"empty" ; return 0 ; }
362.过滤多余的空格 此方法效率不高,但是数据范围实在太小,随便怎么写都行
1 2 3 4 5 6 7 8 9 10 11 12 #include <bits/stdc++.h> using namespace std;int main () { string s; getline (cin,s); while (1 ){ if (s.find (" " ) == -1 ) break ; s = s.replace (s.find (" " ),2 ," " ); } cout << s; return 0 ; }
363.单词的长度 1 2 3 4 5 6 7 8 9 #include <bits/stdc++.h> using namespace std;string e; int main () { cin>>e; cout<<e.size (); while (cin>>e) cout<<"," <<e.size (); return 0 ; }
364.最长最短单词 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;string a,b,ma,mi; int main () { getline (cin,a); a+=' ' ; for (int i=0 ; i<a.size (); i++) { if (a[i]==',' || a[i]==' ' ) { if (b.size ()>ma.size ()) ma=b; if (mi.size ()==0 || b.size ()<mi.size ()) mi=b; b="" ; } else b+=a[i]; } cout<<ma<<'\n' <<mi; return 0 ; }
365.字符串最大跨距 使用 kmp,在线性时间内快速求解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include <bits/stdc++.h> using namespace std;string s,s1,s2,a; int p[320 ],l=-1 ,r=-1 ;void kmp () { for (int i=1 ; i<a.size (); i++) { int j=p[i-1 ]; while (j && a[j]!=a[i]) j=p[j-1 ]; p[i]=j+(a[i]==a[j]); } } int main () { getline (cin,s,',' ); getline (cin,s1,',' ); getline (cin,s2); a=s1+' ' +s, kmp (); for (int i=0 ; i<s.size (); i++) { if (p[i+s1. size ()+1 ]==s1. size ()) { l=i; break ; } } a=s2+' ' +s, kmp (); for (int i=s.size ()-1 ; i>=0 ; i--) { if (p[i+s2. size ()+1 ]==s2. size ()) { r=i; break ; } } if (l==-1 || r==-1 || l>=r-s2. size ()+1 ) cout<<-1 ; else cout<<r-s2. size ()-l; return 0 ; }
366.单词翻转 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <bits/stdc++.h> using namespace std;int main () { string s; while (cin >> s){ for (int i=s.size ()-1 ; i>=0 ; i--) cout << s[i]; cout << " " ; } return 0 ; }
367.单词倒排 1 2 3 4 5 6 7 8 9 10 11 12 #include <bits/stdc++.h> using namespace std;string in, s[5000 ]; int main () { int i=0 ; while (cin >> in) s[i++] = in; i--; while (i>=0 ) cout << s[i--] << " " ; return 0 ; }
368.字符串加密算法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;string s,xx = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ; char a,b,c;int num1,num2,num3,num4;int main () { getline (cin,s); for (int i=0 ; i<s.size (); i+=3 ) { a = s[i]; b = s[i+1 ]; c = s[i+2 ]; num1 = int (a)/4 ; num2 = (int (a)%4 )*16 +(int (b)/16 ); num3 = (int (b)%16 )*4 +(int (c)/64 ); num4 = int (c)%64 ; cout << xx[num1] << xx[num2] << xx[num3] << xx[num4]; } return 0 ; }
369.ISBN号码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;char _d;int a,b,c,d;int main () { scanf ("%d-%d-%d-%c" ,&a,&b,&c,&_d); if (_d == 'X' ) d = 10 ; else d = _d-48 ; int sum = a; sum += (b/100 )*2 + ((b/10 )%10 )*3 + (b%10 )*4 ; sum += (c/10000 )*5 + ((c/1000 )%10 )*6 + ((c/100 )%10 )*7 + ((c/10 )%10 )*8 + (c%10 )*9 ; sum %= 11 ; if (d == sum) cout << "Right" ; else if (sum == 10 ) printf ("%d-%d-%d-X" ,a,b,c); else printf ("%d-%d-%d-%d" ,a,b,c,sum); return 0 ; }
370.身份证号码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <bits/stdc++.h> using namespace std;int main () { char last; int n[17 ],sb[17 ] = {7 ,9 ,10 ,5 ,8 ,4 ,2 ,1 ,6 ,3 ,7 ,9 ,10 ,5 ,8 ,4 ,2 },sum=0 ; string s = "10x98765432" ; for (int i=0 ; i<17 ; i++) n[i] = getchar ()-48 ; last = getchar (); for (int i=0 ; i<17 ; i++) sum += n[i]*sb[i]; if (last == s[sum%11 ]){ printf ("%d%d%d%d-%d%d-%d%d," ,n[6 ],n[7 ],n[8 ],n[9 ],n[10 ],n[11 ],n[12 ],n[13 ]); if (n[16 ]%2 == 0 ) cout << "fe" ; cout << "male" ; } else cout << "wrong!" ; return 0 ; }
371.字符环 使用 dp 求解,时间复杂度 n^2
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <bits/stdc++.h> using namespace std;string a,b; int f[600 ][600 ],ans;int main () { cin>>a>>b; a+=a, b+=b; for (int i=1 ; i<=a.size (); i++) for (int j=1 ; j<=b.size (); j++) if (a[i-1 ]==b[j-1 ]) f[i][j]=f[i-1 ][j-1 ]+1 , ans=max (ans,f[i][j]); cout<<ans; return 0 ; }
372.字符串p型编码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <bits/stdc++.h> using namespace std;char s[1010 ];int cnt = 1 ;int main () { fgets (s,1000 ,stdin); for (int i = 0 ; i < strlen (s) - 1 ; i ++) { if (s[i] != s[i + 1 ]) cout<<cnt<<s[i],cnt = 1 ; else cnt ++; } return 0 ; }
373.行程长度编码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;string s; int main () { cin>>s; char x=0 ; int y=0 ; for (int i=0 ; i<s.size (); i++) { char cur=(s[i]>='a' ?s[i]-32 :s[i]); if (cur!=x) { if (y) printf ("(%c,%d)" ,x,y); x=cur,y=1 ; } else y++; } if (y) printf ("(%c,%d)" ,x,y); return 0 ; }
374.判断字符串是否为回文 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <bits/stdc++.h> using namespace std;int main () { string s,s2; cin >> s; s2 = s; reverse (s2. begin (),s2. end ()); if (s == s2) cout << "yes" ; else cout << "no" ; return 0 ; }
375.机动车违章识别系统 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;string s,name,day2; int n,day;char last;int main () { cout << "license number owner's name" << endl; cin >> n; for (int i=1 ; i<=n; i++) { cin >> s >> name >> day2 >> day; for (int j=s.length ()-1 ; j>=0 ; j--) { if (isdigit (s[j])) { last = s[j]; break ; } } if (day<6 && ((s[4 ]!='B' ) || (last==48 +day || last==58 -day) || (day==5 || last=='0' ))) cout << s << " " << name << endl; } return 0 ; }
376.构造回文数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <bits/stdc++.h> using namespace std;string s; string make (string x) { for (int i=(x.size ()>>1 )+(x.size ()&1 ); i<x.size (); i++) x[i]=x[x.size ()-1 -i]; return x; } int main () { cin>>s; int f=1 ; for (int i=0 ; i<s.size (); i++) { if (s[i]!='9' ) { f=0 ; break ; } } if (f) { putchar ('1' ); for (int i=0 ; i<s.size ()-1 ; i++) putchar ('0' ); putchar ('1' ); return 0 ; } if (make (s) <= s) { s[s.size ()>>1 ]+=1 ; for (int i=s.size ()>>1 ; i>0 ; i--) { if (s[i]>'9' ) s[i]='0' , s[i-1 ]++; } } cout<<make (s); return 0 ; }
377.回文子串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <bits/stdc++.h> using namespace std;void x (string s) { string s2=s; reverse (s2. begin (),s2. end ()); if (s == s2) cout << s << endl; } int main () { int i,j; string s; getline (cin,s); for (i=2 ; i<s.size (); i++) for (j=0 ; j<s.size ()-i+1 ; j++) x (s.substr (j,i)); return 0 ; }
378.模拟登录 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <bits/stdc++.h> using namespace std;string n,p; int main () { cin>>n>>p; while (p!="123456" ) { puts ("Wrong Password!" ); if (p=="0" ) return 0 ; cin>>p; } cout<<"Hello " <<n; return 0 ; }
379.整数提取 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;string s; long long num[999 ];int n;int main () { getline (cin,s); for (int i=0 ; i<s.size (); i++) { if (s[i] >= 48 && s[i] <= 57 ) num[n] = num[n]*10 +(s[i]-48 ); else if (s[i-1 ] >= 48 && s[i-1 ] <= 57 ) n++; } if (s[s.size ()-1 ] >= 48 && s[s.size ()-1 ] <= 57 ) n++; cout << n; for (int i=0 ; i<n; i++) cout << endl << num[i]; return 0 ; }
380.统计单词数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <bits/stdc++.h> using namespace std;string a,b,c; int p,cnt,x=-1 ;int main () { getline (cin,a); getline (cin,b); for (int i=0 ; i<a.size (); i++) if (a[i]>='a' ) a[i]-=32 ; for (int i=0 ; i<b.size (); i++) { if (b[i]>='a' ) b[i]-=32 ; if (b[i]==' ' ) { if (c==a) { if (x==-1 ) x=i-a.size (); cnt++; } c="" ; } else c+=b[i]; } if (x==-1 ) cout<<-1 ; else cout<<cnt<<" " <<x; return 0 ; }
381.找子串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <bits/stdc++.h> using namespace std;int main () { string s; cin >> s; for (int i=1 ; i<=s.size (); i++){ if (s.size ()%i) continue ; int f=1 ; for (int j=i; j<s.size (); j+=i) { if (s.substr (0 ,i) != s.substr (j,i)) { f=0 ; break ; } } if (f){ cout << s.size ()/i; break ; } } return 0 ; }
382.三倍子串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;string a; int s,c[3 ];long long ans;int main () { cin>>a; c[0 ]++; for (int i=0 ; i<a.size (); i++) { s=(s+(a[i]^48 ))%3 ; ans+=c[s]; c[s]++; } cout<<ans; return 0 ; }
383.PER 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <bits/stdc++.h> using namespace std;string a; int ans;int main () { cin>>a; for (int i=0 ; i<a.size (); i++) { if (i%3 ==0 && a[i]!='P' ) ans++; if (i%3 ==1 && a[i]!='E' ) ans++; if (i%3 ==2 && a[i]!='R' ) ans++; } cout<<ans; return 0 ; }
384.查找 提示:前缀可以用 trie 树维护,时间复杂度为线性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <bits/stdc++.h> using namespace std;int n,m,ch[100005 ][10 ],tot,a[10005 ];int calc () { cin>>m; for (int i=1 ; i<=m; i++) { string k; cin>>k; int p=0 ; for (auto j : k) { if (!ch[p][j^48 ]) ch[p][j^48 ]=++tot; p=ch[p][j^48 ]; } a[i]=p; } for (int i=1 ; i<=m; i++) for (int j=0 ; j<=9 ; j++) if (ch[a[i]][j]) return 0 ; return 1 ; } int main () { cin>>n; while (n--) { tot=0 ; memset (ch,0 ,sizeof ch); if (calc ()) puts ("YES" ); else puts ("NO" ); } return 0 ; }
385.IP地址判断 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include <bits/stdc++.h> using namespace std;const int N = 1010 ;string s; map<string,int > mp; int n;bool check (string st) { int t[5 ]; int a = sscanf (st.c_str (),"%d.%d.%d.%d:%d" ,&t[0 ],&t[1 ],&t[2 ],&t[3 ],&t[4 ]); if (a != 5 ) return false ; if (t[0 ] < 0 || t[1 ] < 0 || t[2 ] < 0 || t[3 ] < 0 || t[4 ] < 0 ) return false ; if (t[0 ] > 255 || t[1 ] > 255 || t[2 ] > 255 || t[3 ] > 255 || t[4 ] > 65535 ) return false ; char str[50 ]; sprintf (str,"%d.%d.%d.%d:%d" ,t[0 ],t[1 ],t[2 ],t[3 ],t[4 ]); if (!strcmp (str,st.c_str ())) return true ; return false ; } int main () { cin>>n; for (int i = 1 ; i <= n; i ++) { cin>>s; if (!check (s)) puts ("no" ); else puts ("yes" ); } return 0 ; }
386.潜伏者 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include <bits/stdc++.h> using namespace std;char s[128 ],s2[128 ];string a,b,c,d; int main () { cin>>a>>b>>c; for (int i=0 ; i<b.size (); i++) { if (s[b[i]] && s[b[i]]!=a[i]) { puts ("Failed" ); return 0 ; } s[b[i]]= a[i]; if (s2[a[i]] && s2[a[i]]!=b[i]) { puts ("Failed" ); return 0 ; } s2[a[i]]=b[i]; } for (int i='A' ; i<='Z' ; i++) if (!s[i]) { puts ("Failed" ); return 0 ; } for (int i=0 ; i<c.size (); i++) cout<<s2[c[i]]; return 0 ; }
387.字符串哈希 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <bits/stdc++.h> using namespace std;const int M=998244353 , P=131 ;int h[100005 ],pw[100005 ];char s[100005 ];int n,m;int main () { scanf ("%d%d%s" ,&n,&m,s+1 ); for (int i=1 ; i<=n; i++) h[i]=(1ll *h[i-1 ]*P+s[i])%M; pw[0 ]=1 ; for (int i=1 ; i<=n; i++) pw[i]=1ll *pw[i-1 ]*P%M; for (int i=1 ; i<=m; i++) { int a,b,c,d; scanf ("%d%d%d%d" ,&a,&b,&c,&d); a--, c--; if (((h[b]-1ll *h[a]*pw[b-a])-(h[d]-1ll *h[c]*pw[d-c]))%M == 0 ) { puts ("Yes" ); } else { puts ("No" ); } } return 0 ; }
388.表达式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <bits/stdc++.h> #define int long long using namespace std;int a,b,c;int to (int base, int x) { int y=0 , i=1 ; while (x) { y+=i*(x%16 ); x/=16 ; i*=base; } return y; } int ck (int base, int x) { while (x) { if (x%16 >=base) return 0 ; x/=16 ; } return 1 ; } signed main () { scanf ("%x%x%x" ,&a,&b,&c); for (int base=2 ; base<=16 ; base++) { if (ck (base,a) && ck (base,b) && ck (base,c) && to (base,a)*to (base,b) == to (base,c)) { cout<<base; return 0 ; } } cout<<0 ; return 0 ; }
389.画矩形 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;string str (char c, int x) { string s = "" ; for (int i=0 ; i<x; i++) s += c; return s; } int x,y,b;char c;int main () { cin >> y >> x >> c >> b; cout << str (c,x); for (int i=0 ; i<y-2 ; i++){ cout << endl << c; if (b) cout << str (c,x-2 ); else cout << str (' ' ,x-2 ); cout << c; } cout << endl << str (c,x); return 0 ; }
390.简易五子棋游戏 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;int l[9 ][9 ],n,x,y,color=1 ;int main () { cin >> n; for (int i=0 ; i<n; i++){ cin >> x >> y; l[x][y] = color; color = 3 -color; for (int j=1 ; j<=5 ; j++) printf ("[%d, %d, %d, %d, %d]\n" ,l[j][1 ],l[j][2 ],l[j][3 ],l[j][4 ],l[j][5 ]); if (i != n-1 ) cout << endl; } return 0 ; }
391.杨辉三角形 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <bits/stdc++.h> using namespace std;int num[1000 ][1000 ];int main () { num[0 ][0 ] = 1 ; int n; cin >> n; for (int i=1 ; i<=n; i++){ for (int j=1 ; j<=i; j++){ num[i][j] = num[i-1 ][j-1 ] + num[i-1 ][j]; cout << num[i][j] << " " ; } cout << endl; } return 0 ; }
392.矩阵加法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;int x, y, a[105 ][105 ], b[105 ][105 ];int main () { cin >> x >> y; for (int i=0 ; i<x; i++) for (int j=0 ; j<y; j++) cin >> a[i][j]; for (int i=0 ; i<x; i++) for (int j=0 ; j<y; j++) cin >> b[i][j]; for (int i=0 ; i<x; i++){ for (int j=0 ; j<y; j++){ cout << a[i][j] + b[i][j] << " " ; } cout << endl; } return 0 ; }
393.最长非零串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;int n,m,k,a[1005 ];int main () { cin>>n>>m>>k; for (int i=1 ; i<=n; i++) { for (int j=1 ; j<=m; j++) cin>>a[j]; if (i==k) { int x=0 ,mx=0 ; for (int j=1 ; j<=m; j++) { if (a[j]) x++, mx=max (mx,x); else x=0 ; } cout<<mx; return 0 ; } } return 0 ; }
394.矩阵乘法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <bits/stdc++.h> using namespace std;int n,m,k;int a[105 ][105 ],b[105 ][105 ],c[105 ][105 ];int main () { cin >> n >> m >> k; for (int i=0 ; i<n; i++) for (int j=0 ; j<m; j++) cin >> a[i][j]; for (int i=0 ; i<m; i++) for (int j=0 ; j<k; j++) cin >> b[i][j]; for (int i=0 ; i<n; i++) for (int j=0 ; j<k; j++) for (int i2=0 ; i2<m; i2++) c[i][j] += a[i][i2] * b[i2][j]; for (int i=0 ; i<n; i++) { for (int j=0 ; j<k; j++) cout << c[i][j] << " " ; cout << endl; } return 0 ; }
395.一维数组转二维数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;int a[15 ][15 ];int main () { for (int i=0 ; i<3 ; i++) for (int j=0 ; j<4 ; j++) cin >> a[i][j]; for (int i=0 ; i<3 ; i++) { for (int j=0 ; j<4 ; j++) printf ("%4d" ,a[i][j]); printf ("\n" ); } return 0 ; }
396.一维数组转二维数组2 1 2 3 4 5 6 7 8 #include <bits/stdc++.h> using namespace std;int i,j,a[3 ][4 ]={1 , 3 , 5 , 7 , 9 , 11 , 13 , 15 , 17 , 19 , 21 , 23 };int main () { cin>>i>>j; cout<<a[i][j]; return 0 ; }
397.缺席多少人1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;bool a[105 ][105 ];int n,m,sum;int main () { cin>>n>>m; for (int i=0 ; i<n; i++){ for (int j=0 ; j<m; j++) cout<<sum<<" " ; cout<<endl; for (int j=0 ; j<m; j++){ cin>>a[i][j]; sum += !a[i][j]; } } return 0 ; }
398.缺席多少人 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;int a[1505 ][1505 ],sum[1505 ][1505 ];int n,m;int main () { scanf ("%d %d" ,&n,&m); for (int i=0 ; i<n; i++) for (int j=0 ; j<m; j++) scanf ("%d" ,&a[i][j]); for (int i=0 ; i<n; i++) for (int j=0 ; j<m; j++) sum[i][j] = !a[i][j] + sum[i][j-1 ] + sum[i-1 ][j] - sum[i-1 ][j-1 ]; for (int i=0 ; i<n; i++){ for (int j=0 ; j<m; j++){ printf ("%d " ,sum[i][j]); } printf ("\n" ); } return 0 ; }
399.缺席多少人 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <bits/stdc++.h> using namespace std;int a[1505 ][1505 ],b[1505 ][1505 ];int main () { int x,y,k,x1,x2,y1,y2; cin >> x >> y; for (int i=1 ; i<=x; i++){ for (int j=1 ; j<=y; j++){ scanf ("%d" ,&a[i][j]); b[i][j] = b[i][j-1 ] + b[i-1 ][j] - b[i-1 ][j-1 ] + (1 -a[i][j]); } } cin >> k; for (int i=1 ; i<=k; i++){ scanf ("%d %d %d %d" ,&x1,&y1,&x2,&y2); printf ("%d\n" ,b[x2][y2]-b[x2][y1-1 ]-b[x1-1 ][y2]+b[x1-1 ][y1-1 ]); } return 0 ; }
400.镂空三角形 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <bits/stdc++.h> using namespace std;void print (char c, int n) { for (int i=0 ; i<n; i++) cout << c; } int main () { char c; int n; while (1 ){ cin >> c; if (c == '@' ) break ; cin >> n; for (int i=1 ; i<=n; i++){ print (' ' ,((2 *n-1 )-(2 *i-1 ))/2 ); cout << c; if (i == n) print (c,2 *i-3 ); else print (' ' ,2 *i-3 ); if (i > 1 ) cout << c; cout << endl; } cout << endl; } return 0 ; }
Page 5 401.找最大最小的数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <bits/stdc++.h> using namespace std;Qint a[110 ][110 ];int main () { int m,n,maxn=-1 ,minn=10001 ,maxnn,maxnm,minnn,minnm; cin>>n>>m; for (int i=1 ; i<=n; i++) { for (int j=1 ; j<=m; j++) { int t; cin>>t; if (maxn<t) { maxn=t; maxnn=i; maxnm=j; } if (minn>t) { minn=t; minnn=i; minnm=j; } } } cout<<maxnn<<" " <<maxnm<<" " <<maxn<<endl<<minnn<<" " <<minnm<<" " <<minn; return 0 ; }
402. 矩阵内部调整 提示:适当利用 max_element、swap 等 STL 函数,可以简化代码的实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;int a[30 ];void find (int x, int y, int z, int to) { int m=1e9 ,k; for (int i=0 ; i<25 ; i++) if (a[i]<m && i!=x && i!=y && i!=z) m=a[i], k=i; swap (a[k], a[to]); } int main () { for (int i=0 ; i<25 ; i++) scanf ("%d" ,&a[i]); find (-1 ,-1 ,-1 ,0 ), find (0 ,-1 ,-1 ,4 ), find (0 ,4 ,-1 ,20 ), find (0 ,4 ,20 ,24 ); swap (*max_element (a,a+25 ), a[12 ]); for (int i=0 ; i<25 ; i++) printf ("%d%c" , a[i], i%5 ==4 ?'\n' :' ' ); return 0 ; }
403. 商品关联次数统计 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include <bits/stdc++.h> using namespace std;int m,n,t,an[505 ][505 ],b[505 ];int main () { scanf ("%d %d %d" ,&n,&m,&t); while (m--) { int a=0 ,cnt=0 ; string s; cin >> s; for (int i=0 ; i<s.size (); i++) { if (s[i] >= '0' && s[i] <= '9' ) a = a*10 +s[i]-'0' ; else if (a!=0 ) { b[++cnt] = a; a = 0 ; } } b[++cnt] = a; for (int i = 1 ; i<=cnt; i++) for (int j=i+1 ; j<=cnt; j++) ++an[b[i]][b[j]], ++an[b[j]][b[i]]; } while (t--) { int e,f; scanf ("%d %d" ,&e,&f); printf ("%d\n" ,an[e][f]); } return 0 ; }
404. 子矩阵 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <bits/stdc++.h> using namespace std;int n,m,n2,m2,a[25 ][25 ],b[25 ][25 ];int main () { cin >> n >> m; for (int i=0 ; i<n; i++) for (int j=0 ; j<m; j++) cin >> a[i][j]; cin >> n2 >> m2; for (int i=0 ; i<n2; i++) for (int j=0 ; j<m2; j++) cin >> b[i][j]; int f2=0 ; for (int i=0 ; i<n; i++){ for (int j=0 ; j<m; j++){ int f=0 ; for (int k=0 ; k<n2; k++) for (int l=0 ; l<m2; l++) if (a[i+k][j+l] != b[k][l]) f=1 ; if (!f){ cout << i+1 << " " << j+1 << endl; f2=1 ; } } } if (!f2) puts ("There is no answer" ); return 0 ; }
405. 九宫格 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;int T, a[3 ][3 ];int check () { int s=a[0 ][0 ]+a[0 ][1 ]+a[0 ][2 ]; return a[1 ][0 ]+a[1 ][1 ]+a[1 ][2 ]==s && a[2 ][0 ]+a[2 ][1 ]+a[2 ][2 ]==s && a[0 ][0 ]+a[1 ][0 ]+a[2 ][0 ]==s && a[0 ][1 ]+a[1 ][1 ]+a[2 ][1 ]==s && a[0 ][2 ]+a[1 ][2 ]+a[2 ][2 ]==s && a[0 ][0 ]+a[1 ][1 ]+a[2 ][2 ]==s && a[0 ][2 ]+a[1 ][1 ]+a[2 ][0 ]==s; } int main () { cin>>T; while (T--) { for (int i=0 ; i<3 ; i++) for (int j=0 ; j<3 ; j++) cin>>a[i][j]; if (check ()) puts ("Cheers!" ); else puts ("OMyGa!" ); } return 0 ; }
406.对弈 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 long long n,m,a[105 ][105 ],A,B;int main () { cin>>n>>m; for (int i=1 ;i<=n/2 ;i++) for (int j=1 ;j<=m;j++)scanf ("%lld" ,&a[i][j]); for (int i=n/2 +1 ;i<=n;i++){ for (int j=1 ;j<=m;j++){ scanf ("%lld" ,&a[i][j]); if (n&1 &&i!=n/2 +1 ||n%2 ==0 ){ A+=(a[n-i+1 ][j]>a[i][j]); B+=(a[n-i+1 ][j]<a[i][j]); } } } cout<<A<<":" <<B<<" " ; if (A>B)cout<<"A is winner" ; else if (A<B)cout<<"B is winner" ; else cout<<"In a draw" ; return 0 ; }
407. 叠筐 提示:注意 n=1 的情况。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;int n;char a, b;int main () { while (1 ) { cin>>n; if (!n) return 0 ; cin>>a>>b; if (n==1 ) { putchar (a), putchar ('\n' ); } else { for (int i=1 ; i<=n; i++) { for (int j=1 ; j<=n; j++) { if ((i==1 || i==n) && (j==1 || j==n)) putchar (' ' ); else if (abs (n/2 +1 -j) <= abs (n/2 +1 -i)) putchar (abs (n/2 +1 -i)&1 ? b : a); else putchar (abs (n/2 +1 -j)&1 ? b : a); } putchar ('\n' ); } } putchar ('\n' ); } }
408. 分数矩阵 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <bits/stdc++.h> using namespace std;int n;int main () { while (1 ){ cin >> n; if (n == 0 ) break ; double a=1 ; double sum = 0 ; for (double i=n; i>=1 ; i--){ if (i == n) sum += 1.0 /a*i; else sum += 1.0 /a*i*2 ; a++; } printf ("%0.2lf\n" ,sum); } return 0 ; }
409. 放大的X 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include <bits/stdc++.h> using namespace std;void ps (int n) { for (int i=0 ; i<n; i++) cout << " " ; } void X (int n) { for (int i=0 ; i<n/2 ; i++){ ps (i); cout << "X" ; ps (n-i-i-2 ); cout << "X" ; ps (i); cout << endl; } ps (n/2 ); cout << "X" ; ps (n/2 ); cout << endl; for (int i=n/2 -1 ; i>=0 ; i--){ ps (i); cout << "X" ; ps (n-i-i-2 ); cout << "X" ; ps (i); cout << endl; } } int main () { std::ios::sync_with_stdio (false ); int n,a[999 ]; cin >> n; for (int i=0 ; i<n; i++) cin >> a[i]; for (int i=0 ; i<n; i++){ X (a[i]); cout << endl; } return 0 ; }
410. 递增方阵 提示:求出每个点到边缘的距离。
1 2 3 4 5 6 7 8 9 10 11 12 13 #include <bits/stdc++.h> using namespace std;int n;int main () { ios::sync_with_stdio (0 ), cin.tie (0 ), cout.tie (0 ); cin>>n; for (int i=1 ; i<=n; i++) { for (int j=1 ; j<=n; j++) cout<<min ({i,j,n+1 -i,n+1 -j})<<' ' ; cout<<'\n' ; } return 0 ; }
411. 矩阵归零消减序列和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <bits/stdc++.h> using namespace std;int n,a[105 ][105 ];int main () { cin>>n; for (int i=1 ; i<=n; i++) for (int j=1 ; j<=n; j++) cin>>a[i][j]; while (n>=1 ) { cout<<a[2 ][2 ]<<'\n' ; for (int i=1 ; i<=n; i++) { int minn=1e9 ; for (int j=1 ; j<=n; j++) minn=min (minn, a[i][j]); for (int j=1 ; j<=n; j++) a[i][j]-=minn; } for (int i=1 ; i<=n; i++) { int minn=1e9 ; for (int j=1 ; j<=n; j++) minn=min (minn, a[j][i]); for (int j=1 ; j<=n; j++) a[j][i]-=minn; } for (int i=1 ; i<=n; i++) for (int j=3 ; j<=n; j++) a[i][j-1 ]=a[i][j]; for (int i=1 ; i<=n; i++) for (int j=3 ; j<=n; j++) a[j-1 ][i]=a[j][i]; n--; } return 0 ; }
412. 找最大数序列 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include <bits/stdc++.h> using namespace std;int n;string s[55 ],maxn; bool cmp (string x, string y) { if (x.size () > y.size ()) return 1 ; if (x.size () < y.size ()) return 0 ; return x>y; } int main () { cin>>n; for (int i=1 ; i<=n; i++) { cin>>s[i]; s[i]+="," ; string cur="" ; for (auto j:s[i]) { if (j==',' ) { if (cmp (cur, maxn)) maxn=cur; cur="" ; } else { cur+=j; } } } cout<<maxn<<'\n' ; for (int i=1 ,x=0 ; i<=n; i++) { int f=0 ; string cur="" ; for (auto j:s[i]) { if (j==',' ) { if (cur==maxn) { f=1 ; break ; } cur="" ; } else { cur+=j; } } if (f) { if (x) cout<<"," ; x=1 ; cout<<i; } } return 0 ; }
413. 矩阵转置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;int a[105 ][105 ],b[105 ][105 ];int n,m;int main () { cin >> n >> m; for (int i=0 ; i<n; i++) for (int j=0 ; j<m; j++) cin >> a[i][j]; for (int i=0 ; i<n; i++) for (int j=0 ; j<m; j++) b[j][i] = a[i][j]; for (int i=0 ; i<m; i++){ for (int j=0 ; j<n; j++) cout << b[i][j] << " " ; cout << endl; } return 0 ; }
414. 图像旋转 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #include <bits/stdc++.h> using namespace std;int a[105 ][105 ],b[105 ][105 ];int n,m;int main () { cin >> n >> m; for (int i=0 ; i<n; i++) for (int j=0 ; j<m; j++) cin >> a[i][j]; for (int i=0 ; i<n; i++) for (int j=0 ; j<m; j++) b[j][i] = a[i][j]; for (int i=0 ; i<m; i++){ for (int j=n-1 ; j>=0 ; j--) cout << b[i][j] << " " ; cout << endl; } return 0 ; }
415. 变幻的矩阵 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include <bits/stdc++.h> using namespace std;char a[105 ][105 ],b[105 ][105 ];int n,s1,s2,s3,s4;int main () { cin >> n; for (int i=0 ; i<n; i++) for (int j=0 ; j<n; j++) cin >> a[i][j]; for (int i=0 ; i<n; i++) for (int j=0 ; j<n; j++) cin >> b[i][j]; for (int i=0 ; i<n; i++) for (int j=0 ; j<n; j++) if (a[i][j] == b[j][n-i-1 ]) s1++; for (int i=0 ; i<n; i++) for (int j=0 ; j<n; j++) if (a[i][j] == b[n-j-1 ][i]) s2++; for (int i=0 ; i<n; i++) for (int j=0 ; j<n; j++) if (a[i][j] == b[n-i-1 ][n-j-1 ]) s3++; for (int i=0 ; i<n; i++) for (int j=0 ; j<n; j++) if (a[i][j] == b[i][j]) s4++; if (s1 == n*n)cout << "1" ; else if (s2 == n*n)cout << "2" ; else if (s3 == n*n)cout << "3" ; else if (s4 == n*n)cout << "4" ; else cout << "5" ; return 0 ; }
416. 图像模糊处理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;double a[105 ][105 ],b[105 ][105 ];int n,m;int main () { cin >> n >> m; for (int i=0 ; i<n; i++) for (int j=0 ; j<m; j++) cin >> a[i][j]; for (int i=0 ; i<n; i++) for (int j=0 ; j<m; j++) b[i][j] = round ((i == 0 || i == n-1 || j == 0 || j == m-1 )? a[i][j] : (a[i][j] + a[i-1 ][j] + a[i+1 ][j] + a[i][j-1 ] + a[i][j+1 ])/5.0 ); for (int i=0 ; i<n; i++){ for (int j=0 ; j<m; j++) cout << b[i][j] << " " ; cout << endl; } return 0 ; }
417. 图像相似度 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;double m,n,a[101 ][101 ],b[101 ][101 ],p,f;int main () { cin >> m >> n; p = m*n; for (int i = 1 ;i<=m;i++) for (int j = 1 ;j<=n;j++) cin >> a[i][j]; for (int i = 1 ;i<=m;i++) for (int j = 1 ;j<=n;j++) cin >> b[i][j]; for (int i = 1 ;i<=m;i++) for (int j = 1 ;j<=n;j++) if (a[i][j] == b[i][j]) f++; printf ("%.2f" ,f/p*100.0 ); return 0 ; }
418. 扫雷游戏地雷数计算 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <bits/stdc++.h> using namespace std;bool a[500 ][500 ];int main () { int n,m; char c; cin >> n >> m; for (int i=1 ; i<=n; i++) { for (int j=1 ; j<=m; j++) { cin >> c; if (c == '*' ){ a[i][j] = 1 ; } else { a[i][j] = 0 ; } } } for (int i=1 ; i<=n; i++){ for (int j=1 ; j<=m; j++){ if (a[i][j]){ cout << "*" ; } else { cout << (a[i-1 ][j-1 ] + a[i-1 ][j] + a[i-1 ][j+1 ]) + (a[i][j-1 ] + a[i][j+1 ]) + (a[i+1 ][j-1 ] + a[i+1 ][j] + a[i+1 ][j+1 ]); } } cout<<endl; } return 0 ; }
419. 细菌的繁殖与扩散 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;int a[15 ][15 ][15 ];int main () { int n,m; cin >> n >> m; a[0 ][5 ][5 ] = n; for (int i=1 ; i<=m; i++) for (int j=1 ; j<=9 ; j++) for (int k=1 ; k<=9 ; k++) a[i][j][k] = (a[i-1 ][j-1 ][k-1 ] + a[i-1 ][j-1 ][k] + a[i-1 ][j-1 ][k+1 ]) + (a[i-1 ][j][k-1 ] + a[i-1 ][j][k]*2 + a[i-1 ][j][k+1 ]) + (a[i-1 ][j+1 ][k-1 ] + a[i-1 ][j+1 ][k] + a[i-1 ][j+1 ][k+1 ]); for (int i=1 ; i<=9 ; i++){ for (int j=1 ; j<=9 ; j++){ cout << a[m][i][j] << " " ; } cout << endl; } return 0 ; }
420. 矩阵剪刀石头布 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include <bits/stdc++.h> using namespace std;int r,c,n;char mp[2 ][105 ][105 ];int main () { cin>>r>>c>>n; for (int i=1 ; i<=r; i++) for (int j=1 ; j<=c; j++) cin>>mp[0 ][i][j]; for (int t=0 ; t<n; t++) for (int i=1 ; i<=r; i++) for (int j=1 ; j<=c; j++) { mp[~t&1 ][i][j]=mp[t&1 ][i][j]; #define check(x,y) \ if (mp[t&1][i][j]==x && (mp[t&1][i][j-1]==y || mp[t&1][i][j+1]==y || mp[t&1][i-1][j]==y || mp[t&1][i+1][j]==y))\ mp[~t&1][i][j]=y; check ('R' ,'P' ) check ('S' ,'R' ) check ('P' ,'S' ) } for (int i=1 ; i<=r; i++) { for (int j=1 ; j<=c; j++) cout<<mp[n&1 ][i][j]; cout<<'\n' ; } return 0 ; }
421. 最好的草 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;int r,c,ans;char mp[105 ][105 ];int main () { cin>>r>>c; for (int i=1 ; i<=r; i++) for (int j=1 ; j<=c; j++) cin>>mp[i][j]; for (int i=1 ; i<=r; i++) for (int j=1 ; j<=c; j++) if (mp[i][j]=='#' && mp[i][j-1 ]!='#' && mp[i-1 ][j]!='#' ) ans++; cout<<ans; return 0 ; }
422. 螺旋加密 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include <bits/stdc++.h> using namespace std;int r,c,t[10005 ],mp[25 ][25 ];string s; int main () { cin>>r>>c; cin.ignore (1 ), getline (cin,s); for (int i=0 ; i<s.size (); i++) { int x; if (s[i]==' ' ) x=0 ; else x=s[i]-'A' +1 ; for (int j=0 ; j<5 ; j++) t[i*5 +j]=(x>>(4 -j))&1 ; } for (int i=0 ; i<r; i++) for (int j=0 ; j<c; j++) mp[i][j]=-1 ; for (int i=0 ,x=0 ,y=0 ,d=1 ; i<r*c; i++) { mp[x][y]=t[i]; if (d==1 ) { y++; if (mp[x][y+1 ]!=-1 ) d=2 ; } else if (d==2 ) { x++; if (mp[x+1 ][y]!=-1 ) d=3 ; } else if (d==3 ) { y--; if (mp[x][y-1 ]!=-1 ) d=4 ; } else if (d==4 ) { x--; if (mp[x-1 ][y]!=-1 ) d=1 ; } } for (int i=0 ; i<r; i++) for (int j=0 ; j<c; j++) cout<<mp[i][j]; return 0 ; }
423. 叠方块游戏 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <bits/stdc++.h> using namespace std;string s; int a[11 ][11 ];int main () { cin>>s; for (int i=0 ; i<s.size (); i++) { int x=s[i]^48 ; for (int j=10 ; j>=1 ; j--) { if (j==1 || a[j][x] || a[j-1 ][x] || a[j][x-1 ]) { a[j+1 ][x]=a[j][x]=a[j+1 ][x-1 ]=1 ; break ; } } } for (int i=10 ; i>=1 ; i--) { for (int j=9 ; j>=0 ; j--) putchar (a[i][j] ? '#' : ' ' ); putchar ('\n' ); } return 0 ; }
424. 蛇形矩阵 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <bits/stdc++.h> using namespace std;int main () { int n,num=1 ; bool f = true ; cin >> n; for (int i=1 ; i<=n; i++) { if (f) for (int j=num; j<num+i; j++) cout << j << " " ; else for (int j=num+i-1 ; j>=num; j--) cout << j << " " ; f = !f; cout << endl; num += i; } return 0 ; }
425. 蛇形填充数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <bits/stdc++.h> using namespace std;int a[20 ][20 ];void fill (int x,int y,int len,int num,bool f) { if (f) for (int i=len-1 ; i>=0 ; i--) a[x-i][y+i] = num+(len-1 -i); else for (int i=0 ; i<len; i++) a[x-i][y+i] = num+i; } void out (int n) { for (int i=0 ; i<n; i++){ for (int j=0 ; j<n; j++) cout << a[i][j] << " " ; cout << endl; } } int main () { int n,x=0 ,y=0 ,num=1 ; cin >> n; bool f = false ; for (int i=0 ; i<n; i++){ fill (i,0 ,i+1 ,num,f); num = num+i+1 ; f = !f; } for (int i=1 ; i<n; i++){ fill (n-1 ,i,n-i,num,f); num = num+(n-i); f = !f; } out (n); return 0 ; }
426. 二维数组回形遍历 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <bits/stdc++.h> using namespace std;int r,c,mp[105 ][105 ];string s; int main () { cin>>r>>c; memset (mp,-1 ,sizeof mp); for (int i=0 ; i<r; i++) for (int j=0 ; j<c; j++) cin>>mp[i][j]; for (int i=0 ,x=0 ,y=0 ,d=1 ; i<r*c; i++) { cout<<mp[x][y]<<'\n' ; mp[x][y]=-1 ; if (d==1 ) { y++; if (mp[x][y+1 ]==-1 ) d=2 ; } else if (d==2 ) { x++; if (mp[x+1 ][y]==-1 ) d=3 ; } else if (d==3 ) { y--; if (mp[x][y-1 ]==-1 ) d=4 ; } else if (d==4 ) { x--; if (mp[x-1 ][y]==-1 ) d=1 ; } } return 0 ; }
427. 神奇的幻方 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;int n,a[45 ][45 ];int main () { cin>>n; int i=1 ,j=n; n=2 *n-1 ; for (int k=1 ; k<=n*n; k++) { a[i][j]=k; if (i==1 && j==n || a[i-1 ][j+1 ]) i++; else if (i==1 ) i=n, j++; else if (j==n) i--, j=1 ; else i--, j++; } for (int i=1 ; i<=n; i++) { for (int j=1 ; j<=n; j++) cout<<a[i][j]<<' ' ; cout<<'\n' ; } return 0 ; }
428. 同行列对角线的格子 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <bits/stdc++.h> using namespace std;int N,x,y;int main () { cin >> N >> x >> y; for (int i=1 ; i<=N; i++) printf ("(%d,%d) " ,x,i); cout << endl; for (int i=1 ; i<=N; i++) printf ("(%d,%d) " ,i,y); cout << endl; for (int i=1 ; i<=N; i++) for (int j=1 ; j<=N; j++) if (i-j == x-y) printf ("(%d,%d) " ,i,j); cout << endl; for (int i=1 ; i<=N; i++) for (int j=1 ; j<=N; j++) if (i+j == x+y) printf ("(%d,%d) " ,j,i); return 0 ; }
429. 矩阵交换行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <bits/stdc++.h> using namespace std;int a[9 ][9 ],m,n;int main () { for (int i=1 ; i<=5 ; i++) for (int j=1 ; j<=5 ; j++) cin >> a[i][j]; cin >> m >> n; for (int i=1 ; i<=5 ; i++){ for (int j=1 ; j<=5 ; j++){ if (i == n) cout << a[m][j] << " " ; else if (i == m) cout << a[n][j] << " " ; else cout << a[i][j] << " " ; } cout << endl; } return 0 ; }
430. 计算矩阵边缘元素之和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;int n,m,a,sum;int main () { cin >> n >> m; for (int i=0 ; i<n; i++){ for (int j=0 ; j<m; j++){ cin >> a; if (i == 0 || i == n-1 || j == 0 || j == m-1 ) sum += a; } } cout << sum; return 0 ; }
431.错误探测 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;int n, x, a[105 ], b[105 ], c1, c2;int main () { scanf ("%d" , &n); for (int i=0 ; i<n; i++) for (int j=0 ; j<n; j++) scanf ("%d" , &x), a[i]+=x, b[j]+=x; for (int i=0 ; i<n; i++) c1+=(a[i]&1 ); for (int i=0 ; i<n; i++) c2+=(b[i]&1 ); if (!c1 && !c2) puts ("OK" ); else if (c1==1 && c2==1 ) printf ("%d %d\n" , find_if (a, a+n, [](int n){return n&1 ;})-a+1 , find_if (b, b+n, [](int n){return n&1 ;})-b+1 ); else puts ("Corrupt" ); return 0 ; }
432.计算鞍点 提示:先找出一行的最大值,再检测列。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <bits/stdc++.h> using namespace std;int a[9 ][9 ],cnt;int main () { for (int i=0 ; i<5 ; i++) for (int j=0 ; j<5 ; j++) cin>>a[i][j]; for (int i=0 ; i<5 ; i++){ int m=-1e9 ,maxj,mini; for (int j=0 ; j<5 ; j++) if (a[i][j] > m) m=a[i][j], maxj=j; m = 1e9 ; for (int j=0 ; j<5 ; j++) if (a[j][maxj] < m) m=a[j][maxj], mini=j; if (mini == i){ cout << mini+1 << " " << maxj+1 << " " << a[mini][maxj]; cnt++; } } if (cnt == 0 ) cout << "not found" ; return 0 ; }
433.二维数组右上左下遍历 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;int a[106 ][106 ];int main () { int n, m; cin>>n>>m; for (int i=1 ; i<=n; i++) { for (int j=1 ; j<=m; j++) { cin>>a[i][j]; } } for (int k=2 ; k<=n+m; k++) { for (int i=1 ; i<=n; i++) { int j=k-i; if (j>=1 &&j<=m) { cout<<a[i][j]<<endl; } } } return 0 ; }
434.肿瘤面积 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 long long m,n,a[1001 ][1001 ],tot,f,w[]={0 ,0 ,1 ,0 ,-1 },u[]={0 ,1 ,0 ,-1 ,0 },ans=1e9 ;void dfs (long long x,long long y) { if (x==0 ||x==n+1 ||y==0 ||y==m+1 ){ f=0 ; return ; } a[x][y]=0 ,tot++; for (int i=1 ;i<=4 ;i++){ int x1=x+w[i],y1=y+u[i]; if (a[x1][y1]==255 )dfs (x1,y1); } return ; } int main () { cin>>n>>m; for (int i=1 ;i<=n;i++) for (int j=1 ;j<=m;j++)cin>>a[i][j]; for (int i=1 ;i<=n;i++){ for (int j=1 ;j<=m;j++){ if (a[i][j]==255 ){ tot=0 ,f=1 ,dfs (i,j); if (f)ans=min (ans,tot); } } } return cout<<ans,0 ; }
435.肿瘤检测 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;int n,a[105 ][105 ],vis[105 ][105 ],sx,sy,ans1,ans2;void dfs (int x, int y) { if (x<1 || x>n || y<1 || y>n || a[x][y]==0 || vis[x][y]==1 ) return ; vis[x][y]=1 , ans1++; if (a[x-1 ][y]==0 || a[x+1 ][y]==0 || a[x][y-1 ]==0 || a[x][y+1 ]==0 ) ans2++; dfs (x-1 ,y), dfs (x+1 ,y); dfs (x,y-1 ), dfs (x,y+1 ); } int main () { cin>>n; for (int i=1 ; i<=n; i++) for (int j=1 ; j<=n; j++) { cin>>a[i][j]; if (a[i][j]<=50 ) a[i][j]=1 , sx=i, sy=j; else a[i][j]=0 ; } dfs (sx,sy); cout<<ans1<<" " <<ans2; return 0 ; }
436.最匹配的矩阵 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include <bits/stdc++.h> using namespace std;int m, n, r, s, ans=1e9 , ansi, ansj, a[105 ][105 ], b[105 ][105 ];int main () { cin>>m>>n; for (int i=1 ; i<=m; i++) for (int j=1 ; j<=n; j++) cin>>a[i][j]; cin>>r>>s; for (int i=1 ; i<=r; i++) for (int j=1 ; j<=s; j++) cin>>b[i][j]; for (int i=1 ; i<=m-r+1 ; i++) for (int j=1 ; j<=n-s+1 ; j++) { int x=0 ; for (int p=1 ; p<=r; p++) for (int q=1 ; q<=s; q++) x+=abs (b[p][q]-a[i+p-1 ][j+q-1 ]); if (x<ans) ans=x, ansi=i, ansj=j; } for (int i=1 ; i<=r; i++) { for (int j=1 ; j<=s; j++) cout<<a[ansi+i-1 ][ansj+j-1 ]<<" " ; cout<<'\n' ; } return 0 ; }
437.图像旋转翻转变换 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include <bits/stdc++.h> using namespace std;int a[120 ][120 ], m, n, b[120 ][120 ];int main () { cin>>m>>n; for (int i=0 ; i<m; i++) for (int j=0 ; j<n; j++) cin>>a[i][j]; string s; cin>>s; for (int i=0 ; i<s.size (); i++) { if (s[i] == 'A' ) { for (int i=0 ; i<m; i++) for (int j=0 ; j<n; j++) b[j][i] = a[m-i-1 ][j]; swap (n, m); } else if (s[i] == 'B' ) { for (int i=0 ; i<m; i++) for (int j=0 ; j<n; j++) b[j][i] = a[i][n-j-1 ]; swap (n, m); } else if (s[i] == 'C' ) { for (int i=0 ; i<m; i++) for (int j=0 ; j<n; j++) b[i][j] = a[i][n-j-1 ]; } else if (s[i] == 'D' ) { for (int i=0 ; i<m; i++) for (int j=0 ; j<n; j++) b[i][j] = a[m-i-1 ][j]; } memcpy (a, b, sizeof a); } for (int i=0 ; i<m; i++) { for (int j=0 ; j<n; j++) cout<<a[i][j]<<" " ; cout << endl; } return 0 ; }
438.验证数独 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <bits/stdc++.h> using namespace std;int n, x, h[10 ][10 ], l[10 ][10 ], g[10 ][10 ];bool check () { for (int i=1 ; i<=9 ; i++) for (int j=1 ; j<=9 ; j++) if (h[i][j]!=1 || l[i][j]!=1 || g[i][j]!=1 ) return 1 ; return 0 ; } int main () { cin>>n; while (n--) { memset (h, 0 , sizeof h); memset (l, 0 , sizeof l); memset (g, 0 , sizeof g); for (int i=1 ; i<=9 ; i++) for (int j=1 ; j<=9 ; j++) cin>>x, h[i][x]++, l[j][x]++, g[(i-1 )/3 *3 +(j-1 )/3 +1 ][x]++; puts (check () ? "Wrong" : "Right" ); } return 0 ; }
439.求正方形面积 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;int r, c, a[55 ][55 ], ans;int main () { scanf ("%d%d" , &r, &c); for (int i=1 ; i<=r; i++) { for (int j=1 ; j<=c; j++) { scanf ("%d" , &a[i][j]); a[i][j]=a[i][j-1 ]+a[i-1 ][j]-a[i-1 ][j-1 ]+a[i][j]; } } for (int i=1 ; i<=r; i++) for (int j=1 ; j<=c; j++) for (int x=1 ; x<=min (r-i+1 , c-j+1 ); x++) if (a[i+x-1 ][j+x-1 ]-a[i-1 ][j+x-1 ]-a[i+x-1 ][j-1 ]+a[i-1 ][j-1 ] == x*x) ans=max (ans, x*x); printf ("%d" , ans); return 0 ; }
440.最长连续不重复子序列 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include <bits/stdc++.h> using namespace std;int n;int a[100010 ], s[100010 ];int main () { int ans = 0 ; cin >> n; for (int i=0 ; i<n; i++) cin >> a[i]; for (int i=0 , j=0 ; i<n; i++) { s[a[i]]++; while (s[a[i]] > 1 ) { s[a[j]]--; j++; } ans = max (ans, i-j+1 ); } cout << ans << endl; return 0 ; }
441.包含两个不同整数的最长连续子序列 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std;int n, a[100005 ], b[100005 ], cnt, ans, ansl, ansr;int main () { scanf ("%d" , &n); for (int i=1 ; i<=n; i++) scanf ("%d" , &a[i]); int l=1 ; for (int r=1 ; r<=n; r++) { if (b[a[r]]++ == 0 ) cnt++; while (cnt>2 ) { if (--b[a[l]] == 0 ) cnt--; l++; } if (r-l+1 > ans) ans=r-l+1 , ansl=l, ansr=r; } printf ("%d\n" ,ans); for (int i=ansl; i<=ansr; i++) printf ("%d " , a[i]); return 0 ; }
442.数组元素的目标和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <bits/stdc++.h> using namespace std;int a[100005 ],b[100005 ];int main () { int n,m,x; cin >> n >> m >> x; for (int i=0 ; i<n; i++)cin >> a[i]; for (int i=0 ; i<m; i++)cin >> b[i]; for (int i = 0 , j = m - 1 ; i < n; i ++){ while (j >= 0 && a[i] + b[j] > x) j --; if (a[i] + b[j] == x) printf ("%d %d\n" , i, j); } return 0 ; }
443.判断子序列 提示:子序列不需要连续,扫描一遍 b 贪心匹配即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <bits/stdc++.h> using namespace std;int n, m;int a[100005 ], b[100005 ];int main () { cin>>n>>m; for (int i=0 ; i<n; i++) cin>>a[i]; for (int i=0 ; i<m; i++) cin>>b[i]; int i=0 ; for (int j=0 ; j<m; j++) { if (i<n && a[i]==b[j]) i++; } if (i==n) cout << "Yes" ; else cout << "No" ; return 0 ; }
444.【模板】一维前缀和 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;int n, m, l, r, a[100005 ];int main () { scanf ("%d%d" , &n, &m); for (int i=1 ; i<=n; i++) { scanf ("%d" , &a[i]); a[i]+=a[i-1 ]; } while (m--) { scanf ("%d%d" , &l, &r); printf ("%d\n" , a[r]-a[l-1 ]); } return 0 ; }
445.区间内1的个数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std;int n, m, l, r, a[1000005 ];int main () { scanf ("%d" , &n); for (int i=1 ; i<=n; i++) { scanf ("%d" , &a[i]); a[i]+=a[i-1 ]; } scanf ("%d" , &m); while (m--) { scanf ("%d%d" , &l, &r); printf ("%d\n" , a[r]-a[l-1 ]); } return 0 ; }
446.好胜心强的同学们 1 2 3 4 5 6 7 8 9 10 11 12 #include <bits/stdc++.h> using namespace std;int n,a[200005 ];long long b[1000005 ];int main () { cin>>n; for (int i=1 ; i<=n; i++) cin>>a[i], b[a[i]]+=a[i]; for (int i=1 ; i<=1000000 ; i++) b[i]+=b[i-1 ]; for (int i=1 ; i<=n; i++) cout<<b[1000000 ]-b[a[i]]<<' ' ; return 0 ; }
447.【模板】一维差分 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <bits/stdc++.h> using namespace std;int n,m,l,r,c,a[500005 ],b[500005 ];int main () { cin>>n>>m; for (int i=1 ; i<=n; i++) cin>>b[i]; for (int i=1 ; i<=m; i++) { cin>>l>>r>>c; a[l]+=c, a[r+1 ]-=c; } for (int i=1 ; i<=n; i++) a[i]+=a[i-1 ], cout<<a[i]+b[i]<<' ' ; return 0 ; }
448.nozomi和字符串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <bits/stdc++.h> using namespace std;int n, k, ans, c[2 ];string s; int main () { cin>>n>>k>>s; int l=0 ; for (int r=0 ; r<n; r++) { c[s[r]^48 ]++; while (c[0 ]>k && c[1 ]>k) c[s[l]^48 ]--, l++; ans=max (ans, r-l+1 ); } cout<<ans; return 0 ; }
449.区间修改 代码同 447 题。
450.The Bucket List 提示:差分+前缀和计算区间覆盖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <bits/stdc++.h> using namespace std;int n,m,l,r,c,a[1005 ],ans;int main () { cin>>n; for (int i=1 ; i<=n; i++) { cin>>l>>r>>c; a[l]+=c, a[r+1 ]-=c; } for (int i=1 ; i<=1000 ; i++) a[i]+=a[i-1 ], ans=max (ans, a[i]); cout<<ans; return 0 ; }
Page 6 504.快递中转点 1 2 3 4 5 6 7 8 9 10 11 int n,a[100001 ],ans,sum;int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; sort (a+1 ,a+1 +n); if (n&1 )ans=n/2 +1 ; else ans=n>>1 ; for (int i=1 ;i<=n;i++)sum+=abs (a[i]-a[ans]); return cout<<sum,0 ; }
505.给oliver的情书 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 long long n,m,a[10001 ][1001 ],b[10001 ],flag=0 ,minn=1e10 ,t,lenb;int main () { cin>>n>>m; for (int i=1 ;i<=n;i++) for (int j=1 ;j<=m;j++)cin>>a[i][j]; for (int i=1 ;i<=m;i++){ lenb=1 ,flag=0 ; for (int j=1 ;j<=n;j++)b[j]=a[j][i]; sort (b+1 ,b+n+1 ); for (int j=1 ;j<=n;j++){ t=b[2 ]-b[1 ]; if (j>2 ){ if (b[j]-t!=b[j-1 ]) flag=1 ; break ; } } if (!flag)minn=min (minn,t); memset (b,0 ,lenb+1 ); } if (minn==10e9 )cout<<"NO" ; else cout<<minn; return 0 ; }
506.求平均数、中位数、众数 1 2 3 4 5 6 7 8 9 10 11 int a[41 ],cnt,sum,maxn,c,b[41 ];int main () { for (int i=1 ;i<=40 ;i++)cin>>a[i],sum+=a[i],b[a[i]]++; cout<<sum/40 <<"\n" ; sort (a+1 ,a+1 +40 ); cout<<a[21 ]<<"\n" ; for (int i=1 ;i<=10 ;i++) if (b[i]>maxn) maxn=b[i],c=i; return cout<<c,0 ; }
546.计算概率 1 2 3 4 5 6 7 8 9 10 11 long long n,k,ans,a[100005 ];int main () { cin>>n>>k; for (int i=1 ;i<=n;++i)cin>>a[i]; stable_sort (a+1 ,a+1 +n); for (int i=1 ;i<=n;++i){ int t=upper_bound (a+1 ,a+1 +n,k-a[i])-a-1 ; if (t>=i)t--; ans+=t; } cout<<fixed<<setprecision (2 )<<1. *ans/(n*(n-1 )); return 0 ; }
553.最少操作次数 1 2 3 4 5 6 7 8 9 10 long long a,b,k,cnt;int main () { cin>>a>>b>>k; if (k<=1 ) return cout<<b-a,0 ; else { while (a<=b/k)cnt+=b%k+1 ,b/=k; cout<<cnt+b-a; } return 0 ; }
554.飞行员的工资 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 long long ans,a[100001 ],b[10001 ],n,x,y;int main () { cin>>n; for (int i=1 ;i<=n;i++) cin>>x>>y; ans+=x,a[i]=x-y; for (int i=1 ;i<n;i+=2 ){ int k=0 ; for (int j=1 ;j<=i;j++) if (b[j]==0 &&a[j]>a[k])k=j; b[k]=1 ; ans-=a[k]; } cout<<ans; return 0 ; }
555.智力大冲浪 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 struct data { int x,y; }a[505 ]; bool cmp (data a,data b) { return a.y>b.y; } int m,n,s[505 ],num=0 ,sum=0 ,maxn=0 ; int main () { while (cin>>m){ num=maxn=sum=0 ; cin>>n; for (int i=1 ;i<=n;i++){ cin>>a[i].x; if (a[i].x>maxn)maxn=a[i].x; } for (int i=1 ;i<=n;i++) cin>>a[i].y,num+=a[i].y; sort (a+1 ,a+1 +n,cmp); for (int i=1 ;i<=n;i++)for (int j=a[i].x;j;j--) if (s[j]==0 ){ s[j]=a[i].y; break ;} for (int i=1 ;i<=maxn;i++)sum+=s[i]; cout<<m-(num-sum)<<endl; } return 0 ; }
556.两人过河 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int n,ans,a[1001 ];int main () { cin>>n; for (int i=1 ;i<=n;i++)cin>>a[i]; sort (a+1 ,a+1 +n); while (n>3 ){ ans+=min (a[2 ]+a[2 ]+a[1 ]+a[n],a[n-1 ]+a[1 ]+a[n]+a[1 ]); n-=2 ; } if (n==2 )ans+=a[2 ]; if (n==3 )ans+=a[1 ]+a[2 ]+a[3 ]; cout<<ans; return 0 ; }
571.编辑 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 string s; long long T,l;char c,m;int main () { cin>>s>>T; l=s.size (); while (T--){ cin>>c; if (c=='B' ){ if (l>0 )s.erase (l-1 ,1 ),l--; } if (c=='L' ){ if (l>0 )l--; } if (c=='D' ){ if (l<s.size ())l++; } if (c=='P' ){ cin>>m; s.insert (s.begin ()+l,m); l++; } } cout<<s; return 0 ; }
Page 9 823.没有上司的晚会 树形动态规划模板题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 int f[6005 ][2 ],u,v,n;bool h[6005 ],vis[6005 ];vector<int >g[6005 ]; void solve (int x) { vis[x]=1 ; for (auto y:g[x]){ if (vis[y])continue ; solve (y); f[x][1 ]+=f[y][0 ]; f[x][0 ]+=max (f[y][0 ],f[y][1 ]); } return ; } int main () { cin>>n; for (int i=1 ;i<=n;++i)cin>>f[i][1 ]; for (int i=1 ;i<n;++i)cin>>u>>v,h[u]=1 ,g[v].push_back (u); for (int i=1 ;i<=n;++i) if (!h[i])solve (i),cout<<max (f[i][1 ],f[i][0 ]),exit (0 ); return 0 ; }
Page 10 966.树状数组模板1【单点修改,区间询问】 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 const int M=1e6 +5 ;int n,m;struct BIT { int a[M]; int lowbit (int x) { return x&-x; } int query (int x) { int res=0 ; while (x)res+=a[x],x-=lowbit (x); return res; } void add (int x,int k) { while (x<=n)a[x]+=k,x+=lowbit (x); return ; } }t; signed main () { scanf ("%d%d" ,&n,&m); for (int i=1 ,x;i<=n;++i) scanf ("%d" ,&x),t.add (i,x); for (int i=1 ,op,x,y;i<=m;++i){ scanf ("%d%d%d" ,&op,&x,&y); if (op==1 )t.add (x,y); else printf ("%d\n" ,t.query (y)-t.query (x-1 )); } return 0 ; }
996.欧拉回路 使用 bfs 来判断图是否连通,通过小学奥数可知,没有奇点的图可以形成欧拉回路,有 2 个奇点的图可以 1 笔画完所有路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #define int long long const int M=1e5 +5 ;int n,m;vector<int >g[M]; inline bool bfs () { bitset<M>vis; queue<int >q; q.push (1 ),vis[1 ]=1 ; while (q.size ()){ int x=q.front (); q.pop (); for (auto y:g[x]) if (!vis[y])vis[y]=1 ,q.push (y); } return vis.count ()==n; } signed main () { while (scanf ("%lld" ,&n),n){ scanf ("%lld" ,&m); for (int i=1 ,u,v;i<=m;++i){ scanf ("%lld%lld" ,&u,&v), g[u].push_back (v), g[v].push_back (u); } bool res=1 ; if (!bfs ())res=0 ; for (int i=1 ;i<=n&&res;++i) if (g[i].size ()&1 )res=0 ; printf ("%d\n" ,res); for (int i=1 ;i<=n;++i) g[i].clear (); } return 0 ; }
Page 11 1002.逃生 为了保证排队的顺序,需要使用优先队列来进行拓扑排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #define int long long priority_queue<int >q; vector<int >g[30005 ],ans; int deg[30005 ],n,m,x,y,T;signed main () { scanf ("%lld" ,&T); while (T--){ scanf ("%lld%lld" ,&n,&m); memset (deg,0 ,sizeof deg); while (m--) scanf ("%lld%lld" ,&x,&y), g[y].push_back (x),deg[x]++; for (int i=1 ;i<=n;++i) if (!deg[i])q.push (i); while (q.size ()){ int tmp=q.top (); q.pop (); ans.push_back (tmp); for (auto p:g[tmp]) if (--deg[p]==0 )q.push (p); } reverse (ans.begin (),ans.end ()); for (auto i:ans) printf ("%lld " ,i); puts ("" ),ans.clear (); for (int i=1 ;i<=n;++i)g[i].clear (); } return 0 ; }
Update
日期
更新内容
源码长度
2022 12 17
基础题库前 100 题 发布至洛谷博客
Unknown
2023 01 21
更新至基础题库前 130 题
1236 Lines
2023 08 21
更新至基础题库前 180 题
1786 Lines
2023 08 24
更新至基础题库前 200 题
2051 Lines
2024 08 21
增加约 40 题题解 编写 Blog 页内跳转
2417 Lines
2024 08 22
增加约 49 题题解 修改部分远古码风
3221 Lines
2024 08 24
增加约 9 篇题解
3324 Lines
2024 08 25
增加 T261-300 共 40 篇题解
3927 Lines
2025 03 08
修复 3 篇无法通过的题解
3932 Lines
2025 03 09
修复马蜂
Unknown
2025 03 11
修复题目编号,以及 1 篇 CE 的题解
3948 Lines
2025 03 16 ~ 03 18
更新约 20 篇题解
4397 Lines
2025 04 12
更新至基础题库前 337 题
4736 Lines
2025 04 19
更新至基础题库前 370 题
5282 Lines
2025 04 26
更新至基础题库前 400 题
6082 Lines
2025 06 14
更新至基础题库前 415 题
6525 Lines
2025 07 05
更新至基础题库前 450 题
7437 Lines