#include <bits/stdc++.h> using namespace std; vector<bool> used(10, false); int ans[3]; void solve(int turn){ if(turn == 3){
if(111 * ans[2] + 111 * ans[1] + 111 * ans[0] == 1000 * ans[2] + 110 * ans[0] + ans[1]) cout << "a = " << ans[0] << " b = " << ans[1] << " c = " << ans[2] << '\n'; return; }else{ for(int i = 1; i <= 9; i++){ if(!used[i]){ used[i] = true; ans[turn] = i; solve(turn + 1); used[i] = false; } } }
} int main(){
solve(0); return 0; } |
Reddit, HN, Euler, DH gibi sitelerden iş görüşmesi soruları + ödev soruları (11. sayfa)
-
-
Recursion olmadan da aşağıdaki şekilde yapılabilir :
#include <bits/stdc++.h>
using namespace std;
void solve(){
int ans[3];
int t;
for(int i = 0; i < 1 << 9; i++){
t = 0;
std::bitset<9> x(i);
if(x.count() == 3){
for(int j = 0; j < 9; j++){
if((i >> j) & 1)
ans[t++] = j + 1;
}
int perm[3];
iota(perm, perm + 3, 0);
do{
if(111 * ans[perm[2]] + 111 * ans[perm[1]] + 111 * ans[perm[0]] == 1000 * ans[perm[2]] + 110 * ans[perm[0]] + ans[perm[1]])
cout << "a = " << ans[perm[0]] << " b = " << ans[perm[1]] << " c = " << ans[perm[2]] << '\n';
}while(next_permutation(perm, perm + 3));
}
}
}
int main(){
solve();
return 0;
}
-
Kodu derlerken:
no match for 'operator!' (operand type is 'std::vector<bool>')
15 | if(!used){
| ^~~~~
yanlışını veriyor. Sanırım used vektörüne endeks vermek gerekiyor.
< Bu ileti mini sürüm kullanılarak atıldı > -
(defun bulundu? (i &optional (sonuç '(1 2 3)) (görüldü nil) (n (length sonuç))) (if (< i (1- n)) (do ( (j 1 (1+ j) ) ) ( (> j 9) sonuç) (unless (some (lambda (x) (= x j)) görüldü) (setf (nth i sonuç) j) (push j görüldü) (if (bulundu? (1+ i) sonuç görüldü n) sonuç) (setf görüldü (remove j görüldü)))) (do ( (j 1 (1+ j) ) ) ( (> j 9) sonuç) (unless (some (lambda (x) (= x j)) görüldü) (setf (nth i sonuç) j) (let ((sol (* 111 (reduce #'+ sonuç))) (sağ (reduce #'+ (mapcar (lambda (x y) (* x y)) '(1000 100 10 1) (list (third sonuç) (first sonuç) (first sonuç) (second sonuç)))))) (if (= sol sağ) (return-from bulundu? sonuç) nil))))))
Kod
Yığını:Kod Lisp ile yazıldı. Herhangi bir modern Lisp derleyici ile deneyebilirsiniz.
-
-
Bu mesaj IP'si ile atılan mesajları ara Bu kullanıcının son IP'si ile atılan mesajları ara Bu mesaj IP'si ile kullanıcı ara Bu kullanıcının son IP'si ile kullanıcı ara
KAPAT X