辽宁师范大学 • 张大为@https://daweizh.github.io/noip/

1.6 编程基础之一维数组(15)

  1. 与指定数字相同的数的个数,http://noi.openjudge.cn/ch0106/01/
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int N,m,a[100],sum=0;
      
      cin >> N;
      for(int i=0;i<N;i++)
      {
        cin >> a[i];	
      }
      cin >> m;
      for(int i=0;i<N;i++)
      {
        if (a[i]==m){
          sum = sum + 1;
        }
      }
      cout << sum << endl;
      
      return 0;	
    }   
    
  2. 陶陶摘苹果,http://noi.openjudge.cn/ch0106/02/
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int a[100],h,sum=0;
      
      for(int i=0;i<10;i++)
      {
        cin >> a[i];	
      }
      cin >> h;
      for(int i=0;i<10;i++)
      {
        if (a[i]<=30+h){
          sum = sum + 1;
        }
      }
      cout << sum << endl;
      
      return 0;	
    } 
    
  3. 计算书费,http://noi.openjudge.cn/ch0106/03/
    #include <iostream>
    using namespace std;
    
    int main()
    {
      float cost=0, price[]={28.9,32.7,45.6,78.0,35.0,86.2,27.8,43.0,56.0,65.0};
      int num[10];
      
      for(int i=0;i<10;i++)
      {
        cin >> num[i];	
      }
      for(int i=0;i<10;i++)
      {
        cost = cost + price[i] * num[i]; 
      }
      printf("%.1f",cost);
      
      return 0;	
    }   
    
  4. 数组逆序重放,http://noi.openjudge.cn/ch0106/04/
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int num[100],n;
      
      cin >> n;
      for(int i=0;i<n;i++)
      {
        cin >> num[i];	
      }
      cout << num[n-1];
      for(int i=n-2;i>=0;i--)
      {
        cout << " " << num[i]; 
      }
      cout << endl;
      
      return 0;	
    }   
    
  5. 年龄与疾病,http://noi.openjudge.cn/ch0106/05/
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int num[100],n,sum=0;
      float age[4] = {0.0,0.0,0.0,0.0};
      
      cin >> n;
      for(int i=0;i<n;i++)
      {
        cin >> num[i];
        sum = sum + 1;
        if(num[i]<=18){
          age[0] = age[0] + 1;
        }else if(num[i]<=35){
          age[1] = age[1] + 1;
        }else if(num[i]<=60){
          age[2] = age[2] + 1;
        }else{
          age[3] = age[3] + 1;
        }
      }
      for(int i=0;i<4;i++)
      {
        printf("%.2f%%\n",age[i]*100.00f/sum); 
      }
      
      return 0;	
    }   
    
  6. 校门外的树,http://noi.openjudge.cn/ch0106/06/
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int begin[100],end[100],tree[10000],L,M,sum=0;
      
      cin >> L >> M;
      for(int i=0;i<=L;i++){
        tree[i] = 1;
      }
      for(int i=0;i<M;i++)
      {
        cin >> begin[i] >> end[i];
        for(int j=begin[i];j<=end[i];j++){
          tree[j]=0;
        }
      }
      for(int i=0;i<=L;i++)
      {
        if(tree[i]==1){
          sum = sum + 1;
        }; 
      }
      cout << sum << endl;
      
      return 0;	
    }  
    
  7. 有趣的跳跃,http://noi.openjudge.cn/ch0106/07/
    #include<iostream>
    #include<cmath>
    using namespace std;
    
    int x[10000],y[10000];
    int L,t,g,n,ok=1;
    
    int main() {
      cin>>n;
      for(int i=1;i<=n;i++){
        cin>>x[i];	
      }
      for(int i=2;i<=n;i++){
        t=abs(x[i]-x[i-1]);	
        y[t]++;
      }	
      for(int i=1;i<n;i++){
        if(y[i]!=1){
          ok=0;
        }
      }
      if(ok)
        cout<<"Jolly";
      else 
        cout<<"Not jolly";
      return 0;
    }  
    
  8. 石头剪刀布,http://noi.openjudge.cn/ch0106/08/
    #include <iostream>
    using namespace std;
    
    int main(){
      int a[10000],b[10000];
      int n,na,nb;
      cin >> n >> na >> nb;
      
      for(int i=0;i<na;i++) cin >> a[i];
      for(int i=0;i<nb;i++) cin >> b[i];
      
      int p = na;
      while(p<n){
        a[p] = a[p%na];
        p++;
      }
      p = nb;
      while(p<n){
        b[p] = b[p%nb];
        p++;
      }
    
      int A=0,B=0;	
      for(int i=0;i<n;i++){
        if(a[i]==0 && b[i]==2){
          A++;
        }
        if(a[i]==2 && b[i]==5){
          A++;
        }
        if(a[i]==5 && b[i]==0){
          A++;
        }
        if(a[i]==2 && b[i]==0){
          B++;
        }
        if(a[i]==5 && b[i]==2){
          B++;
        }
        if(a[i]==0 && b[i]==5){
          B++;
        }
      }
      
      if(A>B)
        cout << "A" << endl;
      else if(B>A)
        cout << "B" << endl;
      else
        cout << "draw" << endl; 
      
      return 0;
    }   
    
  9. 向量点积计算,http://noi.openjudge.cn/ch0106/09/
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int n,a[1000],b,sum=0;
      
      cin >> n;
      for(int i=0;i<n;i++)
      {
        cin >> a[i];
      }
      for(int i=0;i<n;i++)
      {
        cin >> b;
        sum = sum + a[i] * b;
      }
      cout << sum << endl;
      
      return 0;	
    }   
    
  10. 大整数加法,http://noi.openjudge.cn/ch0106/10/
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main(){
      char a[300],b[300];
      cin >> a >> b ;
      //cout << a << b << endl;
      int alen = strlen(a);
      int blen = strlen(b);
      //cout << alen << blen << endl;
      
      int max = alen;
      if (blen>max) max = blen;
      //cout << max << endl;
      
      int c[max+1],d[max+1];
      int j,k;
      for (int i=1;i<=max;i++){
        if(i<=alen)
          c[max-i] = a[alen-i] - 48;
        else
          c[max-i] = 0;
          //cout << "a" << c[max-i] << endl;
        if(i<=blen)
          d[max-i] = b[blen-i] - 48;
        else
          d[max-i] = 0;
        //cout << "b" << d[max-i] << endl;
      }
      //printf("%d\n",max);
      
      int more = 0, p=0;
      //cout << more << endl;
      for (int i=max-1 ;i>=0;i--){
        //cout << more << " ";
        p = c[i] + d[i] + more;
        //cout << c[i] << "  " << d[i] <<  "  "  << p << " " << more << endl;
        if(p>=10){
          more = 1;
          c[i] = p - 10;
        }else{
          more = 0;
          c[i] = p ;
        }
      }
      
      int i = 0;
      if (more>0) cout << more;
      else
      for (i=0;i<max;i++) {
        //cout << c[i] << endl;
        if (c[i]>0){
          break;
        }
      } 
      if(i==max) cout << 0 << endl;
      
      for (int j=i;j<max;j++)
        cout << c[j];
      cout << endl;
          
      return 0;
    }  
    
  11. 大整数减法,http://noi.openjudge.cn/ch0106/11/
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    int a[201],b[201];
    char c[201];
    
    int main(){
      gets(c);
      int lena = strlen(c);
      for(int i=0;i<lena;i++)
          a[i] = c[i] - '0';
      reverse(a,a+lena);
      
      gets(c);
      int lenb = strlen(c);
      for(int i=0;i<lenb;i++)
          b[i] = c[i] - '0';
      reverse(b,b+lenb);
      
      for(int i=0;i<lenb;i++){
        if(a[i]<b[i]){
            a[i] = a[i] + 10 - b[i];
          a[i+1] = a[i+1] -1; 
        }else{
          a[i] = a[i] - b[i];
        }
      }
    
      reverse(a,a+lena);
      int i=0;
      while(a[i]==0) i++;
      for(;i<lena;i++)
        cout << a[i];
      cout << endl;	
        
        return 0;
    }  
    
  12. 计算2的N次方,http://noi.openjudge.cn/ch0106/12/
    #include <iostream>
    using namespace std;
    
    int ans[100];
    
    int main(){
      int n,i,m=1;
      cin >> n;
      ans[0]=1;
      while(n--){
        int more =0;
        i=0;
        for(;i<m;i++){
          int temp = ans[i] * 2 + more;
          ans[i]=temp % 10;
          more = temp / 10;
        }
        if(more){
              ans[i]=more;
          m++;
        }	
      }
      
      while(!ans[i]) i--;
      for(int t=i;t>=0;t--){
        cout << ans[t];
      }
      cout << endl;
      
        return 0;
    }  
    
  13. 大整数的因子,http://noi.openjudge.cn/ch0106/13/
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int n[100];
    char c[100];
    
    int main(){
      int flag = 1;
      gets(c);
    
      int len = strlen(c);
      for(int i=0;i<len;i++) 
        n[i] = c[i]-'0';
    
      for(int k=2;k<=9;k++){
        int r = 0;
        for(int i=0;i<len;i++){
          r = (n[i] +r*10) % k; 
        }
        if(r==0){
          cout << k << " ";
          flag = 0;
        }
      }		
      if(flag) cout << "none" << endl;
      else cout << endl;
        
      return 0;
    }  
    
  14. 求10000以内n的阶乘,http://noi.openjudge.cn/ch0106/14/
    #include <iostream>
    using namespace std;
    
    int ans[100000];
    
    int main(){
      int n,len=1;
      cin >> n;
      ans[0]=1;
      for(int i=2;i<=n;i++){
        int more=0,j=0;
            for(;j<len;j++){
              int tmp = ans[j]*i;
              int r =  (tmp + more) / 10;
              ans[j] = (tmp + more) % 10;
          more = r; 
        }
        while(more>0){
          ans[len++] = more % 10;
          more = more / 10;
        }
      }
      for(int i=len-1;i>=0;i--)
          cout << ans[i];
      cout << endl;
      
        return 0;
    }  
    
  15. 阶乘和,http://noi.openjudge.cn/ch0106/15/
    #include <iostream>
    using namespace std;
    
    int ans[100];
    int p[100];
    
    int main(){
      int n,len=1;
      cin >> n;
      p[0]=1,ans[0]=1;
      for(int i=2;i<=n;i++){
        int more=0;
            for(int j=0;j<len;j++){
              int tmp = p[j]*i;
              int r =  (tmp + more) / 10;
              p[j] = (tmp + more) % 10;
          more = r; 
        }
        while(more>0){
          p[len++] = more % 10;
          more = more / 10;
        }
        more = 0;
        for(int j=0;j<=len;j++){
          int tmp = ans[j]+p[j]+more; 
          ans[j] = tmp % 10;
          more = tmp / 10;
        }
      }
      for(int i=len-1;i>=0;i--)
          cout << ans[i];
      cout << endl;		
      
        return 0;
    }  
    

参考网址

  1. http://noi.openjudge.cn