Re: 482 Permutation Arrays
Posted: Thu Dec 16, 2010 9:31 pm
the "sorted" array should be of 'int' type
Code: Select all
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
// freopen("input", "r+", stdin);
int testCases;
while (scanf("%d", &testCases)!=EOF)
{
getchar();
for (int a = 0; a<testCases; a++)
{
if (a>0)
{
getchar();
// printf("\n");
}
getchar();
int index[300000];
double number[300000];
char ch[300000];
gets(ch);
char temp[20];
int ipos = 0;
for (int i = 0, t = 0; i<=strlen(ch); i++)
{
if (ch[i]>=48 && ch[i]<=57)
{
temp[t] = ch[i];
t++;
}
else
{
temp[t] = '\0';
t = 0;
index[ipos] = atoi(temp);
ipos++;
}
}
index[ipos] = NULL;
for (int i = 0; i<ipos; i++)
{
scanf("%lf", &number[i]);
}
for (int i = 0; i<ipos; i++)
{
for (int j = 0; j<ipos-i-1; j++)
{
int k = 0;
float f;
if (index[j] > index[j+1])
{
k = index[j];
index[j] = index[j+1];
index[j+1] = k;
f = number[j];
number[j] = number[j+1];
number[j+1] = f;
}
}
}
for (int i = 0; i<ipos; i++)
{
cout << number[i] << endl;
}
cout << endl;
}
}
return 0;
}
Code: Select all
int main() {
int maxSize = 100000;
int input_arr[maxSize];
string output[maxSize];
int iterations;
int x=0;
string mystr;
char * pch;
bool y = false;
cin >> iterations;
getline (cin,mystr);
while (iterations > 0){
y ? getline (cin,mystr) : y = true;
getline (cin,mystr);
getline (cin,mystr);
pch = strtok (&mystr[0]," ");
while (pch != NULL){
input_arr[x++] = atoi(pch) - 1;
pch = strtok (NULL, " ");
}
for (int i=0; i<x; i++)
cin >> output[input_arr[i]];
for (int i=0; i<=x; i++){
cout << output[i];
if (i != x) cout << endl;
}
if (iterations != 1) cout << endl;
x = 0;
iterations--;
}
return 0;
}
Code: Select all
/*
Martuza, Islamic University
*/
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
int main()
{
long tc,i,s=0;
scanf("%ld\n",&tc);
while(tc--)
{
if(s>0)
printf("\n");
s=1;
char a[100000]={0},tmp[15]={0},d[100000]={0},e[10000][15]={0},temp1[15]={0};
long l,k=0,j=0,w,l1,t;
int b[100000]={0};
double c[100000]={0},temp;
gets(a);
l=strlen(a);
for(i=0; i<l; i++)
{
if(isdigit(a[i]))
tmp[k++]=a[i];
if(a[i]==' ' || i==l-1)
{
b[j++]=atoi(tmp);
for(w=0; w<15; w++)
tmp[w]=0;
k=0;
}
}
gets(d);
l1=strlen(d);
j=0; k=0; t=0;
for(i=0; i<l1; i++)
{
if(isdigit(d[i]) || d[i]=='.' || d[i]=='-')
e[t][k++]=d[i];
if(d[i]==' ' || i==l1-1)
{
c[j++]=atof(e[t]);
k=0;
t++;
}
}
for(i=0; i<t; i++)
for(j=0; j<t-1; j++)
if(c[j]>c[j+1])
{
temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
strcpy(temp1,e[j]);
strcpy(e[j],e[j+1]);
strcpy(e[j+1],temp1);
}
for(w=0; w<t; w++)
printf("%s\n",e[b[w]-1]);
}
return 0;
}
Code: Select all
#include<iostream>
#include <cstring>
#include<cstdio>
using namespace std;
char line [1000000];
int list1[50010];int list2[50010];
int cont;
void saca_int(){
int aux=0;cont=0;
for(int i=0;(size_t)(i)<=strlen(line);i++){
if(line[i]>='0'&&line[i]<='9'){
aux=aux*10+(line[i]-'0');
}
else{
if(aux!=0){
list1[aux-1]=cont;cont++;
aux=0;
}
}
}
}
void busca_float(){
int conta=0;bool passa=0;
for(int i=0;(size_t)(i)<=strlen(line);i++){
if((line[i]>='0'&&line[i]<='9')||line[i]=='-'||line[i]=='.'){
if(!passa){list2[conta]=i;passa=1;conta++;}
}
else{
if(passa){passa=0;}
}
}
}
void saca_float(int x){
for(int i=x;;i++){
if((line[i]>='0'&&line[i]<='9')||line[i]=='-'||line[i]=='.'){
cout<<line[i];
}
else{
return;
}
}
}
int main(){
int N=0;float aux;scanf("%d\n",&N);
if(N==0){return(0);}
for(int ii=0;ii<N;ii++){
if(ii!=0){cout<<"\n";scanf("\n");}
gets(line);saca_int();
memset(line,'\0',strlen(line));
gets(line);busca_float();
for(int j=0;j<cont;j++){
saca_float(list2[list1[j]]);cout<<"\n";
}
memset(line,'\0',strlen(line));
}
}
Code: Select all
removed after AC
Code: Select all
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
struct combo_number
{
string number_as_string;
double number;
};
int main()
{
unsigned int n, i, u, k;
scanf("%d", &n);
scanf("\n");
vector<int> order;
vector<double> numbers;
vector<string> numbers_as_strings;
vector<combo_number> combo_numbers;
unsigned int order_number;
double number;
string number_as_string;
char input_character;
for (i = 0; i < n; i++)
{
order.clear();
numbers.clear();
numbers_as_strings.clear();
number_as_string.clear();
combo_numbers.clear();
while (true)
{
scanf("%d", &order_number);
order.push_back(order_number);
scanf("%c", &input_character);
if (input_character == ' ')
continue;
else
break;
}
while (true)
{
cin >> number_as_string;
combo_number combo;
combo.number_as_string = number_as_string;
combo.number = atof(number_as_string.c_str());
numbers_as_strings.push_back(number_as_string);
numbers.push_back(combo.number);
combo_numbers.push_back(combo);
scanf("%c", &input_character);
if (input_character == ' ')
continue;
else
break;
}
sort(numbers.begin(), numbers.end());
for (u = 0; u < (int) order.size(); u++)
{
//printf("%lf\n", numbers[order[u] - 1]);
for (k = 0; k < (int) combo_numbers.size(); k++)
{
if (numbers[order[u] - 1] == combo_numbers[k].number)
cout << combo_numbers[k].number_as_string << endl;
}
//cout << numbers_as_strings[order[u] - 1] << endl;
}
if (i != n - 1)
printf("\n");
scanf("\n");
}
return 0;
}
Code: Select all
2
3 1 2
32.0 54.7 -2
1 3 2 4
.004 +5.0e0 0.000007 3
Code: Select all
54.7
-2
32.0
.004
0.000007
+5.0e0
3
Code: Select all
54.7
-2
32.0
0.000007
3
.004
+5.0e0
Code: Select all
removed after AC