Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
1
Indexable
Never
תרגיל 1:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int f1(int n);

int main() 
{
    int n = 13;
    int longest_subseries = f1(n);
    printf("The length of the longest consecutive subseries is: %d\n", longest_subseries);
    int f1();
    return 0;
}


int f1(int n) {
    int max_length = 0;
    int current_length = 1;
    int previous_number, current_number;

    // Read the first number separately
    scanf("%d", &previous_number);

    for (int i = 1; i < n; i++) {
        scanf("%d", &current_number);
        if (current_number > previous_number) {
            current_length++;
        } else {
            current_length = 1;
        }
        if (current_length > max_length) {
            max_length = current_length;
        }
        previous_number = current_number;
    }

    return max_length;
}

תרגיל 2:

#include <stdio.h>

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int f2(int n); 

int main() 
{
  int n;
  printf("Enter a positive number:\n");
  scanf("%d", &n);
  int result = f2(n);
  printf("%d\n", result); // Output: 1 or 0
  return 0;
}


int f2(int n) {
  int digits = 0; // Initialize digits count to 0
  int tmp = n; // Make a copy of the input integer to avoid modifying it
  while (tmp != 0) { // Count the number of digits in the input integer
    digits++; // Increment digits count
    tmp /= 10; // Divide the input integer by 10 to move to the next digit
  }

  if (digits % 2 == 1) { // If the number of digits is odd
    return 0; // Return 0, because the input integer can't be split into two halves
  }

  int divisor = 1; // Initialize the divisor to 1
  for (int i = 0; i < digits / 2; i++) { // Calculate the divisor using a loop
    divisor *= 10; // Multiply the divisor by 10 for each digit in the first half
  }

  int first_half = n / divisor; // Calculate the first half of the input integer
  int second_half = n % divisor; // Calculate the second half of the input integer
  if (first_half == second_half) // If the two halves are equal
  { 
    return 1; // Return 1, because the input integer can be split into two equal halves
  } 
  
  else 
  {
    return 0; // Return 0, because the input integer can be split into two unequal halves
  }
}


תרגיל 3:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int f3(int n);

int main()
{
    int n;
    printf("Enter a positive number:\n");
    scanf("%d", &n);
    int result = f3(n);
    printf("%d\n", result); // Output: 1 or 0
    return 0;
}

int f3(int n)
{
    int digits = 0;
    int temp = n;
    while (temp > 0) {
        digits++;
        temp /= 10;
    }

    if (digits % 2 != 0) { // If the number of digits is odd
        return 0;
    }

    int divider = 1;
    for (int i = 0; i < digits / 2; i++) {
        divider *= 10;
    }

    int first_half = n / divider; // Calculate the first half of the digits
    int second_half = n % divider; // Calculate the second half of the digits
    if (first_half == second_half) // If the two halves are equal
    {
        return 1;
    }
    else // If the two halves are not equal
    {
        return 0;
    }
}