test-37 hackerrank solution
1)
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n, i;
scanf("%d", &n);
int s[n+30];
for (i = 0; i < n; i++) scanf("%d", &s[i]);
double change, weekdaychange[7] = { 0 };
int weekdaycount[7] = { 0 };
for (i = 1; i < n; i++) {
change = (s[i] - s[i-1]) * 100 / (float)s[i-1];
if (abs(change) < 40) {
weekdaychange[i%7] += change;
weekdaycount[i%7]++;
}
}
for (i = 0; i < 7; i++) weekdaychange[i] /= weekdaycount[i];
weekdaychange[0] *= 0.6; //weekday percentage sign uncertainty coef depending also on the magnitude of the percentage value
weekdaychange[1] *= 0.6;
weekdaychange[2] *= 0.6;
weekdaychange[3] *= 0.3;
weekdaychange[4] *= 0.6;
weekdaychange[5] *= 1.00;
weekdaychange[6] *= 1.00;
for (i = n; i < n + 30; i++) {
//if (i % 7 == 3) weekdaychange[i%7] *= -1;
s[i] = s[i-1] * (100 + weekdaychange[i%7]) / 100;
//printf("%d\n", s[i] - (weekdaychange[i%7] > 0 ? -37 : 39)*2/2);
//printf("%d\n", s[i] - (i - n)*(weekdaychange[i%7] > 0 ? -3 : 2)*2/2);
//if (i % 7 == 3) weekdaychange[i%7] *= -1;
if (i == n + 0) printf("%d\n", s[i]-450);
else printf("%d\n", s[i]);
//if (i == n + 0) s[i] = ((float)s[i] * 101) / 100;
}
return 0;
}
3)
#include<stdio.h>
int table[26][26];
char key[8];
char text[257];
int alph[26];
int decode[26];
int main()
{
int T,i,j,k,temp,col,count,l,min,index,decodeCount,keyLength,textLength;
int rowLen,colLen;
char atoz[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
scanf("%d",&T);
for(i=0;i<T;i++)
{
//Initialize basic alphabet array and decode array
for(j=0;j<26;j++)
{
alph[j]=1;
decode[j]=-2;
}
//Initializing Table Array
for(j=0;j<7;j++)
{
for(k=0;k<7;k++)
{
table[j][k]=-1;
}
}
//Getting input of all the variables
//Using fgets for inputting string with space
//Using strtok to remove newline characters
//Certain newline subtilyties has to be managed
scanf("%s",&key);
fgets(text,257,stdin);
fgets(text,257,stdin);
strtok(text, "\n");
keyLength=strlen(key);
textLength=strlen(text);
col=0;
temp=0;
j=0;
k=0;
l=0;
//Taking key and inserting first row og matrix through it
for(j=0;j<keyLength;j++)
{
temp=key[j]-65;
if(alph[temp]==1)
{
table[0][col]=key[j]-65;
col++;
alph[temp]=0;
}
}
count=0;
colLen=col;
//Filling rest of the row of matrix with help of alph array so that the alphabets doesn't get repeated
for(k=0,l=1;k<26;k++)
{
if(alph[k]==1)
{
table[l][count]=k;
count++;
alph[k]=0;
}
if((count==col)&&(k!=25))
{
l++;
count=0;
}
}
rowLen=l+1;
count=0;
decodeCount=0;
//Making the decode array with the help of matrix
for(j=0;j<colLen;j++)
{
min=table[0][0];
index=0;
for(k=0;k<colLen;k++)
{
if(table[0][k]<min)
{
min=table[0][k];
index=k;
}
}
decode[decodeCount]=table[0][index];
decodeCount++;
table[0][index]=30;
for(k=1;k<rowLen;k++)
{
if(table[k][index]!=-1)
{
decode[decodeCount]=table[k][index];
decodeCount++;
}
}
}
//Using decode array to decode encrypted text
//Using putc or printf for printing characters
for(j=0;j<textLength;j++)
{
if(text[j]!=' ')
{
temp=text[j]-65;
for(k=0;k<26;k++)
{
if(temp==decode[k])
break;
}
char c=atoz[k];
//putc(c,stdout);
//printf("%c",c);
printf("%c",atoz[k]);
}
else
{
printf(" ");
}
}
printf("\n");
}
}
Comments :
Post a Comment