阴阳神万物主 发表于 2018-1-5 16:15:30

求帮助我简化代码并指导一下我

本帖最后由 阴阳神万物主 于 2020-1-3 15:30 编辑

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
int main()
{
        freopen("filenamein.txt","r",stdin);
        freopen("filenameout.txt","w",stdout);
        freopen("filenamerr.txt","w",stderr);
    int N; // Number of elements which make up the association table.
    cin >> N; cin.ignore();cerr<<N<<endl;
    int Q; // Number Q of file names to be analyzed.
    cin >> Q; cin.ignore();cerr<<Q<<endl;
    string EXT; // file extension
    string MT;// MIME type.
    for (int i = 0; i < N; i++) {
      cin >> EXT >> MT; cin.ignore();
      for(int b=0;b<EXT.length();b++)
      {
            if(isupper(EXT))EXT+=32;
            }
      cerr<<"EXT["<<i+1<<"]:"<<EXT<<" "<<"MT["<<i+1<<"]:"<<MT<<endl;
    }
    for (int i = 0; i < Q; i++) {
      string FNAME; // One file name per line.
      getline(cin, FNAME);cerr<<"FNAME"<<i+1<<":"<<FNAME<<endl;
      cerr<<"rfind:"<<FNAME.rfind('.')<<endl;
      if(FNAME.rfind('.') <FNAME.length()){
            int a=FNAME.rfind('.') + 1;
            char c="";int b=0;
            for (;a<FNAME.length();b++)
            {
                if(isupper(FNAME))c=FNAME + 32;
                else c=FNAME;
                a++;
                }
            cerr<<"c:"<<c<<endl;
            int v=0;
            if(c=="")cout<<"UNKNOWN"<<endl;
            else
            {   for(;EXT!=c && v<N;v++);
                  if(v<N)cout<<MT<<endl;
                  else cout<<"UNKNOWN"<<endl;
                }
      }
      else cout<<"UNKNOWN"<<endl;
    }
    fclose(stdin);fclose(stdout);fclose(stderr);
        return 0;   
}
为解决问题:https://www.codingame.com/ide/puzzle/mime-type
已经解决,把输入方式改为 scanf 即可
页: [1]
查看完整版本: 求帮助我简化代码并指导一下我