原文:
给定一个数字数组,任务是按升序打印这些数字,用逗号分隔,数字中有 1、2 和 3。如果没有包含数字 1、2 和 3 的数字,则打印-1。
示例:
input : numbers[] = {123, 1232, 456, 234, 32145}
output : 123, 1232, 32145
input : numbers[] = {9821, 627183, 12, 1234}
output : 1234, 627183
input : numbers[] = {12, 232, 456, 234}
output : -1
问于:高盛 t3】方法:首先从包含 1,2 & 3 的数组中找出所有的数字,然后按照 1,2,3 排序,然后打印出来。
卡片打印处理机(card print processor 的缩写)
// cpp program to print all number containing
// 1, 2 and 3 in any order.
#include
using namespace std;
// convert the number to string and find
// if it contains 1, 2 & 3.
bool findcontainsonetwothree(int number)
{
string str = to_string(number);
int countones = 0, counttwo = 0, countthree = 0;
for(int i = 0; i < str.length(); i ) {
if(str[i] == '1') countones ;
else if(str[i] == '2') counttwo ;
else if(str[i] == '3') countthree ;
}
return (countones && counttwo && countthree);
}
// prints all the number containing 1, 2, 3
string printnumbers(int numbers[], int n)
{
vector onetwothree;
for (int i = 0; i < n; i )
{
// check if the number contains 1,
// 2 & 3 in any order
if (findcontainsonetwothree(numbers[i]))
onetwothree.push_back(numbers[i]);
}
// sort all the numbers
sort(onetwothree.begin(), onetwothree.end());
string result = "";
for(auto number: onetwothree)
{
int value = number;
if (result.length() > 0)
result = ", ";
result = to_string(value);
}
return (result.length() > 0) ? result : "-1";
}
// driver code
int main() {
int numbers[] = { 123, 1232, 456, 234, 32145 };
int n = sizeof(numbers)/sizeof(numbers[0]);
string result = printnumbers(numbers, n);
cout << result;
return 0;
}
// this code is contributed
// by sirjan13
java 语言(一种计算机语言,尤用于创建网站)
// java program to print all number containing
// 1, 2 and 3 in any order.
import java.io.filenotfoundexception;
import java.util.arraylist;
import java.util.collections;
import java.util.iterator;
class gfg {
// prints all the number containing 1, 2, 3
// in any order
private static string printnumbers(int[] numbers)
{
arraylist array = new arraylist<>();
for (int number : numbers) {
// check if the number contains 1,
// 2 & 3 in any order
if (findcontainsonetwothree(number))
array.add(number);
}
// sort all the numbers
collections.sort(array);
stringbuffer strbuf = new stringbuffer();
iterator it = array.iterator();
while (it.hasnext()) {
int value = (int)it.next();
if (strbuf.length() > 0)
strbuf.append(", ");
strbuf.append(integer.tostring(value));
}
return (strbuf.length() > 0) ?
strbuf.tostring() : "-1";
}
// convert the number to string and find
// if it contains 1, 2 & 3.
private static boolean findcontainsonetwothree(
int number)
{
string str = integer.tostring(number);
return (str.contains("1") && str.contains("2") &&
str.contains("3"));
}
public static void main(string[] args)
{
int[] numbers = { 123, 1232, 456, 234, 32145 };
system.out.println(printnumbers(numbers));
}
}
计算机编程语言
# python program for printing
# all numbers containing 1,2 and 3
def printnumbers(numbers):
# convert all numbers
# to strings
numbers = map(str, numbers)
result = []
for num in numbers:
# check if each number
# in the list has 1,2 and 3
if ('1' in num and
'2' in num and
'3' in num):
result.append(num)
# if there are no
# valid numbers
if not result:
result = ['-1']
return sorted(result);
# driver code
numbers = [123, 1232, 456,
234, 32145]
result = printnumbers(numbers)
print ', '.join(num for num in result)
# this code is contributed
# by ishitatripathi
c
// c# program to print all number
// containing 1, 2 and 3 in any order.
using system;
using system.collections.generic;
using system.text;
class gfg
{
// prints all the number
// containing 1, 2, 3
// in any order
private static string printnumbers(int[] numbers)
{
list array = new list();
foreach (int number in numbers)
{
// check if the number contains 1,
// 2 & 3 in any order
if (findcontainsonetwothree(number))
{
array.add(number);
}
}
// sort all the numbers
array.sort();
stringbuilder strbuf = new stringbuilder();
system.collections.ienumerator it =
array.getenumerator();
while (it.movenext())
{
int value = (int)it.current;
if (strbuf.length > 0)
{
strbuf.append(", ");
}
strbuf.append(convert.tostring(value));
}
return (strbuf.length > 0) ? strbuf.tostring() : "-1";
}
// convert the number
// to string and find
// if it contains 1, 2 & 3.
private static bool findcontainsonetwothree(int number)
{
string str = convert.tostring(number);
return (str.contains("1") &&
str.contains("2") && str.contains("3"));
}
// driver code
public static void main(string[] args)
{
int[] numbers = new int[] {123, 1232,
456, 234, 32145};
console.writeline(printnumbers(numbers));
}
}
// this code is contributed by shrikant13
output:
123, 1232, 32145
时间复杂度:上述方法的时间复杂度为 o(nlg(n)) 。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处