Практикум по программированию. Основы. Циклы. Счастливые билеты

From AsIsWiki
(Difference between revisions)
Jump to: navigation, search
Line 57: Line 57:
  
 
<pre>
 
<pre>
 +
// g++ 4.2
 +
 +
#include <iostream>
 +
 +
using namespace std;
 +
 +
int main() {
 +
 +
    int nb, ne;
 +
   
 +
    cout << "\n Поиск счастливых билетов\n";
 +
    cout << "--------------------------\n";
 +
   
 +
    cout << " Введите начальный номер: ";
 +
    cin >> nb;
 +
   
 +
    cout << " Введите конечный номер:  ";
 +
    cin >> ne;
 +
   
 +
    cout << "------- Счастливые -------\n";
 +
   
 +
    int n, s1, s2;
 +
   
 +
    for (int i = nb; i <= ne; i++) {
 +
       
 +
        n = i; s1 = 0; s2 = 0;
 +
       
 +
        while (n > 1000) {
 +
            s1 += n % 10;
 +
            n /= 10;
 +
        }
 +
       
 +
      while (n > 0) {
 +
            s2 += n % 10;
 +
            n /= 10;
 +
        }
 +
       
 +
        if (s1 == s2) {
 +
            printf(" %d\n", i);
 +
        }
 +
    }
 +
 +
    cout << "\n";
 +
    return 0;
 +
}
 
</pre>
 
</pre>
  

Revision as of 07:45, 17 April 2015

Назад · Задачи · Дальше


Contents

Java

import java.util.Scanner;

public class Task14 {

    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        
	System.out.println();
        System.out.println(" Поиск счастливых билетов ");
        System.out.println("--------------------------");

        System.out.print(" Введите начальный номер: "); 
	int nb = in.nextInt();

        System.out.print(" Введите конечный номер:  "); 
	int ne = in.nextInt();

        System.out.println("------- Счастливые -------");

	int n, s1, s2;
        
	for (int i = nb; i <= ne; i++) {

	    n = i; s1 = 0; s2 = 0;

	    while (n > 1000) {
		s1 += n % 10;
		n /= 10;
	    }

   	    while (n > 0) {
		s2 += n % 10;
		n /= 10;
	    }

	    if (s1 == s2) {
		System.out.println(" " + i);
	    }
	}
    }
}


C++

// g++ 4.2

#include <iostream>

using namespace std;

int main() {

    int nb, ne;
    
    cout << "\n Поиск счастливых билетов\n";
    cout << "--------------------------\n";
    
    cout << " Введите начальный номер: ";
    cin >> nb;
    
    cout << " Введите конечный номер:  ";
    cin >> ne;
    
    cout << "------- Счастливые -------\n";
    
    int n, s1, s2;
    
    for (int i = nb; i <= ne; i++) {
        
        n = i; s1 = 0; s2 = 0;
        
        while (n > 1000) {
            s1 += n % 10;
            n /= 10;
        }
        
   	    while (n > 0) {
            s2 += n % 10;
            n /= 10;
        }
        
        if (s1 == s2) {
            printf(" %d\n", i);
        }
    }

    cout << "\n";
    return 0;
}


Pascal




Назад · Задачи · Дальше

Personal tools
Namespaces

Variants
Actions
Navigation
Tools