forked from PraveenMohan13/BASIC-50
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhalf reverse
More file actions
136 lines (122 loc) · 2.19 KB
/
half reverse
File metadata and controls
136 lines (122 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include<stdio.h>
int main() {
int n=12345,ans=0,r=0;
int c=floor(log10(n))+1;
int half=(c/2)+1;
int t_half=half;
while(half)
{
r=n%10;
ans=ans*10+r;
half--;
n=n/10;
}
int fans=n*pow(10,t_half)+ans;
printf("%d",fans);
}
------------------------------------------------------------------------------------------------------------------
Need to write a program left and right half separately
Input Format
7
1 2 3 4 5 6 7
Constraints
1<=n<=20
Output Format
3 2 1 4 7 6 5
Sample Input 0
6
1 2 3 4 5 6
Sample Output 0
3 2 1 6 5 4
front and back
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int half=n/2;
int l=0;
int r=half-1;
while(l<r)
{
int temp=a[l];
a[l]=a[r];
a[r]=temp;
l++;
r--;
}
half=ceil(n/2.0);
l=half;
r=n-1;
while(l<r)
{
int temp=a[l];
a[l]=a[r];
a[r]=temp;
l++;
r--;
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
------------------------------------------------------------------------------------------------------------------
Array
#include<stdio.h>
int main() {
int n,L,r,temp;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int half=n/2;
L=half;
r=n-1;
while(L<r)
{
temp=a[L];
a[L]=a[r];
a[r]=temp;
L++;
r--;
}
for(int i=0;i<n;i++)
printf("%d",a[i]);
}
------------------------------------------------------------------------------------------------------------------
front and back
#include<stdio.h>
int main(){
int n,i,l,r;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int half=n/2;
l=half;
r=n-1;
while(l<r)
{
int temp=a[l];
a[l]=a[r];
a[r]=temp;
l++;
r--;
}
l=0,r=half-1;
while(l<r)
{
int temp=a[l];
a[l]=a[r];
a[r]=temp;
l++;
r--;
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
}