1,输入若干数,求对他们进行从大到小的排序,输出排名。
#include <bits/stdc++.h>
using namespace std;
#define n 10
int main(){
int a[n];
queue<int>q;
for(int i=0;i<n;i++){
cin>>a[i];
q.push(a[i]);
}
for(int i=0;i<n;i++)
for(int j=0;j<n-1;j++)
if(a[j]<a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
for(int i=0;i<n;i++)
{
int temp=q.front();
q.pop();
for(int j=n-1;j>=0;j--)
if(a[j]==temp)cout<<j+1<<endl;
}
return 0;
}
2,加减算式
样例输入
99+88-77
样例输出
110
#include <bits/stdc++.h>
using namespace std;
int transform(char *p){
int temp=0,i=0,res=0;
while(*p!='\0'){
temp+=(*p-'0')*pow(10,i);
p++;
i++;
}
while(i--){
res+=(temp%10)*pow(10,i);
temp/=10;
}
return res;
}
void solve(char a[])
{
int ans=0;
queue<char>q1;
queue<int>q2;
char *p1=a,*p2=a;
while(*p1!='\0'){
p1++;
if(*p1=='+'||*p1=='-'){
char temp=*p1;
q1.push(temp);
*p1='\0';
q2.push(transform(p2));
*p1=temp;
while(p2!=p1)p2++;
p2++;
}
if(*p1=='\0')q2.push(transform(p2));
}
ans+=q2.front();q2.pop();
do{
if(q1.front()=='+')ans+=q2.front();
else if(q1.front()=='-')ans-=q2.front();
q1.pop();
q2.pop();
}while(q2.size()>0);
cout<<ans;
}
int main(){
char a[10000];
cin>>a;
solve(a);
return 0;
}
3,找出最早出现的最短单词
#include <bits/stdc++.h>
using namespace std;
int main(){
char a[200];
cin.get(a,200);
queue<string>q;
int mi=200;
char *p1=a,*p2=a;
while(*p1++==' '&&p2++);
while(*p1!='\0'){
p1++;
if(*p1==' '){
*p1='\0';
q.push(p2);
*p1=' ';
mi=min(mi,(int)(p1-p2));
while(*++p1==' ');
while(p2!=p1)p2++;
}
if(*p1=='.'){
*p1='\0';
if(*(p1-1)==' '){
while(*p2++==' ');
if(p1-p2>0)mi=min(mi,(int)(p1-p2));
}
else{
mi=min(mi,(int)(p1-p2));
}
if(p1-p2>0)q.push(p2);
}
}
/*while(q.size()){
cout<<q.front()<<endl;
q.pop();
}*/
while(q.size()){
if(q.front().length()==mi){
cout<<q.front();
break;
}
q.pop();
}
return 0;
}
未完待续。。
持续施工!收藏点起来!