Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
You are given a string S. Your task is to determine if sum of ASCII values of all character results in a perfect square or not.
INPUTS ARE:
1) 2
MKNKEWIILKVRCNNWOGBVXKXRWRSDUOVJBLWFIFOWRMOUDFRRLVPLINEFIYIFNGORTLYFSPCLFSGIXACLVRXFHEMPDVUTEMLYAKETBIFIDNQCNUPMMNTTRGMXCJRHVFIWPOSSWABANTEDOURDKLAFSMDUVUEUAORRDKNCKQFAL
d

2) 2
d
MKNKEWIILKVRCNNWOGBVXKXRWRSDUOVJBLWFIFOWRMOUDFRRLVPLINEFIYIFNGORTLYFSPCLFSGIXACLVRXFHEMPDVUTEMLYAKETBIFIDNQCNUPMMNTTRGMXCJRHVFIWPOSSWABANTEDOURDKLAFSMDUVUEUAORRDKNCKQFAL


What I have tried:

#include <bits/stdc++.h>
using namespace std;

int ischeck(int n)
{
    int z=sqrt(n);
    return ((z*z)==n);
}

int main() {
	int t;
	cin>>t;
	while(t--)
	{
	    string s;
	    cin.ignore();
	    getline(cin,s);
	    int i,sum=0;
	    for(i=0;i<s.length();i++)
	    sum+=int(s[i]);
	    if(ischeck(sum))
	    cout<<"1"<<endl;
	    else
	    cout<<"0"<<endl;
	}
	return 0;
}
Posted
Updated 21-Jan-19 11:20am
Comments
Richard MacCutchan 21-Jan-19 12:14pm    
What is the question?
Prateek Krishna 21-Jan-19 12:20pm    
You are given a string S. Your task is to determine if sum of ASCII values of all character results in a perfect square or not.

Input:
The first line of the input contains a single integer T, denoting the number of test cases. Then T test cases follow. Each testcase has a string S

Output:
Print 1 if the resulting sum is a perfect square, else print 0.

Constraints:
1<=T<=100
1<=|S|<=1000
Richard MacCutchan 21-Jan-19 12:24pm    
Fine, but what is your question?
Prateek Krishna 21-Jan-19 12:27pm    
when i entered the input
2
d
dddd
it's output is 1 0.
but when i entered the input as
2
dddd
d
the output is 1 1.
why is this happening?
Richard MacCutchan 21-Jan-19 12:31pm    
No idea. But you could start by printing the values that you are calculating as the sum, and also the square root. Chances are that using integer values means you will not get exact results for some numbers, since the square roots of many numbers are not exact integers.

Advice: output the sum too.

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include <bits/stdc++.h>
using namespace std;

int ischeck(int n)
{
    int z=sqrt(n);
    return ((z*z)==n);
}

int main() {
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin.ignore();
        getline(cin,s);
        int i,sum=0;
        for(i=0;i<s.length();i++)
            sum+=int(s[i]);
        if(ischeck(sum))
            cout<<"1"<<endl;
        else
            cout<<"0"<<endl;
    }
    return 0;
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

Quote:
Why the code is showing different behaviour on changing the order of inputs.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
You are ignoreing too much. try
C++
include <iostream>
#include <cmath>
using namespace std;

int ischeck(int n)
{
  int z=sqrt(n);
  return ((z*z)==n);
}

int main()
{
  int t;
  cin>>t;
  cin.ignore();
  while(t--)
  {
    string s;
    getline(cin,s);
    size_t sum=0;
    for(size_t i=0; i<s.length(); i++)
      sum += int(s[i]);
    cout << ischeck(sum) << endl;
  }
  return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900