marți, 11 decembrie 2018

c++ programming part 17

Curs gratuit cu program de internship de programare java- taxa de inscriere 100 lei, Ploiesti
Rel. la 0729 131 325


1394. Permutation
1395. There is a lot with n elements and it is required to display all the permutations of
the set.
1396. Ex: n = 3 and the items are: 1,2,3.
1397. Permutations are:
1398. 1 2 3
1399. 1 3 2
1400. 2 1 3
1401. 2 3 1
1402. 3 1 2
1403. 3 2 1
1404. #include  
1405. int st[20], n, k, as, ev;
1406. //a an array called st, n the number, k array position, as means i have succesor
1407. //ev means it is valid
1408. void init (int k, int st [])
1409. //init array
1410. {st [k] = 0;
1411. }
1412. int successor (int k, int st [])
1413. //search the next element in array, called succesor
1414. {if (st [k] 1415. {st [k] ++;
1416. as = 1;
1417. //in c++, value 1 means true
1418. }
1419. else as = 0;
1420. //in c++, value 0 means false
1421. return as;
1422. //return what i calculate in function
1423. }
1424. int valid (int k, int st [])
1425. {
1426. //verify if the value on position k into array is valid
1427. ev = 1;
1428. for (int i = 1; i 1429. if (st [i] == st [k])
1430. ev = 0;
1431. //the elements must be distinct
1432. return ev;

1433. }
1434. int solution (int k)
1435. //verify that the array is complete, means current position k it's equals with the n
1436. {if (k == n)
1437. return 1;
1438. else return 0;}
1439. void typar ()
1440. //display a solution
1441. {for (int i = 1; i <= n; i ++)
1442. cout << st [i];
1443. cout << endl;
1444. }
1445. int main ()
1446. //strating main
1447. {cin >> n;
1448. //read input data
1449. k = 1;
1450. //i am on the first position in array
1451. init (k, st);
1452. //init array
1453. while (k> 0)
1454. {
1455. //while the array is not empty
1456. do {
1457. //execute
1458. as = successor (K, st);
1459. //search the next element into the array
1460. if (as)
1461. //if exist
1462. ev = valid (k, st);
1463. //verify if it is valid
1464. }
1465. while (! (! as || (as && ev)));
1466. if (as)
1467. //if exist next element in the array
1468. if (solution (k))
1469. //verify if I obtain a solution
1470. typar ();
1471. //and the answer is yes, will display the solution
1472. else
1473. {k ++;
1474. //else we are going on the next position into the array
1475. init (k, st);
1476. //we start again
1477. }

1478. else k--;
1479. //decrease the position in the array
1480. }
1481. return 0;
1482. //return an int
1483. }
1484. Generate arrangements
1485. There is a set of n elements. Generate all arrangements for p take by n.
1486. Example: n = 3 and p = 2
1487. The arrangements of 3 are taken 2.
1488. It will display:
1489. 1 2
1490. 2 1
1491. 1 3
1492. 3 1
1493. 2. 3
1494. 3 2
1495. #include  
1496. int [20], n, p, k, as, ev;
1497. void init (int k, int st [])
1498. {st [k] = 0;}
1499. int successor (int k, int st [])
1500. {if (st [k] 1501. {st [k] ++;
1502. as = 1;
1503. }
1504. else as = 0;
1505. return as;
1506. }
1507. int valid (int k, int st [])
1508. {
1509. ev = 1;
1510. for (int i = 1; i 1511. if (st [i] == st [k])
1512. ev = 0;
1513. return ev;
1514. }
1515. int solution (int k)
1516. {if (k == p)
1517. return 1;
1518. else return 0;}
1519. void typar ()
1520. {for (int i = 1; i <= p; i ++)
1521. //the array have p elements

1522. cout << st [i];
1523. cout << endl;
1524. }
1525. int main ()
1526. {cin >> n;
1527. cin >> p;
1528. k = 1;
1529. init (k st);
1530. while (k> 0)
1531. {
1532. do {
1533. as = successor (K st);
1534. if (as)
1535. ev = valid (k, st);
1536. }
1537. while (! (! as || (as && ev)));
1538. if (as)
1539. if (solution (k))
1540. typar ();
1541. else
1542. {k ++;
1543. init (k st);
1544. }
1545. else k--;
1546. }
1547. return 0;
1548. }


Niciun comentariu:

Trimiteți un comentariu

Rețineți: Numai membrii acestui blog pot posta comentarii.