80 条题解
-
2
dueen LV 7 @ 4 年前
充分利用各种输入形式
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
struct node{
int id;
string x;
}arr[1001];
bool cmp(node a, node b){
return a.id < b.id;
}
int main(){
int n;
int i = 0;
while(1){
scanf("%d",&arr[i].id);
i++;
if(getchar() == '\n')
break;
}
for(int j = 0; j < i; j++){
cin >> arr[j].x;
}
sort(arr, arr+i, cmp);
for(int j = 0; j < i; j++)
cout << arr[j].x << endl;
return 0;
} -
25 年前@
-
11 年前@
-
13 年前@
这道题不难,重点就是输入,处理好输入就基本上没有问题了
代码 : -
14 年前@
-
14 年前@
//
// main.cpp
// 123
//
// Created by Siyuan on 2020/8/3.
// Copyright © 2020 Siyuan. All rights reserved.
//#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
struct node{
int id;
string x;
}arr[1001];
bool cmp(node a, node b){
return a.id < b.id;
}
int main(){
int n;
int i = 0;
while(1){
scanf("%d",&arr[i].id);
i++;
if(getchar() == '\n')
break;
}
for(int j = 0; j < i; j++){
cin >> arr[j].x;
}
sort(arr, arr+i, cmp);
for(int j = 0; j < i; j++)
cout << arr[j].x << endl;
return 0;
} -
16 年前@
这题也太水了吧
python -
16 年前@
很简单的一道题,主要是在输入处理,因为N的数量未知,所以得使用判断回车符来判断N输入结束,因此有两种办法
(1)使用gets函数只有遇到回车符结束的特点来获取字符串,然后再用sscanf来格式化输入,那么就需要有一个保存第一行的空间,粗略计算一下,1<=N<=1000,那么就需要1000(数量) × 3(假设每个的位数)+ 若干空格(本题没有提到两个整数之间是有一个空格,那有可能有很长的空格),因此所开的字符数组的数量无法确定,只能使用变长字符数组(C++ string),那么只能使用C++的输入了;(2)使用scanf("%c")来读入,判断回车符即结束,因为输入一定是整数,因此只要简单累乘计算一下即可;
对比两种,最终选择第二种,理由有:
(1)利用第二种不需要新增空间保存,节约了空间;
(2)如果要使用第一种,那么得使用C++ string,这样得使用C++的cin,C++的输入速度感人,要是有刷POJ就知道一般输入量巨大的用cin会超时,所以建议不要用cin;或者开一个自认为不会越界的字符数组,但是这样有可能出现RE的不确定因素。
-
17 年前@
-
17 年前@
1.首先不知道N的大小,所以可以使用字符串流;2.因为要保证小数要按照原格式输出,所以不能用浮点数,可以使用字符串保存小数
-
06 年前@
-
06 年前@
#include<iostream>
#include<sstream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1000 + 5;
struct Letter
{
int x; string s;
bool operator < (const Letter& rhs) const
{
return x < rhs.x;
}
}letter[maxn];
int n = 1; string str;
int main()
{
getline (cin, str);
stringstream ss(str);
while(ss >> letter[n++].x);
n--;n--;
for(int i = 1; i <= n; i++)
cin >> letter[i].s;
sort(letter + 1, letter + n + 1);
for(int i = 1; i <= n; i++)
cout << letter[i].s << endl;
return 0;
} -
07 年前@
排序 (✿◡‿◡)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<string>
#include<ctime>
#define ll long long
#define DB double
#define inf 987654321
#define mod 1000007
using namespace std;
inline int read()
{
int x=0,w=1;char ch=0;
while(!isdigit(ch)){if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*w;
}
int buf[50];
void write(int x)
{
if(x<0) putchar('-'),x=-x;
buf[0]=0;
while(x) buf[++buf[0]]=x%10,x/=10;
if(buf[0]==0) buf[0]=1,buf[1]=0;
while(buf[0]) putchar('0'+buf[buf[0]--]);
}
int n;
struct po{
int id;
string s;
}a[1002];
bool cmp(po x,po y)
{
return x.id<y.id;
}
int main()
{
n=read();
a[1].id=n;
for(int i=2;i<=n;i++)
{
int x;x=read();
if(x>n) n=x;
a[i].id=x;
}
for(int i=1;i<=n;i++)
cin>>a[i].s;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
cout<<a[i].s<<endl;
return 0;
} -
07 年前@
-
07 年前@
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#define maxa 1000+10
#define FOR(i,x,y) for(i=x;i<=y;++i)
using namespace std;
struct node
{
string s;
int num;
}e[maxa];
bool comp(node p,node q)
{
return p.num<q.num;
}
int main()
{
char ch;
int i =0;
do{
scanf("%d",&e[i++].num);
ch = getchar();
}while(ch!='\n');
int j;
for(j=0;j<i;++j)
cin>>e[j].s;
sort(e,e+i,comp);
FOR(j,0,i-1)
cout<<e[j].s<<endl;
} -
08 年前@
-
08 年前@
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str = input.nextLine();
String[] s1 = str.split(" ");
str = input.nextLine();
String[] s2 = str.split(" ");
int n=s1.length;
int a[] = new int[n];
for(int i=0;i<n;i++)
{
a[i] = Integer.valueOf(s1[i]);
}
boolean[] flag = new boolean[n];
for(int j=0;j<n;j++)
{
int min=10000000;
int p=0;
for(int i=0;i<n;i++)
{
if(!flag[i])
{
if(a[i]<min)
{
p=i;
min=a[i];
}
}
}
flag[p]=true;
System.out.println(s2[p]);
}
}
} -
08 年前@
算短了吧。。
-
08 年前@
-
08 年前@
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct num{
int x;
char y[280];
}a[1050];
int cmp(struct num a,struct num b)
{
return a.x<b.x;
}
int main(){
char ch[4000];
char str[256000];
// freopen("lyy.txt","r",stdin);
// freopen("out.txt","w",stdout);
gets(ch);
gets(str);
int p=0;int q=0;
while(p<strlen(ch)){
int num=0;
while(ch[p]!=' '&&p<strlen(ch)){
num=10*num+ch[p]-'0';
p++;
}
p++;
a[q].x =num;
q++;}
int m=0;int n=0;
while(m<strlen(str)){
char s[300]={0};
int j=0;
while(str[m]!=' '&&m<strlen(str)){
s[j++]=str[m++];
}
m++;
strcpy(a[n].y ,s);
n++;
}
sort(a,a+n,cmp);
for(int i=0;i<q;i++){
cout<<a[i].y<<endl;
}
return 0;
}