Answer the question
In order to leave comments, you need to log in
C — How to find the largest subsequence of perfect squares in an array?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ent(int *arrayName, int arraySize)
{
int i;
for (int i = 0; i < arraySize; i++)
{
printf("a[%i] = ", i);
scanf("%i", &arrayName[i]);
}
}
void proc(int *arrayName, int arraySize) {
int i,k = 0;
int flag = 0;
for (i = 0; i < arraySize; ++i)
{
if ((sqrt(arrayName[i])) == (round((sqrt(arrayName[i])))))
{
printf("%d - full square \n", arrayName[i]);
k++;
}
else
{
if (k!=0)
k = 1;
}
}
printf("Max otrezok = %d \n", k);
}
void printArray(int *arrayName, int arraySize) {
int i;
for (i = 0; i < arraySize; ++i) {
printf("%d ", arrayName[i]);
}
}
void main()
{ int i;
int n;//
int squar = 0;
printf("Size Massive: ");
scanf("%d", &n);
int Arr[n];
ent(Arr, n);
printf("Array: ");
printArray(Arr, n);
printf("\n");
proc(Arr,n);
system("pause");
}
void proc(int *arrayName, int arraySize) {
int i,k = 0;
int flag = 0;
for (i = 0; i < arraySize; ++i)
{
if ((sqrt(arrayName[i])) == (round((sqrt(arrayName[i])))))
{
printf("%d - full square \n", arrayName[i]);
k++;
}
else
{
if (k!=0)
k = 1;
}
}
printf("Max otrezok = %d \n", k);
}
Answer the question
In order to leave comments, you need to log in
Do you even understand what a maximum subsequence is? Based on your algorithm, no.
And what are you writing the code for? Under beer? Everything dances like I don't know how.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ent(int * arrayName, int arraySize) {
// int i; Так давно уже писать не нужно
for (int i = 0; i < arraySize; i++) {
printf("a[%i] = ", i);
scanf("%i", & arrayName[i]);
}
}
void proc(int * arrayName, int arraySize) {
int repeat = 0, maxRepeat = 0;
for (int i = 0; i < arraySize; ++i) {
if (checkSquareNumber(arrayName[i])) {
printf("%d - full square \n", arrayName[i]);
repeat++;
} else {
repeat = 0;
}
maxRepeat = repeat > maxRepeat ? repeat : maxRepeat;
}
printf("Max otrezok = %d \n", repeat); // Забыли как будет слово отрезок на английском?
}
int checkSquareNumber(int number) {
if (number == 0 || number == 1)
return 1;
if (number % 4 == 0 || number % 9 == 0)
return 1;
if (number % 8 == 1 || number % 3 == 1)
return 1;
return 0;
}
void printArray(int * arrayName, int arraySize) {
// int i; Так давно уже писать не нужно
for (int i = 0; i < arraySize; ++i) {
printf("%d ", arrayName[i]);
}
}
void main() {
int i;
int n; //
int squar = 0;
printf("Size Massive: "); // Забыли как будет слово массив на английском?
scanf("%d", & n);
int Arr[n];
ent(Arr, n);
printf("Array: ");
printArray(Arr, n);
printf("\n");
proc(Arr, n);
system("pause");
}
1. 2 - because you make k = 1 when the sequence ends. Make k = 0 - it will be 1.
And remove the condition - it is completely unnecessary.
else
{
k = 0;
}
double v = sqrt(arrayName[i]);
double v1 = floor(v);
if((v - v1) < 0.000001)
{
// число- полный квадрат
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ent(int *arrayName, int arraySize)
{
int i;
for (int i = 0; i < arraySize; i++)
{
printf("a[%i] = ", i);
scanf("%i", &arrayName[i]);
}
}
void proc(int *arrayName, int arraySize) {
int i,k = 0, max = 0;
int flag = 0;
for (i = 0; i < arraySize; ++i)
{
if ((sqrt(arrayName[i])) == (round((sqrt(arrayName[i])))))
{
printf("%d - full square \n", arrayName[i]);
k++;
}
else k = 0;
if (k>max)
max = k;
}
printf("Max otrezok = %d \n", max);
}
void printArray(int *arrayName, int arraySize) {
int i;
for (i = 0; i < arraySize; ++i) {
printf("%d ", arrayName[i]);
}
}
void main()
{ int i;
int n;//
int squar = 0;
printf("Size Massive: ");
scanf("%d", &n);
int Arr[n];
ent(Arr, n);
printf("Array: ");
printArray(Arr, n);
printf("\n");
proc(Arr,n);
system("pause");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question