• 51nod水题集


    1000 A + B

    1 #include<iostream>
    2 using namespace std;
    3 int main(){
    4     int a,b;
    5     cin>>a>>b;
    6     cout<<a+b;
    7     return 0;
    8 } 
    View Code

    1005 大数加法

     1 #include<iostream>
     2 #include<cmath>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<algorithm>
     6 using namespace std;
     7 const int MAXN=10005;
     8 struct bign{
     9     int num,f[MAXN],fl;
    10     bign(){num=0; memset(f,0,sizeof(f)); fl=1;}
    11     void read(){
    12         char last=' ',ch=getchar();
    13         while (ch<'0' || ch>'9') last=ch,ch=getchar();
    14         while (ch>='0' && ch<='9') f[++num]=ch-'0',ch=getchar();
    15         if (last=='-') fl=-1;
    16         for (int i=1;i<=(num/2);i++) swap(f[i],f[num-i+1]); 
    17     }
    18     void out(){
    19         if (fl==-1) printf("-");
    20         for (int i=num;i>=1;i--) printf("%d",f[i]);
    21     }
    22 }a,b;
    23 bign operator +(bign a,bign b){
    24     bign ans; int num_=max(a.num,b.num);
    25     for (int i=num_;i>=1;i--)
    26         ans.f[i]=a.f[i]*a.fl+b.f[i]*b.fl;
    27     for (int i=1;i<=num_;i++)
    28         if (abs(ans.f[i])>9) ans.f[i+1]+=ans.f[i]/10,ans.f[i]=ans.f[i]%10;
    29     ans.num=num_+1;
    30     while (ans.f[ans.num]==0 && ans.num>1) ans.num--;
    31     if (ans.f[ans.num]<0) {
    32         ans.fl=-1;
    33         for (int i=1;i<=ans.num;i++) ans.f[i]=-ans.f[i];
    34     }
    35     for (int i=1;i<=ans.num;i++)
    36             if (ans.f[i]<0) ans.f[i]+=10,ans.f[i+1]--;
    37     while (ans.f[ans.num]==0 && ans.num>1) ans.num--;
    38     return ans;
    39 }
    40 int main(){
    41     a.read(),b.read();
    42     a=a+b; a.out();
    43     return 0;
    44 }
    View Code

    1006 最长公共子序列Lcs

     1 #include<iostream>
     2 #include<cmath>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<algorithm>
     6 using namespace std;
     7 #define N 1005 
     8 char ch1[N],ch2[N],ans[N];
     9 int f[N][N],nu;
    10 struct node{
    11     int i,j;    
    12 }t[N][N];
    13 void solve(int i,int j){
    14     if (i==0 || j==0) return ; 
    15     if (t[i][j].i==i-1 && t[i][j].j==j-1) ans[++nu]=ch1[i];
    16     solve(t[i][j].i,t[i][j].j); 
    17 }
    18 int main(){
    19     scanf("%s%s",ch1+1,ch2+1);
    20     int len1=strlen(ch1+1),len2=strlen(ch2+1);
    21     for (int i=1;i<=len1;i++)
    22         for (int j=1;j<=len2;j++){
    23             if (f[i][j]<f[i][j-1]) f[i][j]=f[i][j-1],t[i][j].i=i,t[i][j].j=j-1;
    24             if (f[i][j]<f[i-1][j]) f[i][j]=f[i-1][j],t[i][j].i=i-1,t[i][j].j=j;
    25             if (ch1[i]==ch2[j] && f[i][j]<f[i-1][j-1]+1) f[i][j]=f[i-1][j-1]+1,t[i][j].i=i-1,t[i][j].j=j-1;
    26         } 
    27     solve(len1,len2);
    28     for (int i=nu;i>=1;i--)
    29         printf("%c",ans[i]);
    30     return 0;
    31 }
    View Code

    1008 N的阶乘 mod P

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm> 
     4 using namespace std;
     5 typedef long long ll;
     6 ll read(){
     7     char last=' ',ch=getchar(); ll ans=0;
     8     while (ch<'0'||ch>'9') last=ch,ch=getchar();
     9     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    10     if (last=='-') ans=-ans; return ans;
    11 }
    12 int main(){
    13     ll n=read(),p=read(),ans=1LL;
    14     for (ll i=1;i<=n;i++) ans=(ans*i)%p;
    15     printf("%lld",ans);
    16     return 0;
    17 }
    View Code

    1011 最大公约数GCD

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm> 
     4 using namespace std;
     5 typedef long long ll;
     6 ll read(){
     7     char last=' ',ch=getchar(); ll ans=0;
     8     while (ch<'0'||ch>'9') last=ch,ch=getchar();
     9     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    10     if (last=='-') ans=-ans; return ans;
    11 }
    12 int gcd(int a,int b){
    13     return (b==0)?a:gcd(b,a%b);
    14 }
    15 int main(){
    16     int a=read(),b=read();
    17     printf("%d",gcd(a,b));
    18     return 0;
    19 } 
    View Code

    1012 最小公倍数LCM

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm> 
     4 using namespace std;
     5 typedef long long ll;
     6 ll read(){
     7     char last=' ',ch=getchar(); ll ans=0;
     8     while (ch<'0'||ch>'9') last=ch,ch=getchar();
     9     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    10     if (last=='-') ans=-ans; return ans;
    11 }
    12 int gcd(int a,int b){
    13     return (b==0)?a:gcd(b,a%b);
    14 }
    15 int main(){
    16     int a=read(),b=read();
    17     printf("%lld",(ll)a*(ll)b/(ll)gcd(a,b));
    18     return 0;
    19 } 
    View Code

     1018 排序

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm> 
     4 using namespace std;
     5 typedef long long ll;
     6 ll read(){
     7     char last=' ',ch=getchar(); ll ans=0;
     8     while (ch<'0'||ch>'9') last=ch,ch=getchar();
     9     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    10     if (last=='-') ans=-ans; return ans;
    11 }
    12 int a[50005];
    13 int main(){
    14     int n=read();
    15     for (int i=1;i<=n;i++) a[i]=read();
    16     sort(a+1,a+n+1);
    17     for (int i=1;i<=n;i++) printf("%d
    ",a[i]);
    18     return 0;
    19 }
    View Code

    1019 逆序数

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm> 
     4 using namespace std;
     5 typedef long long ll;
     6 ll read(){
     7     char last=' ',ch=getchar(); ll ans=0;
     8     while (ch<'0'||ch>'9') last=ch,ch=getchar();
     9     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    10     if (last=='-') ans=-ans; return ans;
    11 }
    12 int a[50005],b[50005];
    13 ll ans;
    14 void merge(int l,int r){
    15     int mid=(l+r)>>1,i=l,j=mid+1,nu=0;
    16     while (i<=mid && j<=r){
    17         while (i<=mid && a[i]<=a[j]) b[++nu]=a[i],i++;
    18         while (j<=r && a[j]<a[i]) b[++nu]=a[j],j++,ans+=(ll)(mid-i+1);
    19     }
    20     while (i<=mid) b[++nu]=a[i],i++;
    21     while (j<=r) b[++nu]=a[j],j++;
    22     for (int i=1;i<=nu;i++) a[l+i-1]=b[i];
    23 }
    24 void msort(int l,int r){
    25     if (l==r) return ;
    26     int mid=(l+r)>>1; 
    27     msort(l,mid);
    28     msort(mid+1,r);
    29     merge(l,r);
    30 }
    31 int main(){
    32     int n=read();
    33     for (int i=1;i<=n;i++) a[i]=read();
    34     msort(1,n);
    35     printf("%d",ans);
    36     return 0;
    37 }
    View Code

    1027 大数乘法

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm> 
     6 using namespace std;
     7 const int MAXN=2005;
     8 struct bign{
     9     int num,f[MAXN],fl;
    10     bign(){num=0; memset(f,0,sizeof(f)); fl=1;}
    11     void read(){
    12         char last=' ',ch=getchar();
    13         while (ch<'0' || ch>'9') last=ch,ch=getchar();
    14         while (ch>='0' && ch<='9') f[++num]=ch-'0',ch=getchar();
    15         if (last=='-') fl=-1;
    16         for (int i=1;i<=(num/2);i++) swap(f[i],f[num-i+1]); 
    17     }
    18     void out(){
    19         if (fl==-1) printf("-");
    20         for (int i=num;i>=1;i--) printf("%d",f[i]);
    21     }
    22 }a,b;
    23 bign operator *(bign a,bign b){
    24     bign c; c.fl=a.fl*b.fl;
    25     for (int i=1;i<=a.num;i++)
    26         for (int j=1;j<=b.num;j++){
    27             c.f[i+j-1]+=a.f[i]*b.f[j];
    28         }
    29     c.num=a.num+b.num;
    30     for (int i=1;i<=c.num;i++) c.f[i+1]+=c.f[i]/10,c.f[i]%=10;
    31     while (c.f[c.num]==0) c.num--;
    32     return c;
    33 }
    34 int main(){
    35     a.read(),b.read();
    36     a=a*b; a.out();
    37     return 0;
    38 }
    View Code

    1046 A^B Mod C

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm> 
     4 using namespace std;
     5 typedef long long ll;
     6 ll read(){
     7     ll ans=0; char last=' ',ch=getchar();
     8     while (ch<'0'||ch>'9') last=ch,ch=getchar();
     9     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    10     if (last=='-') ans=-ans; return ans;
    11 }
    12 int main(){
    13     ll a=read(),b=read(),c=read(),ans=1;
    14     for (;b;b>>=1,a=(a*a)%c) if (b&1) ans=(ans*a)%c;
    15     printf("%lld",ans);
    16     return 0;
    17 }
    View Code

    1049 最大子段和

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm> 
     4 using namespace std;
     5 typedef long long ll;
     6 const int inf=1<<29;
     7 ll read(){
     8     ll ans=0; char last=' ',ch=getchar();
     9     while (ch<'0'||ch>'9') last=ch,ch=getchar();
    10     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    11     if (last=='-') ans=-ans; return ans;
    12 }
    13 ll f[50005],a[50005];
    14 int main(){
    15     int n=read(); ll ans=0; f[0]=-inf;
    16     for (int i=1;i<=n;i++) a[i]=read();
    17     for (int i=1;i<=n;i++) f[i]=max(f[i-1]+a[i],a[i]);
    18     for (int i=1;i<=n;i++) ans=max(ans,f[i]);
    19     printf("%lld",ans);
    20     return 0;
    21 }
    View Code

    1057 N的阶乘

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm> 
     6 using namespace std;
     7 typedef long long ll;
     8 const int MAXN=1000005;
     9 ll ans[MAXN];
    10 int main(){
    11     int n; scanf("%d",&n); ans[1]=1; int num=1;
    12     for (int i=2;i<=n;i++) {
    13         int c=0;
    14         for(int j=1;j<=num;j++)
    15         {
    16             int tmp=i*ans[j]+c;
    17             c=tmp/10000; ans[j]=tmp%10000;
    18         }
    19         while(c>0)
    20         {
    21             ans[++num]=c%10000; c/=10000;
    22         }
    23     }
    24     printf("%d",ans[num]);
    25     for (int i=num-1;i>=1;i--) printf("%04d",ans[i]);
    26     return 0;
    27 }
    View Code

    1058 N的阶乘的长度

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm> 
     6 using namespace std;
     7 typedef long long ll;
     8 ll read(){
     9     ll ans=0; char last=' ',ch=getchar();
    10     while (ch<'0'||ch>'9') last=ch,ch=getchar();
    11     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    12     if (last=='-') ans=-ans; return ans;
    13 }
    14 int main(){
    15     int n=read(); double ans=1;
    16     for (int i=1;i<=n;i++) ans+=log10(i);
    17     printf("%d",(int)ans);
    18     return 0;
    19 }
    View Code

    1066 Bash游戏

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 int read(){
     6     int ans=0; char last=' ',ch=getchar();
     7     while (ch<'0'||ch>'9') last=ch,ch=getchar();
     8     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
     9     if (last=='-') ans=-ans; return ans;
    10 }
    11 int main(){
    12     int T=read();
    13     while (T--){
    14         int n=read(),k=read();
    15         if (n%(k+1)==0) printf("B
    ");
    16         else printf("A
    ");    
    17     }
    18     return 0;
    19 } 
    View Code

    1069 Nim游戏2333

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 int read(){
     6     int ans=0; char last=' ',ch=getchar();
     7     while (ch<'0'||ch>'9') last=ch,ch=getchar();
     8     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
     9     if (last=='-') ans=-ans; return ans;
    10 }
    11 int main(){
    12     int n=read(),x=0;
    13     for (int i=1;i<=n;i++) {
    14         int a=read();
    15         x^=a;
    16     }
    17     if (x==0) printf("B
    ");
    18     else printf("A
    ");    
    19     return 0;
    20 } 
    View Code

    1072 威佐夫游戏

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 int read(){
     6     int ans=0; char last=' ',ch=getchar();
     7     while (ch<'0'||ch>'9') last=ch,ch=getchar();
     8     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
     9     if (last=='-') ans=-ans; return ans;
    10 }
    11 #define N 2000000
    12 int f[N+5],nu;
    13 int main(){
    14     for (int i=1;i<=N;i++)
    15     if (!f[i]){
    16         if (i+nu+1>N) break;
    17         f[i]=i+(++nu); f[i+nu]=i;
    18     }
    19     int T=read();
    20     while (T--){
    21         int a=read(),b=read();
    22         if (f[a]==b) printf("B
    ");
    23         else printf("A
    ");    
    24     }
    25     return 0;
    26 } 
    View Code

    1089 最长回文子串 V2(Manacher算法)2333

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 typedef long long ll;
     8 ll read(){
     9     ll ans=0; char last=' ',ch=getchar();
    10     while (ch<'0'||ch>'9') last=ch,ch=getchar();
    11     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    12     if (last=='-') ans=-ans; return ans;
    13 }
    14 char ch[100005],s[200005];
    15 int p[200005];
    16 int main(){
    17     scanf("%s",ch);
    18     int n=strlen(ch),ans=0,Mi=0;
    19     for (int i=0;i<=n;i++){
    20         s[i+i+2]=ch[i];
    21         s[i+i+1]='#';
    22     }
    23     s[0]='*';
    24     for (int i=1;i<=n+n;i++){
    25         if (Mi+p[Mi]>i) p[i]=min(p[Mi+Mi-i],Mi+p[Mi]-i);
    26         else p[i]=1;
    27         while (s[i-p[i]]==s[i+p[i]]) ++p[i];
    28         if (Mi+p[Mi]<i+p[i]) Mi=i;
    29         if (p[i]>ans) ans=p[i];
    30     }
    31     printf("%d",ans-1);
    32     return 0;
    33 } 
    View Code

    1106 质数检测

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 typedef long long ll;
     8 ll read(){
     9     ll ans=0; char last=' ',ch=getchar();
    10     while (ch<'0'||ch>'9') last=ch,ch=getchar();
    11     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    12     if (last=='-') ans=-ans; return ans;
    13 }
    14 int main(){
    15     int T=read();
    16     while (T--){
    17         int n=read(),sn=sqrt(n),flag=0;
    18         for (int i=2;i<=sn;i++)
    19         if (n%i==0) {
    20             flag=1;
    21             break;
    22         }
    23         if (!flag) puts("Yes");
    24         else puts("No");
    25     }
    26     return 0;
    27 } 
    View Code

    1118 机器人走方格

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 typedef long long ll;
     8 ll read(){
     9     ll ans=0; char last=' ',ch=getchar();
    10     while (ch<'0'||ch>'9') last=ch,ch=getchar();
    11     while (ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
    12     if (last=='-') ans=-ans; return ans;
    13 }
    14 const int N=1005;
    15 ll dp[N][N];
    16 int main(){
    17     int n=read(),m=read();
    18     dp[1][1]=1;
    19     for (int i=1;i<=n;i++)
    20         for (int j=1;j<=m;j++)
    21             if (!dp[i][j]) dp[i][j]=(dp[i-1][j]+dp[i][j-1])%1000000007; 
    22     printf("%lld",dp[n][m]);
    23 }
    View Code
  • 相关阅读:
    init-method,@postcontruct,afterPropertiesSet的先后顺序
    读写分离与分库分表,分布式事务面试题
    innerHTML的HTML居然必须大写..不可思议
    postgres/greenplum unnest(Array) 实现列转行
    AWS EBS磁盘挂载和卸载
    当npm 与淘宝镜像cnpm运行都很慢时候
    IntersectionObserver API 之学习
    vue之队列过渡组效果,后进先出坑点
    ele之vue3.0的form表单验证与重置
    vue3.0之DOM的$refs之运用
  • 原文地址:https://www.cnblogs.com/SXia/p/7590872.html
Copyright © 2020-2023  润新知