Números amigos
Dois números são amigos se a sua soma der origem a um número cujo algarismo das unidades seja 0. Ou seja, que possa ser expresso como dezenas sem casas decimais. Por exemplo, o 26 e o 4 são amigos, pois a sua soma dá 30. O 26 e o 5 já não.
Objectivo
O que se pretende é que, dado um conjunto de números (não necessariamente diferentes), obter todos os pares não ordenados e diferentes de números amigos existentes nesse conjunto.
Soluções
C
#include <stdio.h>
#define MAX 5000
int num, counter=0, i, j, nums[MAX];
int seen[MAX][MAX];
int main()
{
scanf("%d", &num);
for(i=0; i<num; i++)
scanf("%d", &nums[i]);
for(i=0; i<num; i++)
for(j=i; j<num; j++)
if((nums[i]+nums[j])%10==0 && !seen[nums[i]][nums[j]])
{
counter++;
seen[nums[i]][nums[j]]=1;
seen[nums[j]][nums[i]]=1;
}
printf("%d", counter);
return 0;
}
Solução por pcladeira.
C
int[] nums = new int[7];
nums[0] = 6;
nums[1] = 1;
nums[2] = 9;
nums[3] = 2;
nums[4] = 8;
nums[5] = 4;
nums[6] = 22;
int quantd = 0;
int i = 0;
int j = 0;
for (i = 0; i <= 6; i++)
{
for (j = 0; j <= 6; j++)
{
if ((nums[i] + nums[j]) % 10 == 0)
{
quantd = quantd + 1;
listBox1.Items.Add(nums[i] + "," + nums[j]);
}
}
}
Solução por gooden.
VB.net
Dim nums(6)
nums(0) = 6
nums(1) = 1
nums(2) = 9
nums(3) = 2
nums(4) = 8
nums(5) = 4
nums(6) = 22
Dim quantd = 0
Dim i As Integer = 0
Dim j As Integer = 0
For i = 0 To 6
For j = 0 To 6
If (nums(i) + nums(j)) Mod 10 = 0 Then
quantd = quantd + 1
ListBox1.Items.Add(nums(i) & "," & nums(j))
End If
Next j
Next i
Solução por gooden.
java
public class Main {
private static String Inverter(String a){
char[] f = a.toCharArray();
String i = new String("");
int j = f.length - 1;
for(int h = 0 ; h < f.length; h++)
i+=f[j--];
return i;
}
private static String numerosAmigos(int n,int[] numeros){
String txt = new String("");
int total = 0;
for(int i = 0; i < n - 1; i ++ )
for(int f = i + 1; f < n ; f++)
if((numeros[i] + numeros[f]) % 10 == 0){
txt += ( numeros[i] + "," + numeros[f] + " ");
total++;
}
String[] pares = txt.split(" ");
for(int i = 0; i < pares.length - 1; i ++ )
for(int f = i + 1; f < pares.length; f++){
if(pares[i].equals(pares[f]) || pares[i].equals(Inverter(pares[f]))){
pares[i] = "f";
total--;
}
}
txt = "";
for(int i = 0; i < pares.length; i++){
if(!pares[i].equals("f"))
txt += "(" + pares[i] + "), ";
}
return "" + total + "nn" + txt.substring(0,txt.length()-2);
}
public static void main(String[] args) {
int [] n = {1,1,1,1};
int max = 4;
System.out.print(numerosAmigos(max, n));
}
}
Solução por sag.
C++
#include <iostream>
#include <fstream>
using namespace std;
class lista {
class par {
public:
int _a, _b;
par *next;
par(int a, int b): _a(a), _b(b), next(NULL) {
}
};
par *first, *last;
public:
lista(): first(NULL), last(NULL) {}
void add(int a, int b) {
for (par *p=first; p!=NULL; p=p->next)
if (( (p->_a == a) && (p->_b == b) ) || ( (p->_a == b) && (p->_b == a) ))
return;
par *p = new par(a,b);
if (first == NULL) first = p;
if (last != NULL) last->next = p;
last = p;
}
int contagem() {
int i=0;
for (par *p=first; p!=NULL; ++i, p=p->next);
return i;
}
};
int main() {
ifstream nfile("amigos.txt");
if (!nfile) {
cout << "amigos.txt nao encontrado." << endl;
return 1;
}
int total_numeros;
nfile >> total_numeros;
int *tabnumeros = new int[total_numeros];
for (int i = 0; i<total_numeros; ++i)
nfile >> tabnumeros[i];
lista l;
for (int i = 0; i<total_numeros-1; ++i) {
for (int j = i+1; j<total_numeros; ++j) {
if ((tabnumeros[i] + tabnumeros[j]) % 10 == 0) l.add(tabnumeros[i], tabnumeros[j]);
}
}
cout << "Total de conjuntos de numeros amigos: " << l.contagem() << endl;
return 0;
}
Solução por warrior.
C++ (usando a STL)
#include <iostream>
#include <fstream>
#include <utility>
#include <list>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
ifstream nfile("amigos.txt");
if (!nfile) {
cout << "amigos.txt nao encontrado." << endl;
return 1;
}
int total_numeros;
nfile >> total_numeros;
vector<int> tabnumeros;
{
int j;
for (int i = 0; i<total_numeros; ++i) {
nfile >> j;
tabnumeros.push_back(j);
}
}
list<pair<int, int>> l;
for (vector<int>::iterator i = tabnumeros.begin(); i!=tabnumeros.end()-1; ++i) {
for (vector<int>::iterator j = i+1; j!=tabnumeros.end(); ++j) {
if ((*i + *j) % 10 == 0)
if (find(l.begin(), l.end(), make_pair(*i, *j) ) == l.end() && find(l.begin(), l.end(), make_pair(*j, *i) ) == l.end())
l.push_back(make_pair(*i, *j));
}
}
cout << "Total de conjuntos de numeros amigos: " << l.size() << endl;
return 0;
}
Solução por warrior.
Python
def par(x, y):
if cmp(x, y) is 1: return [y, x]
else: return [x, y]
pares = []
numbs = []
tmp = open("input", "r")
for i in range(0, int(tmp.readline())): numbs.append(int(tmp.readline()))
for x in numbs:
for y in numbs:
if not (x+y) % 10 and not par(x, y) in pares:
pares.append(par(x, y))
print "Total de conjuntos de numeros amigos: %s" % len(pares)
Solução por djthyrax
Haskell
import Data.Char (digitToInt)
import Data.List (tails)
import Control.Arrow ((&&&))
import Data.Set (empty, insert, size)
main = getContents >>= print . size . foldl procPares empty . tails . map fun . tail . lines
where fun :: String -> (Integer, Int)
fun = read &&& digitToInt . last
procPares s [] = s
procPares s ((n,d):l) = foldl (inserePar n) s $ filter ((dig ==) . snd) l
where dig = if d == 0 then 0 else 10 - d
inserePar n1 s (n2,_) = insert par s
where par = if n1 <= n2 then (n1,n2) else (n2,n1)
Solução por Betovsky.