Feb 20, 2020 at 8:55am UTC
Sorry I know Java more than c++ and I did this challenge given to me in Java but its suppose to be in c++.
If there is anyone that is experienced in both Java and c++.
Please help me change this code to c++
import java.util.*;
import java.util.Set;
import java.util.HashSet;
public class username {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int numberOfNames = (int)Double.parseDouble(input);
String arr[] = new String[numberOfNames];
Set <String> unique = new HashSet<>();
//add the names to the major array for the database
for (int i =0; i<numberOfNames; i++) {
String names = scanner.next();
arr[i] = names;
}
//unique
for (int j = 0; j< arr.length;j++) {
if (!unique.contains(arr[j])) {
unique.add(arr[j]);
}
}
for (int i = 0; i<arr.length; i++) {
int counter= 1;
for (int j=i+1; j<arr.length; j++) {
String no = (arr[i]);
String no2 = (arr[j]);
if (no.equals(no2)) {
counter +=1;
String counterString = Integer.toString(counter);
arr[j] += counterString;
}
}
}
int count = 0;
for (String data : unique) {
count +=1;
}
System.out.println(count);
for (String namesg:arr){
System.out.println(namesg);
}
}
}
Feb 20, 2020 at 11:35am UTC
You must format your code. Some forums reject a post with unformatted code.
This is the Java stuff.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
import java.util.*;
import java.util.Set;
import java.util.HashSet;
public class username {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int numberOfNames = (int )Double.parseDouble(input);
String arr[] = new String[numberOfNames];
Set <String> unique = new HashSet<>();
//add the names to the major array for the database
for (int i =0; i<numberOfNames; i++) {
String names = scanner.next();
arr[i] = names;
}
//unique
for (int j = 0; j< arr.length;j++) {
if (!unique.contains(arr[j])) {
unique.add(arr[j]);
}
}
for (int i = 0; i<arr.length; i++) {
int counter= 1;
for (int j=i+1; j<arr.length; j++) {
String no = (arr[i]);
String no2 = (arr[j]);
if (no.equals(no2)) {
counter +=1;
String counterString = Integer.toString(counter);
arr[j] += counterString;
}
}
}
int count = 0;
for (String data : unique) {
count +=1;
}
System.out.println(count);
for (String namesg:arr){
System.out.println(namesg);
}
}
}
This is the C++ stuff.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <string>
#include <set>
#include <vector>
#include <functional>
#include <iostream>
int main() {
std::string line;
std::getline(std::cin, line);
auto numberOfNames = atoi(line.c_str());
std::vector<std::string> names; // your code calls this arr
names.reserve(numberOfNames);
for (auto i = 0; i != numberOfNames && std::getline(std::cin, line); ++i)
names.emplace_back(std::move(line));
std::set<std::size_t> unique;
for (const auto & name : names)
unique.insert(std::hash<std::string>{}(name));
for (auto i = 0; i < numberOfNames; ++i) {
std::size_t counter{1};
for (auto j{i + 1}; j < numberOfNames; ++j) {
const auto & str_i = arr[i];
const auto & str_j = arr[j];
if (str_i == str_j)
arr[j] += std::to_string(++counter);
}
}
// you can do the printing yourself
}
Last edited on Feb 20, 2020 at 11:39am UTC
Feb 20, 2020 at 12:34pm UTC
I don't I I'm right but is this correct or
1 2 3 4 5 6
int count = 0;
for (int i=0; i < unique; I++) {
count ++;
}
Cout << count;
Though here I can't
1 2
for (String namesg:arr){
System.out.println(namesg);
Last edited on Feb 20, 2020 at 12:34pm UTC
Feb 20, 2020 at 2:04pm UTC
The first part is just unique.size().
Surely you must be able to print the values in a container.
Feb 20, 2020 at 3:34pm UTC
Surely I can't bro if I would I couldn't ask.please
Feb 20, 2020 at 4:41pm UTC
almost!
in c++ case matters. Cout is not correct. its cout.
Feb 20, 2020 at 5:27pm UTC
Not sure why you've included using namespace std
, and ignored the indentation and blank line convention.
Last edited on Feb 20, 2020 at 5:28pm UTC
Feb 20, 2020 at 5:34pm UTC
it works the way you said you wanted it to work for me when I run it on my machine.
you occasionally see this kind of thing if you somehow scramble dos and unix end of lines. both dos eol characters make an eol in the terminal, but only one is seen as eol by the code, if you manage to scramble them... are you working remotely from win to a unix box?
Last edited on Feb 20, 2020 at 5:35pm UTC
Feb 20, 2020 at 9:12pm UTC
Is it trying to create an initializer list on the "auto j{i+1};" call for anyone else?
Works correctly once I change the auto to an int. (this is a bad place to use auto, imo.)
Edit: Weird, it seems to only be cpp.sh that doesn't it.
Last edited on Feb 20, 2020 at 9:14pm UTC
Feb 20, 2020 at 9:41pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
#include <sstream>
#include <string>
#include <map>
using namespace std;
int main()
{
// istream &in = cin;
istringstream in( "6 \n"
"Alice\n"
"Alice\n"
"Bob \n"
"John \n"
"Bob \n"
"Alice\n" );
string name, output;
int N;
map<string,int > M;
in >> N;
for ( int i = 0; i < N; i++ )
{
in >> name;
M[name]++;
output += name + ( M[name] > 1 ? to_string( M[name] ) : "" ) + '\n' ;
}
cout << output;
}
Alice
Alice2
Bob
John
Bob2
Alice3
Last edited on Feb 20, 2020 at 9:42pm UTC