+ 5
Codegolf(just for fun)
Let's see who can display how many times a value appears in a list using the least code[cpp only] the list should contain 10 values of your own choosing.
5 odpowiedzi
+ 4
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> v = {1,2,1,6,9,6,1,3,2,1};
int main() {
cout<<count(v.begin(),v.end(),1);
return 0;}
+ 4
Yup You are right @YellowBunny consider yourself #1
+ 3
@YellowBunny Well technically your code doesn't display how many times the value appears instead it displays the value each time it appears. Still pretty neat.
+ 2
/* This program outputs the amout of times a certain element is in a list in unary. */
#include <iostream>
int main(){
int x[]{1,2,3,4,1,2,3,1,2,1},i=0;
for(;i<10;i++)x[i]!=1?:std::cout<<1;
}
+ 1
@Marco Linde If it is looking for 2 instead it outputs 111. That is 3 in unary and thereby the amout of 2s. Your task did not say to output in decimal :)
I could also declare another integer, replace the cout command with an increment command and output the number at the end, but that would be one more line of code.