2013年北理复试上机题

1.求两个数的最大公约数
例:输入:24 18
输出:6

#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
   while(b>0){
    int c=a%b;
      a=b;
      b=c;
   }

return a;

}
int main(){

int a,b;
cin>>a>>b;
if(a<b) swap(a,b);
cout<<gcd(a,b)<<endl;

return 0;
}

2.输入一组英文单词,按字典顺序(不区分大小写)排序输出
例:输入:Information Info Inform info Suite suite suit
输出:Info info Inform Information suit Suite suite

#include<bits/stdc++.h>
using namespace std;
struct node{
  string str;
  int f;
};
vector<node>v;
string s;
void join(int i,int j){
  node a;
  a.str=s.substr(i,j-i+1);
  if(a.str[0]<='Z'){
    a.f=1; a.str[0]=a.str[0]+('a'-'A');
  }else a.f=0;
 // cout<<"str="<<a.str<<endl;
v.push_back(a);
}
int cmp(node a,node b){
    if(a.str==b.str)
        return a.f>b.f;
   return a.str<b.str;
}
int main(){
  string s1,s2;
  getline(cin,s);
  int i,j,l,k=0,cnt;
  l=s.length();
  for(i=0;i<=l;i++)
  {
      if(s[i]==' '||i==l)
      {
         join(k,i-1);k=i+1;
      }

  }
 sort(v.begin(),v.end(),cmp);
 for(i=0;i<v.size();i++)
 {
     if(v[i].f==1) v[i].str[0]=v[i].str[0]-('a'-'A');
     cout<<v[i].str<<" ";
 }
 cout<<endl;
 return 0;
}
发布了1 篇原创文章 · 获赞 0 · 访问量 38

猜你喜欢

转载自blog.csdn.net/qq_38375152/article/details/104400366