欢迎找茬
核桃的数量
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<vector> #include<set> #include<map> #include<queue> #include<cstdio> using namespace std; int gcd(int a, int b){ return (b == 0)? a: gcd(b,a%b); } int main(){ int a, b, c, d; cin>>a>>b>>c; int A = a/gcd(a,b)*b, B = a/gcd(a,c)*c; cout<<A/gcd(A,B)*B<<endl; return 0; }
合根植物
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<vector> #include<set> #include<map> #include<queue> #include<cstdio> using namespace std; const int maxn = (1000+10) * (1000+10); int fa[maxn]; int n, m, k, N; int a, b; int findfa(int x){ while(x != fa[x]) x = fa[x]; return x; } int main(){ cin>>n>>m>>k; N = n*m; for(int i = 1; i <= N; i++) fa[i] = i; while(k--){ cin>>a>>b; int A = findfa(a), B = findfa(b); if(A != B){ fa[A] = B; N--; } } cout<<N<<endl; return 0; } /* sample input 5 4 16 2 3 1 5 5 9 4 8 7 8 9 10 10 11 11 12 10 14 12 16 14 18 17 18 15 19 19 20 9 13 13 17 sample output 5 */
小数第n位
// 注意弯道超车
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<vector> #include<set> #include<map> #include<queue> #include<cstdio> using namespace std; typedef long long ll; ll a, b, n, t, k = 3, cnt = 0; int main(){ cin>>a>>b>>n; a = a%b; while(n-10 > 3){ //speed up a = (a * 10000000000) % b; n -= 10; if(a == 0) { cout<<"000\n"; return 0; } } while(k){ a = a*10; t = a / b; a = a % b; if(++cnt >= n){ cout<<t; k--; } } return 0; } /* sample input 1 8 1 1 8 3 282866 999000 6 sample output 125 500 914 */
回文数字
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<vector> #include<set> #include<map> #include<queue> #include<cstdio> using namespace std; int ok = 0; int n; void solve0(){ for(int x = 1; x < 10; x++){ for(int y = 0; y < 10; y++) { int z = n-2*x-2*y; if( z>=0 && z<=9){ cout<<x<<y<<(n-2*x-2*y)<<y<<x<<endl; ok = 1; } } } } void solve1(){ for(int x = 1; x < 10; x++){ for(int y = 0; y < 10; y++) { int z = n-2*x-2*y; if( z%2==0 && z>=0 && z<=18){ cout<<x<<y<<z/2<<z/2<<y<<x<<endl; ok = 1; } } } } int main(){ cin>>n; solve0(); solve1(); if(!ok) cout<<"-1\n"; return 0; } /* sample input 40 sample output 99899 499994 589985 598895 679976 688886 697796 769967 778877 787787 796697 859958 868868 877778 886688 895598 949949 958859 967769 976679 985589 994499 */
数字游戏
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<vector> #include<set> #include<map> #include<queue> #include<cstdio> using namespace std; typedef long long ll; int main() { ll n, k, T; ll sum = 0; cin>>n>>k>>T; for(ll i = 0; i < T ; i++){ ll t = 1+i*n; if(t%2==0) sum+= (1+( ((t/2)%k) *((t-1)%k) )%k )%k; else sum+= (1+( (t%k) *( ((t-1)/2)%k) )%k )%k; } cout<<sum<<endl; return 0; } /* sample input 999999 999999 999999 sample output 999999 */
九宫幻方
我们先将所有结果跑出放到一个数组中
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<vector> #include<set> #include<map> #include<queue> #include<cstdio> using namespace std; char A[8][10]= {"276951438", "294753618", "438951276", "492357816", "618753294", "672159834", "816357492", "834159672"}; int main(){ int a[9], cnt = 0, indix; for(int i = 0; i < 9; i++) cin>>a[i]; for(int i = 0; i < 8; i++){ bool ok = true; for(int j = 0; j < 9; j++){ if(a[j] && a[j] != (int)(A[i][j]-'0')) { ok = false; break; } } if(ok) {cnt++; indix = i;} } if(cnt > 1) cout<<"Too Many\n"; else for(int i = 0; i < 10; i++){ cout<<A[indix][i]; if(i % 3 == 2) cout<<endl; else cout<<" "; } } /* #define _for(i,a,b) for(i = (a); i <= (b); i++) int x[9],xx[9]; bool judge(){ memcpy(xx,x,sizeof(x)); sort(xx,xx+9); for(int i = 0; i < 9; i++) if(xx[i] != i+1) return false; if(x[6]+x[7] + x[8] != 15 || x[2]+x[5] + x[8] != 15 || x[2]+x[4] + x[6] != 15) return false; return true; } void print(){ for(int i = 0; i < 9; i++) cout<<x[i]; cout<<endl; } int main() { _for(x[0],0,9){ _for(x[1],0,9){ _for(x[3],0,9){ _for(x[4],0,9){ x[2] = 15-x[0]-x[1]; x[5] = 15-x[3]-x[4]; x[6] = 15-x[0]-x[3]; x[7] = 15-x[1]-x[4]; x[8] = 15-x[0]-x[4]; if(judge() ) print(); } } } } return 0; } 276951438 294753618 438951276 492357816 618753294 672159834 816357492 834159672 */
excel地址
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<vector> #include<set> #include<map> #include<queue> #include<cstdio> using namespace std; int main(){ int n; string s; cin>>n; while(n>0){ s += char((--n)%26+'A'); n = n/26; } reverse(s.begin(),s.end()); cout<<s; return 0; } /* input 2054 output BZZ */
日期问题
//注意重复
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<vector> #include<set> #include<map> #include<queue> #include<cstdio> using namespace std; int A[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int y, m, d, k = 0; char s[3][20]; string ss[3]; bool is_rightday(int yy,int mm, int dd){ if(yy >= 60) yy += 1900; else yy += 2000; if( (mm < 1) || (mm > 12) ) return false; if(yy % 4) A[2] = 28; else A[2] = 29; if(dd > A[mm] || dd < 1) return false; sprintf(s[k],"%d-%02d-%02d\n",yy,mm,dd); return true; } int main(){ int a, b, c; scanf("%d/%d/%d",&a,&b,&c); if( is_rightday(a,b,c) )k++; if( is_rightday(c,b,a) )k++; if( is_rightday(c,a,b) )k++; for(int i = 0; i < k; i++) ss[i] = s[i]; sort(ss,ss+k); int n = unique(ss,ss+k) - ss; for(int i = 0; i < n; i++) cout<<ss[i]; return 0; }
最大子阵
//别人的思想 混杂的代码 自己的知识
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<vector> #include<set> #include<map> #include<queue> #include<cstdio> using namespace std; const int maxn = 500+5; int M[maxn][maxn], C[maxn][maxn]; int n, m; int a[maxn], dp[maxn]; int main(){ cin>>n>>m; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ scanf("%d",&M[i][j]); C[i][j] = M[i][j] + C[i-1][j]; } } int ans = M[1][1]; for(int i = 0; i < n; i++){ for(int j = i+1; j <= n; j++){ //以下为i+1到j行最大方阵求解 for(int k = 1; k <= m; k++){ dp[k] = C[j][k]-C[i][k]; if(dp[k-1] > 0) dp[k] += dp[k-1]; ans = max(ans, dp[k] ); } } } cout<<ans<<endl; return 0; } /* input 3 3 -1 -4 3 3 4 -1 -5 -2 8 output 10 */
油漆问题
//当maxn为8000+50为80分 为10000+50时为0分 待完善
#include<iostream> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> #include<vector> #include<set> #include<map> #include<queue> #include<cstdio> using namespace std; const int maxn = 10000+50; const int dx[] = {0,0,1,-1}; const int dy[] = {1,-1,0,0}; int X1[maxn], Y1[maxn], X2[maxn], Y2[maxn]; int x[maxn*2], y[maxn*2]; int color[maxn][maxn]; bool v[maxn][maxn]; int n, nx, ny; int ans = 0; int part(int *x){ sort(x,x+2*n+2); return unique(x,x+2*n+2)-x; } int ID(int a, int n, int *x){ return lower_bound(x, x+n, a) - x; } int main(){ cin>>n; x[0] = y[0] =0, x[1] = y[1] = maxn; for(int i = 1; i <= n; i++){ scanf("%d%d%d%d",&X1[i],&Y1[i],&X2[i],&Y2[i]); x[2*i] = ++X1[i]; x[2*i+1] = ++X2[i]; y[2*i] = ++Y1[i]; y[2*i+1] = ++Y2[i]; } nx = part( x ); ny = part( y ); for(int i = 1; i <= n; i++){ int xx1 = ID(X1[i],nx,x), xx2 = ID(X2[i],nx,x); int yy1 = ID(Y1[i],ny,y), yy2 = ID(Y2[i],ny,y); for(int j = xx1; j < xx2; j++) for(int k = yy1; k < yy2; k++) color[j][k] = 1; } for(int i = 0; i < nx; i++) for(int j = 0; j < ny; j++) if(color[i][j]) ans += ( x[i+1]-x[i] )*( y[j+1]-y[j] ); cout<<ans<<endl; return 0; } /* input 3 1 5 10 10 3 1 20 20 2 7 15 17 --------------------- 3 5 2 10 6 2 7 12 10 8 1 15 15 output 340 -------------------- 128 */