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

1.9 编程基础之顺序查找(15)

  1. 查找特定的值,http://noi.openjudge.cn/ch0109/01/
    #include <iostream>
    using namespace std;
    
    int main(){
        int num[10005];
        int n,t,i;
        cin >> n;
        for(i=1;i<=n;i++){
            cin >> num[i];
        }
        cin >> t;
        for(i=1;i<=n;i++){
            if(num[i]==t){
                cout << i << endl;
                break;
            }
        }
        if(i>n)
            cout << -1 << endl;
            
        return 0;
    }
    
  2. 输出最高分数的学生姓名,http://noi.openjudge.cn/ch0109/02/
    #include <iostream>
    using namespace std;
    
    int main(){
        int n,score,mx=0;
        string name,ans;
        
        cin >> n;
        for(int i=0;i<n;i++){
            cin >> score >> name;
            if(score>mx){
                mx = score;
                ans = name;
            }
        }
        
        cout << ans << endl;
        
        return 0;
    }
    
  3. 不高兴的津津,http://noi.openjudge.cn/ch0109/03/
    #include <iostream>
    using namespace std;
    
    int main(){
      int a,b,flag=1;
      
      for(int i=1;i<=7;i++){
        cin >> a >> b;
        if(a+b>8){
          flag = 0;
          cout << i << endl;
          break;
        }
      }
      
      if(flag){
        cout << 0 << endl;
      }
      return 0;
    } 
    
  4. 谁拿了最多奖学金,http://noi.openjudge.cn/ch0109/04/
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    struct Student{
      char name[100];
      int average;
      int scroe;
      char leader;
      char west;
      int papers;
      int money;
    }s[105];
    
    int sum,total=0,p,maxx=0,n;
    
    int main(){
      cin >> n;
    
      for(int i=0;i<n;i++){
        scanf("%s %d %d %c %c %d",&s[i].name,&s[i].average,&s[i].scroe,&s[i].leader,&s[i].west,&s[i].papers);
        sum = 0;
        if(s[i].average>80 && s[i].papers>=1) sum = sum + 8000;
        if(s[i].average>85 && s[i].scroe>80) sum = sum + 4000;
        if(s[i].average>90) sum = sum + 2000;
        if(s[i].average>85 && s[i].west=='Y') sum = sum + 1000;
        if(s[i].scroe>80 && s[i].leader=='Y') sum = sum + 850;
        s[i].money = sum;
        if(sum > maxx){
          maxx=sum;
          p = i;
        }
        total = total + sum;
      }
    
      cout << s[p].name <<endl;
      cout << s[p].money << endl;
      cout << total << endl;
      
      return 0;
    }
    
  5. 最大值和最小值的差,http://noi.openjudge.cn/ch0109/05/
    #include <iostream>
    using namespace std;
    
    int main(){
      int n,a,minn=100000,maxx=-100000;
      cin >> n;
      for(int i=1;i<=n;i++){
        cin >> a;
        maxx = max(maxx,a);
        minn = min(minn,a);
      }
      cout << maxx - minn <<endl;	
      return 0;
    }
    
  6. 笨小猴,http://noi.openjudge.cn/ch0109/06/
    #include <iostream>
    #include <cmath>
    #include <cstring>
    using namespace std;
    
    bool isPrime(int num){
      if(num==0) return false;
      if(num==1) return false;
      if(num==2) return true;
      if(num==3) return true;
      if(num==4) return false;
    
      bool flag = true;
      
      for(int i=2;i<sqrt(num);i++)
        if(num%i==0){
          flag = false;
        }
      return flag;
    }
    
    int cnum[27],maxn=0,minn=10000000;
    char word[100];
    
    int main(){
      scanf("%s",word);	
      for(int i=0;i<strlen(word);i++){
        cnum[word[i]-'a']++;
      }
      for(int i=0;i<27;i++){
        if(cnum[i]>0){
          maxn=max(maxn,cnum[i]);
          minn=min(minn,cnum[i]);
        }
      }
      if(isPrime(maxn-minn)){
        cout << "Lucky Word" << endl;
        cout << maxn - minn << endl;
      }else{
        cout << "No Answer" << endl;
        cout << 0 << endl;	
      }
      
      return 0;
    }
    
  7. 不与最大数相同的数字之和,http://noi.openjudge.cn/ch0109/07/
    #include <iostream>
    using namespace std;
    
    int main(){
      int n,a[101],sum=0,maxx=-10000000;
      
      cin >> n;
      
      for(int i=0;i<n;i++){
        cin >> a[i];
        maxx = max(maxx,a[i]);
      }
      
      for(int i=0;i<n;i++){
        if(a[i]!=maxx)
          sum = sum + a[i];
      }
      
      cout << sum << endl;
      
      return 0;
    }
    
  8. 白细胞计数,http://noi.openjudge.cn/ch0109/08/
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main(){
      int n,mx,mn;
      double a[305],avg,sub,sum=0.0,maxx=0.0,minn=10000000000000.0;
    
      cin >> n;
      for(int i=0;i<n;i++){
        cin >> a[i];
        if(a[i]>maxx){
          maxx = a[i];
          mx = i;
        }
        if(a[i]<minn){
          minn = a[i];
          mn = i;
        }
        sum = sum + a[i];
      }
      avg = (sum-maxx-minn)/(n-2);
    
      maxx = 0.0;
      for(int i=0;i<n;i++){
        if(i!=mn && i!=mx){
          maxx = max(maxx,abs(avg-a[i]));
        }
      }
      printf("%.2f %.2f",avg,maxx);
      
      return 0;
    }
    
  9. 直方图,http://noi.openjudge.cn/ch0109/09/
    #include <iostream>
    using namespace std;
    
    int n,a[10005],d[10005],fmax=0;
    int main(){
      cin >> n;
    
      for(int i=0;i<n;i++){
        cin >> a[i];
        fmax = max(fmax,a[i]);
        d[a[i]]++;
      }
    
      for(int i=0;i<=fmax;i++)
        cout << d[i] << endl;
        
      return 0;
    }
    
  10. 找最大数序列,http://noi.openjudge.cn/ch0109/10/
    #include <iostream>
    using namespace std;
    
    int a[35][105],maxn=0,n;
    char ln[1000];
    
    void lnToNum(int i){
      int p=0,j=0,num=0;
      while(ln[p]!='\0'){
        if(ln[p]!=','){
          num = num * 10 + ln[p]-'0';
        }else{
          if(num>maxn) maxn=num;
          a[i][j++]=num;
          num = 0;
        }
        p++;
      }
      if(num>maxn) maxn=num;
      a[i][j++]=num;
      a[i][j]=-1;
    }
    
    int main(){
      
      cin >> n;
      for(int i=0;i<n;i++){
        scanf("%s",ln);
        lnToNum(i);
      }	
      cout << maxn << endl;
      
      bool first = true;	
      for(int i=0;i<n;i++){
        for(int j=0;a[i][j]>=0;j++)
          if(a[i][j]==maxn){
            if(first){
              cout << i+1;
              first = false;
            }else{
              cout << "," << i+1;
            }
            break;
          }
      }
      cout << endl;
          
      return 0;
    }
    
  11. 连续出现的字符,http://noi.openjudge.cn/ch0109/11/
    #include <iostream>
    using namespace std;
    int main(){
      bool flag = true;
      int k,tim=0;
      char str[10005],tmp='\0';
      cin >> k;
      scanf("%s",str);
      for(int i=0;str[i]!='\0';i++){
        if(str[i]!=tmp){
          tim = 1;
          tmp = str[i];
        }else{
          tim = tim + 1;
        }
        if(tim>=k){
          flag = false;
          cout << str[i] << endl;
          break;
        }
      }	
      
      if(flag)
        cout << "No" << endl;
        
      return 0;
    }
    
  12. 最长平台,http://noi.openjudge.cn/ch0109/12/
    #include <iostream>
    using namespace std;
    
    int main(){
      int n,a,tmp=-1,tim=0,max=1;
      cin >> n;
      for(int i=0;i<n;i++){
        scanf("%d",&a);
        if(a!=tmp){
          if(tim>max) max = tim;
          tim = 1;
          tmp = a;
        }else{
          tim = tim + 1;
        }
      } 
      
      if(tim>max) max = tim;
      
      cout << max << endl;
      
      return 0;
    }
    
  13. 整数去重,http://noi.openjudge.cn/ch0109/13/
    #include <iostream>
    using namespace std;
    int f[200];
    int main(){
      int n,a,first=1;
      cin >> n;
      for(int i=0;i<n;i++){
        scanf("%d",&a);
        if(!f[a]){
          if(first){
            cout << a;
            first = 0;
          }else{
            cout << " " << a;
          }
          f[a]=1;
        }
      }
      cout << endl;
      
      return 0;
    }
    
  14. 铺地毯,http://noi.openjudge.cn/ch0109/14/
    #include <iostream>
    using namespace std;
    
    int n,dt[10005][4],x,y,idx=-1;
    int main(){
      cin >> n;
      for(int i=0;i<n;i++){
        scanf("%d %d %d %d",&dt[i][0],&dt[i][1],&dt[i][2],&dt[i][3]);
      }
      cin >> x >> y;
      for(int i=0;i<n;i++){
        if(x-dt[i][0]>=0 && x-dt[i][0]<=dt[i][2] && y-dt[i][1]>=0 && y-dt[i][1]<=dt[i][3]){
          idx = i+1;
        }
      }	
      cout << idx << endl;
      
      return 0;
    }
    
  15. 接水问题,http://noi.openjudge.cn/ch0109/15/
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int n,m,p[105],a,maxt=0;
    
    int main(){
      cin >> n >> m;
      
      for(int i=0;i<m;i++)
        cin >> p[i];
      
      for(int i=0;i<n-m;i++){
        sort(p,p+m);
        cin >> a;
        p[0]=p[0]+a;
      }
      
      for(int i=0;i<m;i++)
        if(p[i]>maxt)
          maxt = p[i];
          
      cout << maxt << endl;
        
      return 0;
    }
    

参考网址

  1. http://noi.openjudge.cn