简单模拟 1 2 3 4 5 6 7 8 9 10 11 12 13 14 int main () { # 输入要用double double n; scanf ("%lf" ,&n); # 输出要有换行符,注意输出格式,可以使用%g if (n>5000 ) printf ("discount=0.8,pay=%.1f\n" ,n*0.8 ) ; else if (n>3000 ) printf ("discount=0.85,pay=%.1f\n" ,n*0.85 ); else if (n>2000 ) printf ("discount=0.9,pay=%.1f\n" ,n*0.9 ); else if (n>1000 ) printf ("discount=0.95,pay=%.1f\n" ,n*0.95 ); else printf ("discount=1,pay=%.1f\n" ,n); return 0 ; }
1722 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 <iostream> #include <string> #include <cctype> using namespace std ; int main () { int weights[17 ] = {7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 , 6 , 3 , 7 , 9 , 10 , 5 , 8 , 4 , 2 }; string num_map_str[11 ] = {"1" , "0" , "X" , "9" , "8" , "7" , "6" , "5" , "4" , "3" , "2" }; string id_card; while (cin >> id_card){ int sum=0 ; bool is_valid = true ; if (id_card.length()!=18 ){ cout <<"ID Wrong" <<endl ; is_valid=false ; continue ; } for (int i=0 ;i<17 ;i++){ if (!isdigit (id_card[i])){ is_valid=false ; break ; } sum += (id_card[i]-'0' )*weights[i]; } string expect_num=num_map_str[sum%11 ]; if (expect_num[0 ]!=id_card[17 ]){ is_valid=false ; } if (is_valid) printf ("ID Corrent\n" ); else printf ("ID Wrong\n" ); } }
进制转换 10转2 十进制为输入,则输入的是数字,转成字符数组,输出也是字符数组
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 <iostream> #include <string.h> #include <cctype> using namespace std;int main () { int n,k=0 ; char a[105 ]={0 }; while (scanf ("%d" ,&n)!=EOF){ if (n==0 ) continue ; int k=0 ; char a[105 ]={0 }; for (int i=0 ;n>0 ;i++){ a[i]=n%2 +'0' ; n/=2 ; k++; } for (int i=k-1 ;i>=0 ;i--){ printf ("%c" ,a[i]); } printf ("\n" ); } }
10进制转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 #include <iostream> #include <string> #include <cctype> using namespace std;int main () { int n,x; char ss[105 ]; scanf ("%d %d" ,&n,&x); int i=0 ; while (n>0 ){ int num=(n%x); if (num<10 ) ss[i++]=num+'0' ; else ss[i++]=num-10 +'A' ; n/=x; } for (int j=i-1 ;j>=0 ;j--){ printf ("%c" ,ss[j]); } printf ("\n" ); }
2转10 二进制为输入,输入为字符串。如果是零,数值翻倍。如果是一,翻倍后再加一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <iostream> #include <string.h> #include <cctype> using namespace std ; int main () { char s[105 ]; cin >>s; int ans=0 ; for (int i=0 ;i<strlen (s);i++){ if (s[i]=='0' ) ans*=2 ; else ans=ans*2 +1 ; } printf ("%d" ,ans); }
x转10 先自乘进制(相当于把x进制的每一位乘上进制),再加上这一位的字符值减去‘0’或者‘A’或者‘a’
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 <iostream> #include <string.h> #include <cctype> using namespace std ; int main () { int x; char s[105 ]; cin >>s>>x; int ans=0 ; for (int i=0 ;i<strlen (s);i++){ ans*=x; if (s[i]>='0' &&s[i]<='9' ) ans+=s[i]-'0' ; else ans+=s[i]-'A' +10 ; } printf ("%d" ,ans); }
x转y x先转10,10再转y
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 #include <iostream> #include <string.h> #include <cctype> using namespace std;int main () { long long m,n; char x[105 ]={0 }; cin>>m>>n>>x; long long ans=0 ; for (int i=0 ;i<strlen (x);i++){ ans*=m; if (x[i]>='0' &&x[i]<='9' ) ans+=x[i]-'0' ; else ans+=x[i]-'A' +10 ; } char x2[105 ]={0 }; long long k=0 ; if (ans==0 ) x2[k++]='0' ; else { while (ans>0 ){ int ans2=ans%n; if (ans2>=0 &&ans2<=9 ) x2[k++]=ans2+'0' ; else x2[k++]=ans2-10 +'a' ; ans/=n; } } for (int j=k-1 ;j>=0 ;j--){ printf ("%c" ,x2[j]); } }
1176 大数字直接使用python
format()format(value, format_spec)的工作原理可以拆解为:
第一个参数value:要格式化的对象(这里必须是整数,因为'b'只适用于整数);
第二个参数format_spec:格式指令(不同的字符对应不同的进制 / 格式)。
int(x, base)的底层规则:
base参数可以指定2~36之间的任意整数(对应二进制到三十六进制);
函数会把x看作base进制的数值,然后转换为十进制整数;
如果省略base,默认是10(十进制)。
1 2 3 4 5 6 7 8 9 while True : try : a=int (input ()) b=format (a,'b' ) b1=b[::-1 ] b2=int (b1,2 ) print (b2) except : break
排版问题 输出菱形
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 <iostream> #include <string.h> #include <cctype> using namespace std ; int main () { int n; scanf ("%d" ,&n); for (int i=1 ;i<=n;i++){ for (int j=1 ;j<=n-i;j++){ printf (" " ); } for (int j=1 ;j<=1 +2 *(i-1 );j++){ printf ("*" ); } printf ("\n" ); } for (int i=n-1 ;i>=1 ;i--){ for (int j=1 ;j<=n-i;j++){ printf (" " ); } for (int j=1 ;j<=1 +2 *(i-1 );j++){ printf ("*" ); } printf ("\n" ); } }
杨辉三角 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 <iostream> #include <string.h> #include <cctype> using namespace std ; int main () { int n; while (scanf ("%d" ,&n)!=EOF){ if (n==0 ) break ; int arr[21 ][21 ]={0 }; arr[1 ][1 ]=1 ; for (int i=2 ;i<=n;i++){ for (int j=1 ;j<=i;j++){ arr[i][j]=arr[i-1 ][j]+arr[i-1 ][j-1 ]; } } for (int i=1 ;i<=n;i++){ for (int j=1 ;j<=i;j++){ printf ("%d " ,arr[i][j]); } printf ("\n" ); } } }
1377旋转方阵 思路是写一个旋转90°的函数
关键在于旋转时,交换数组需要使用一个新的,避免原数组被覆盖
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 # include <bits/stdc++.h> using namespace std ; int n,a;int arr1[10 ][10 ]={0 },arr2[10 ][10 ]={0 };int temp1[10 ][10 ] = {0 },temp2[10 ][10 ] = {0 },temp3[10 ][10 ] = {0 };bool isEqual (int arr1[10 ][10 ],int arr2[10 ][10 ]) { for (int i=1 ;i<=n;i++){ for (int j=1 ;j<=n;j++){ if (arr1[i][j]!=arr2[i][j]){ return false ; } } } return true ; } void rotate90 (int mat[10 ][10 ], int res[10 ][10 ]) { for (int i = 1 ; i <= n; i++) { for (int j = 1 ; j <= n; j++) { res[j][n - i + 1 ] = mat[i][j]; } } } int main () { while (scanf ("%d" ,&n)!=EOF){ int arr1[10 ][10 ]={0 },arr2[10 ][10 ]={0 }; int temp1[10 ][10 ] = {0 },temp2[10 ][10 ] = {0 },temp3[10 ][10 ] = {0 },temp4[10 ][10 ] = {0 }; for (int i=1 ;i<=n;i++){ for (int j=1 ;j<=n;j++){ scanf ("%d" ,&a); arr1[i][j]=a; } } for (int i=1 ;i<=n;i++){ for (int j=1 ;j<=n;j++){ scanf ("%d" ,&a); arr2[i][j]=a; } } if (isEqual(arr1, arr2)) { printf ("0\n" ); continue ; } rotate90(arr1, temp1); if (isEqual(temp1, arr2)) { printf ("%d\n" , 90 ); continue ; } rotate90(temp1, temp2); if (isEqual(temp2, arr2)) { printf ("%d\n" , 180 ); continue ; } rotate90(temp2, temp3); if (isEqual(temp3, arr2)) { printf ("%d\n" , 270 ); continue ; } printf ("%d\n" , -1 ); } return 0 ; }
1216 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 # include <bits/stdc++.h> using namespace std;int main () { int n; cin>>n; int a[30 ][30 ]; for (int i=0 ;i<30 ;i++){ for (int j=0 ;j<30 ;j++){ a[i][j]=-1 ; } } int flag=1 ; int num=1 ,i=0 ,j=0 ; while (num<=n*n) { if (a[i][j]==-1 ){ a[i][j]=num; num++; } if (flag==1 ){ i++; if (i>=n||a[i][j]!=-1 ){ i--; flag=2 ; } } else if (flag==2 ){ j++; if (j>=n||a[i][j]!=-1 ){ j--; flag=3 ; } } else if (flag==3 ){ i--; if (i<0 ||a[i][j]!=-1 ){ i++; flag=4 ; } } else if (flag==4 ){ j--; if (j<0 ||a[i][j]!=-1 ){ j++; flag=1 ; } } } for (int i=0 ;i<n;i++) { for (int j=0 ;j<n;j++) { printf ("%-3d " ,a[i][j]); } printf ("\n" ); } }
2.4日期类问题 根据日期算星期几(不考虑闰年)
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 main () { int f[13 ]={0 ,31 ,28 ,31 ,30 ,31 ,30 ,31 ,31 ,30 ,31 ,30 ,31 }; char str[8 ][10 ]={"Thursday" ,"Friday" ,"Saturday" ,"Sunday" ,"Monday" ,"Tuesday" ,"Wednesday" }; int month,day,ans=0 ; cin >>month>>day; ans+=day; for (int i=4 ;i<month;i++){ ans+=f[i]; } ans-=12 ; printf ("%s" ,str[ans%7 ]); }
计算到1年1月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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include <stdio.h> int isLeapYear (int year) { if ((year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0 )) { return 1 ; } return 0 ; } int getDaysInMonth (int month, int year) { int days[] = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 }; if (month == 2 && isLeapYear(year)) { return 29 ; } return days[month - 1 ]; } int dateToDays (int year, int month, int day) { int days = 0 ; for (int i = 1 ; i < year; i++) { days += isLeapYear(i) ? 366 : 365 ; } for (int i = 1 ; i < month; i++) { days += getDaysInMonth(i, year); } days += day; return days; } int daysBetweenDates (int year1, int month1, int day1, int year2, int month2, int day2) { int days1 = dateToDays(year1, month1, day1); int days2 = dateToDays(year2, month2, day2); return days2 - days1; } int main () { int year1, month1, day1; int year2, month2, day2; printf ("请输入第一个日期 (yyyy mm dd):" ); scanf ("%d %d %d" , &year1, &month1, &day1); printf ("请输入第二个日期 (yyyy mm dd):" ); scanf ("%d %d %d" , &year2, &month2, &day2); int diff = daysBetweenDates(year1, month1, day1, year2, month2, day2); printf ("两个日期之间的天数差为:%d天\n" , diff); return 0 ; }
1410 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;bool isrun (int m) { if (m%100 !=0 &&m%4 ==0 ) return true ; if (m%400 ==0 ) return true ; return false ; } int main () { int year, days; while (scanf ("%d %d" ,&year,&days)!=EOF){ int f[]={31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 }; if (isrun (year)) f[1 ]=29 ; int month=0 ; while (days>f[month]){ days-=f[month]; month++; } printf ("%04d-%02d-%02d\n" ,year,month+1 ,days); } }
1446 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;bool isrun (int m) { if (m%100 !=0 &&m%4 ==0 ) return true ; if (m%400 ==0 ) return true ; return false ; } int main () { int n; scanf ("%d" ,&n); while (n>0 ){ int y,m,d,t; int f[]={0 ,31 ,28 ,31 ,30 ,31 ,30 ,31 ,31 ,30 ,31 ,30 ,31 }; scanf ("%d %d %d %d" ,&y,&m,&d,&t); if (isrun (y)) f[2 ]=29 ; int days = d+t; while (days>f[m]){ if (isrun (y)) f[2 ]=29 ; else f[2 ]=28 ; days-=f[m]; m++; if (m > 12 ) { y++; m = 1 ; } } printf ("%04d-%02d-%02d\n" ,y,m,days); n--; } }
1053 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;int main () { int n; scanf ("%d" ,&n); while (n>0 ){ int hour,minu; scanf ("%d:%d" ,&hour,&minu); int hhour,mminu; hhour=hour+13 ; mminu=minu+15 ; if (mminu>=60 ){ hhour+=mminu/60 ; mminu=mminu%60 ; } hhour=hhour%24 ; printf ("%d:%d\n" ,hhour,mminu); n--; } }
2.5字符串问题 1012 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 main () { string a,a1,a2; cin >> a; for (int i=0 ;i<a.size ();i++){ if (a[i] >= '0' && a[i] <= '9' ){ a2 += a[i]; } else { a1 += a[i]; } } cout << a1 << a2 << endl; }
1292 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 main () { string s; while (getline (cin,s)){ int a[27 ]={0 }; for (char c:s){ if (c>='A' &&c<='Z' ){ int index=c-'A' ; a[index]++; } } for (int i=0 ;i<26 ;i++){ char c='A' +i; printf ("%c:%d\n" ,c,a[i]); } } }
1240 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 () { string s; while (getline (cin, s)) { int i=0 ; for (char c:s){ if ((s[i]==' ' ||s[i]=='\t' ||s[i]=='\r' ||s[i]=='\n' ) && (i+1 < s.size ()) && (s[i+1 ]>='a' &&s[i+1 ]<='z' )) s[i+1 ]-=32 ; i++; } if (s[0 ]>='a' &&s[0 ]<='z' ){ s[0 ]-=32 ; } cout<<s<<endl; } }
1394 重点
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 main () { string s; while (getline (cin, s)){ bool first_kg=false ; int i=0 ; bool first_word=true ; for (char c:s){ if (c=='.' ){ if (first_kg){ if (!first_word) printf (" " ); printf ("%d" ,i); } break ; } if (c!=' ' ){ i++; first_kg=true ; } if (c==' ' ){ if (first_kg){ if (!first_word) printf (" " ) ; printf ("%d" ,i); i=0 ; first_kg=false ; first_word=false ; } } } printf ("\n" ); } }
1027 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;int main () { string s,t; getline (cin,s); int i=0 ; for (char c:s){ t+=tolower (c); i++; } i=0 ; for (char c:t){ if (i + 2 < t.size () && t[i]=='g' &&t[i+1 ]=='z' &&t[i+2 ]=='u' ){ t[i]='0' ; t[i+1 ]='0' ; t[i+2 ]='0' ; i+=3 ; } else i++; } i=0 ; for (char c:t){ if (c!='0' ){ printf ("%c" ,s[i]); } i++; } printf ("\n" ); }
2.6排序问题 1159 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;struct student { int id; int score; }; bool cmp (student x,student y) { if (x.score==y.score) return x.id<y.id; else return x.score<y.score; } int main () { int n; cin>>n; student all[100 ]; for (int i=0 ;i<n;i++){ cin>>all[i].id; cin>>all[i].score; } sort (all,all+n,cmp); for (int i=0 ;i<n;i++){ cout<<all[i].id<<' ' <<all[i].score<<endl; } }
1217 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 () { int n; string country[100 ]; cin>>n; cin.ignore (); for (int i=0 ;i<n;i++){ getline (cin,country[i]); } sort (country,country+n); for (int i=0 ;i<n;i++){ cout<<country[i]<<endl; } }
1248 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;bool cmp_asc (int x,int y) { return x < y; } bool cmp_desc (int x,int y) { return x > y; } int main () { int a[11 ]={0 },a_ji[11 ]={0 },a_ou[11 ]={0 }; int j=0 ,k=0 ; while (cin>>a[0 ]>>a[1 ]>>a[2 ]>>a[3 ]>>a[4 ]>>a[5 ]>>a[6 ]>>a[7 ]>>a[8 ]>>a[9 ]){ j=0 ,k=0 ; for (int i=0 ;i<10 ;i++){ if (a[i]%2 !=0 ){ a_ji[j]=a[i]; j++; } else { a_ou[k]=a[i]; k++; } } sort (a_ji,a_ji+j,cmp_desc); sort (a_ou,a_ou+k,cmp_asc); for (int z=0 ;z<j;z++){ cout<<a_ji[z]<<' ' ; } for (int z=0 ;z<k;z++){ cout<<a_ou[z]<<' ' ; } } }
1254 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <bits/stdc++.h> using namespace std;int main () { string s; while (getline (cin,s)){ sort (s.begin (),s.end ()); cout<<s<<endl; } }
1255 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;bool cmp_ (int x,int y) { return tolower (x)<tolower (y); } int main () { string s; while (getline (cin,s)){ string c; int num=0 ; for (int i=0 ;i<s.size ();i++){ if (isalpha (s[i])){ c+=s[i]; } } stable_sort (c.begin (),c.end (),cmp_); for (int i=0 ;i<s.size ();i++){ if (!isalpha (s[i])) num++; if (isalpha (s[i])){ cout<<c[i-num]; } if (!isalpha (s[i])){ cout<<s[i]; } } cout<<endl; } }
1261 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 #include <bits/stdc++.h> using namespace std;bool cmp_ (string x,string y) { return x.size ()<y.size (); } int main () { int n; while (cin>>n){ cin.ignore (); string* s = new string[n]; int num=0 ; for (int i=0 ;i<n;i++){ getline (cin,s[i]); if (s[i]=="stop" ){ break ; } num++; } sort (s,s+num,cmp_); for (int i=0 ;i<num;i++){ cout<<s[i]<<endl; } } }
1294 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;bool cmp_ (string x,string y) { return x.size ()<y.size (); } int main () { string s; while (getline (cin,s)){ int len=s.size (); string ss[len]; for (int i=0 ;i<len;i++){ ss[i]=s.substr (i); } sort (ss,ss+len); for (int i=0 ;i<len;i++){ cout<<ss[i]<<endl; } } }
1338 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 49 50 51 52 53 54 55 56 57 58 59 60 61 #include <bits/stdc++.h> using namespace std;struct student { string id; string name; int score; }; bool cmp_1 (student arr1, student arr2) { return arr1. id<arr2. id; } bool cmp_2 (student arr1, student arr2) { if (arr1. name==arr2. name) return arr1. id<arr2. id; else return arr1. name<arr2. name; } bool cmp_3 (student arr1,student arr2) { if (arr1. score==arr2. score) return arr1. id<arr2. id; return arr1. score<arr2. score; } int main () { int n,m; while (cin>>n>>m){ vector<student> arr (n) ; for (int i=0 ;i<n;i++){ cin>>arr[i].id>>arr[i].name>>arr[i].score; } cout << "Case" << ":" << endl; if (m==1 ){ sort (arr.begin (), arr.end (),cmp_1); for (int i=0 ;i<n;i++){ cout<<arr[i].id<<' ' <<arr[i].name<<' ' <<arr[i].score<< endl; } } if (m==2 ){ sort (arr.begin (), arr.end (),cmp_2); for (int i=0 ;i<n;i++){ cout<<arr[i].id<<' ' <<arr[i].name<<' ' <<arr[i].score<< endl; } } if (m==3 ){ sort (arr.begin (), arr.end (),cmp_3); for (int i=0 ;i<n;i++){ cout<<arr[i].id<<' ' <<arr[i].name<<' ' <<arr[i].score<< endl; } } } }
1412 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;bool cmp (string a,string b) { if (a.length () != b.length ()) { return a.length () < b.length (); } else return a<b; } int main () { int n; while (cin>>n){ vector<string> s (n) ; for (int i=0 ;i<n;i++){ cin>>s[i]; } sort (s.begin (),s.end (),cmp); for (int i=0 ;i<n;i++){ cout<<s[i]<<endl; } } }
sort函数的应用 自定义比较函数cmp(a, b)的返回值本质是回答一个问题:「a 是否应该排在 b 的前面?」
返回true → a 排在 b 前面;返回false → a 不排在 b 前面(b 可能在前,或两者相等)。
所以判断逻辑一定是!=,把排序环节放到return中
1817 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;struct student { string name; int yw; int sx; int yy; int score; }; bool cmp (student a,student b) { if (a.score != b.score) return a.score>b.score; else if (a.yw != b.yw) return a.yw>b.yw; else if (a.sx != b.sx) return a.sx>b.sx; else if (a.yy != b.yy) return a.yy>b.yy; return false ; } int main () { int n; cin>>n; vector<student> a (n) ; for (int i=0 ;i<n;i++){ cin>>a[i].name>>a[i].yw>>a[i].sx>>a[i].yy; a[i].score=a[i].yw+a[i].sx+a[i].yy; } stable_sort (a.begin (),a.end (),cmp); for (int i=0 ;i<n;i++){ cout<<a[i].name<<' ' <<a[i].yw<<' ' <<a[i].sx<<' ' <<a[i].yy<<endl; } }
return false;为什么要用这一句
cmp函数的返回值类型是bool(布尔型),C++ 规定:所有路径的函数执行结束时,必须有明确的返回值 ,否则编译器会报「函数缺少返回值」的警告(甚至直接编译失败),即使编译通过,也会返回随机的内存值(导致排序逻辑混乱)。
当两个学生的总分、语文、数学、英语完全相同 时,所有if/else if都不触发,函数执行到末尾没有返回值
cmp(a, b) 的返回值本质是回答:「a 是否应该排在 b 的前面?」
返回true → a 排在 b 前面;
返回false → a 不排在 b 前面(b 在前,或两者保持原始顺序)。
当两个学生所有成绩都相同时,我们的需求是「按出现的顺序输出」,此时:
若 a 是先输入的、b 是后输入的,我们不希望交换 a 和 b 的位置;
因此cmp(a, b)应返回false(表示「a 不需要排在 b 前面」),sort/stable_sort就不会交换两者的位置,从而保留原始输入顺序。
1798 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;int main () { string s; cin>>s; stringstream ss (s) ; vector<int > num; string temp; while (getline (ss,temp,',' )){ int a=stoi (temp); num.push_back (a); } sort (num.begin (),num.end ()); for (int i=0 ;i<num.size ();++i){ if (i%4 ==0 &&i!=0 ){ cout<<endl<<num[i]<<' ' ; } else cout<<num[i]<<' ' ; } }
2.7 1177 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 main () { int m,n,num; while (cin>>n>>m){ vector<int > reader (n) ; map<int ,int > book_count; for (int i=0 ;i<n;i++){ cin>>num; reader[i]=num; book_count[num]++; } for (int i=0 ;i<n;i++){ int p=reader[i]; if (book_count[p]-1 >0 ) cout<<book_count[p]-1 <<endl; else cout<<"BeiJu" <<endl; } } }
数组去重操作 一、最常用的去重方法(排序 + unique+erase) 这是 C++ 处理数组去重的经典组合 ,也是你代码中用到的方式,核心逻辑是「先排序让重复元素连续,再移除连续重复项」,步骤清晰且效率高(时间复杂度 O (n logn),主要来自排序)。
1. 核心步骤(四步走)
步骤
函数 / 操作
作用
关键说明
1
sort(vec.begin(), vec.end())
升序排序
让重复元素变成「连续的一段」(unique 只能去连续重复)
2
auto last = unique(vec.begin(), vec.end())
移动重复元素
把连续重复的元素移到数组末尾,返回「去重后最后一个有效元素的迭代器」
3
vec.erase(last, vec.end())
删除重复元素
擦除末尾的重复元素,完成真正去重
4
(可选)reverse(vec.begin(), vec.end())
降序去重
若需要降序,排序后反转即可
sort :必须先排序!因为unique只处理「连续的重复元素」,未排序的数组(如[2,1,2])用unique无法去重;
unique :不是 “删除” 重复元素,而是 “覆盖 + 移动”—— 把不重复的元素依次放到数组前面,重复元素挤到末尾,返回值是「不重复元素的最后一个位置」;
erase :根据unique的返回值,删除从该位置到数组末尾的所有元素,完成真正去重。
1383 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 main () { int n,k; while (cin>>n){ vector<int > a (n) ; for (int i=0 ;i<n;i++){ cin>>a[i]; } cin>>k; sort (a.begin (),a.end ()); auto last=unique (a.begin (),a.end ()); a.erase (last,a.end ()); cout<<a[k-1 ]<<endl; } }
1387 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 #include <bits/stdc++.h> using namespace std;int main () { string s; while (cin>>s){ int num; cin>>num; while (num--){ string option; cin>>option; int i = option[1 ] - '0' ; int len = option[2 ] - '0' ; if (option[0 ]=='0' ){ reverse (s.begin ()+i,s.begin ()+i+len); cout<<s<<endl; } else if (option[0 ] == '1' ) { string re_string =option.substr (3 ); s.replace (i,len,re_string); cout<<s<<endl; } } } }
2.8贪心 1478
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 #include <bits/stdc++.h> using namespace std; struct drink{ int mi; int wi; }; bool cmp(drink a1,drink a2){ double res1=a1.mi*1.0/a1.wi; double res12=a2.mi*1.0/a2.wi; return res1>res12; } int main () { int x,n; while(cin>>x>>n){ if(x==-1&&n==-1) break; drink a[1002]; for(int i=0;i<n;i++){ cin>>a[i].mi>>a[i].wi; } sort(a,a+n,cmp); double result=0; for(int i=0;i<n;i++){ if(x>=a[i].wi){ result+=a[i].mi; x-=a[i].wi; } else if(x<a[i].wi){ //注意浮点数运算 result += (a[i].mi *1.0/ a[i].wi) * x; break; } } printf("%.3f\n", result); } }