Notes
Notes - notes.io |
1. Запросить у пользователя ввод целого числа, которое нужно получить как сумму чисел из массива.
2. Создать массив, запрашивая и сохраняя введенные пользователем целые числа.
3. Пройтись по массиву, сложить каждую пару чисел и проверить, равна ли их сумма числу, введенному пользователем.
4. Если сумма равна заданному числу, сохранить индексы этих чисел в новый массив.
5. Вывести новый массив с индексами.
Примерный код на Java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Введите число для суммы:");
int target = scanner.nextInt();
System.out.println("Введите размер массива:");
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
int[] indices = new int[2]; // создаем массив для индексов
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] + arr[j] == target) {
indices[0] = i;
indices[1] = j;
break;
}
}
}
System.out.println("Индексы чисел, сумма которых равна " + target + ": " + indices[0] + ", " + indices[1]);
}
}
Задача 2:
1. Запросить у пользователя ввод строки.
2. Разбить строку на слова, подсчитать количество букв в первом и последнем слове.
3. Вывести количество букв для первого и последнего слова.
Примерный код на Java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Введите строку:");
String input = scanner.nextLine();
String[] words = input.split(" ");
int firstWordLength = words[0].length();
int lastWordLength = words[words.length - 1].length();
System.out.println("Количество букв в первом слове: " + firstWordLength);
System.out.println("Количество букв в последнем слове: " + lastWordLength);
}
}
Это примеры кода на Java для решения данных задач.
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team