Problems:
- Here is the list of questions to practice STL algorithms:
- 1. UVa 00123 - Searching Quickly
- 2. UVa 00146 - ID Codes *
- 3. UVa 00400 - Unix ls
- 4. UVa 00450 - Little Black Book
- 5. UVa 00790 - Head Judge Headache
- 6. UVa 00855 - Lunch in Grid City
- 7. UVa 01209 - Wordfish
- UVa 10057 - A mid-summer night ...
- UVa 10107 - What is the Median? *
- UVa 10194 - Football a.k.a. Soccer
- UVa 10258 - Contest Scoreboard *
- UVa 10698 - Football Sort
- UVa 10880 - Colin and Ryan
- UVa 10905 - Children’s Game
- UVa 11039 - Building Designing
- UVa 11321 - Sort Sort and Sort
- UVa 11588 - Image Coding
- UVa 11777 - Automate the Grades
- UVa 11824 - A Minimum Land Price
- UVa 12541 - Birthdates
Here is the link to these questions: https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=625
Here are hints and my solutions to some of these:
Hint and Solutions
Uva 00146: ID Codes
Simple: You need to use the next_permutation. Here is my code that solves the problem:
#include <bits/stdc++.h> | |
using namespace std; | |
int main(int argc, char const *argv[]) { | |
string s; | |
while (getline(cin, s),s!="#") { | |
string t = s; | |
sort(t.begin(), t.end()); | |
next_permutation(s.begin(),s.end()); | |
if(t==s) | |
std::cout << "No Successor" << '\n'; | |
else | |
std::cout <<s<< '\n'; | |
} | |
return 0; | |
} |
Uva 10107: What is the Median?
Create an array of size 100000, store all elements into it, keeping track of the last filled location. Each time filling a location in it, sort using the sort function it and output the median as defined in the problem.
Here is the code that does it:
#include <bits/stdc++.h> | |
using namespace std; | |
int main(int argc, char const *argv[]) { | |
int arr[100003]; | |
int end=0; | |
int temp; | |
while (scanf("%d", &temp)==1) { | |
end; | |
arr[end] = temp; | |
sort(arr, arr+end+1); | |
int median; | |
if(end==0) | |
median = arr[0]; | |
else if(end%2==1&&end!=0) | |
median = (arr[end/2] + arr[end/2 +1])/2; | |
else | |
median = arr[end/2]; | |
end++; | |
printf("%d\n", median); | |
} | |
return 0; | |
} |
Uva 10905: Children's Game
You need a modified form of the sort function, create a compare function that does the job. Head here to learn it: https://rishabhjainiitbhu.blogspot.com/2019/08/custom-sorting-in-c.html
Here is the code that solves the problem:
#include <bits/stdc++.h> | |
using namespace std; | |
bool compare(const string &a,const string &b){ | |
if(a+b>b+a) | |
return true; | |
else | |
return false; | |
} | |
int main(int argc, char const *argv[]) { | |
int N; | |
while (scanf("%d", &N), N!=0) { | |
string arr[N]; | |
for (size_t i = 0; i < N; i++) { | |
cin>>arr[i]; | |
} | |
sort(arr, arr+N, compare); | |
for (size_t i = 0; i < N; i++) { | |
cout<<arr[i]; | |
} | |
cout<<'\n'; | |
} | |
return 0; | |
} | |
Uva 11321: Sort! Sort! and Sort!
Create a custom sorting function. Keep in mind the negative numbers.
Here is my solution to the problem:
#include <bits/stdc++.h> | |
using namespace std; | |
int M; | |
bool compare(int a, int b){ | |
int temp= a%M; | |
int temp2= b%M; | |
if(temp<temp2) | |
return true; | |
if(temp>temp2) | |
return false; | |
if(a%2==0&&b%2==1||(a%2==0&&b%2==-1)) | |
return false; | |
if(a%2==1&&b%2==0||(a%2==-1&&b%2==0)) | |
return true; | |
if((a%2==1||a%2==-1)&&a>b) | |
return true; | |
if((a%2==1||a%2==-1)&&a<b) | |
return false; | |
if(a%2==0&&a>b) | |
return false; | |
if(a%2==0&&a<b) | |
return true; | |
return false; | |
} | |
int main(int argc, char const *argv[]) { | |
int N; | |
while(scanf("%d %d", &N, &M), N||M) { | |
int arr[N]; | |
for (size_t i = 0; i < N; i++) { | |
scanf("%d", &arr[i]); | |
} | |
sort(arr, arr+N, compare); | |
printf("%d %d\n", N, M); | |
for (size_t i = 0; i < N; i++) { | |
printf("%d\n", arr[i]); | |
} | |
} | |
printf("%d %d\n", 0, 0); | |
return 0; | |
} |
Uva 11777: Automate the Grades
No hint(or even the code) should be needed. Still, here is my code:
#include <bits/stdc++.h> | |
using namespace std; | |
inline int average(int ct1, int ct2, int ct3){ | |
if(ct1<ct2)swap(ct1, ct2); | |
if(ct2<ct3)swap(ct2, ct3); | |
return (ct1+ct2)/2; | |
} | |
int main(int argc, char const *argv[]) { | |
int T; | |
scanf("%d\n", &T); | |
int i=1; | |
while(T--){ | |
int term1, term2, final, attendance, class_test1, class_test2, class_test3; | |
scanf("%d %d %d %d %d %d %d\n", &term1, &term2, &final, &attendance, &class_test1, &class_test2, &class_test3); | |
int total = term1+term2+final+attendance + average(class_test1, class_test2, class_test3); | |
printf("Case %d: ", i++); | |
if(total<60){ | |
printf("%c\n", 'F'); | |
continue; | |
} | |
if(total<70){ | |
printf("%c\n", 'D'); | |
continue; | |
} | |
if(total<80){ | |
printf("%c\n", 'C'); | |
continue; | |
} | |
if(total<90){ | |
printf("%c\n", 'B'); | |
continue; | |
} | |
printf("%c\n", 'A'); | |
} | |
return 0; | |
} |
No comments:
Post a Comment