Spot the Difference

I wrote a piece of code for a spoj problem, and after about an hour of struggling, I looked up the answer, and I could not see the semantic difference, please help:
My code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

int main() {
	int t;
	long long int b;
	int A[][4] ={{0, 0, 0, 0}, {1, 1, 1, 1},{2, 4, 8, 6}, {3, 9, 7, 1},{4, 6, 4, 6}, {5, 5, 5, 5},{6, 6, 6, 6}, {7, 9, 3, 1},{8, 4, 2, 6}, {9, 1, 9, 1}};
	string a;
	cin>>t;
	for(int i=0; i<t; ++i) {
		cin>>a>>b;
		if(b==0) {
			cout<<'1\n';
			continue;
		}
		b%=4;
		b= (b==0) ? 3 : b-1;
		int e=a[a.length()-1]-48;
		cout<<A[e][b]<<'\n';
	}
	return(0);
}

The solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;	
int main(int argc, const char *argv[]) {
int zad[][4] ={{0, 0, 0, 0}, {1, 1, 1, 1},{2, 4, 8, 6}, {3, 9, 7, 1},{4, 6, 4, 6}, {5, 5, 5, 5},
{6, 6, 6, 6}, {7, 9, 3, 1},
{8, 4, 2, 6}, {9, 1, 9, 1}};
int t;
cin >> t;
for(int i=0; i<t; i++) {
string a;
long long b;
cin >> a >> b;
if(b==0) {
cout << 1 << endl;
} else {
int aa = a[a.size()-1] - '0';
b %= 4;
b = (b == 0) ? 3 : b-1;
cout << zad[aa][b] << endl;
}
}
return 0;
}
cout<<'1\n';
Hmm... does that even compile?
why would it not, is prints the number one, with a newline char. Same as
cout<<"some text\n";
Last edited on
Can you give sample input and output?
Edit: Only other difference I see is int e=a[a.length()-1]-48; and int aa = a[a.size()-1] - '0';
Last edited on
Sample I/O:

Input:
3
3 10
6 2
150 53

Output:
9
6
0
Last edited on
Script_Coder.cpp:14:19: warning: multi-character character constant [-Wmultichar]
Script_Coder.cpp: In function ‘int main()’:
Script_Coder.cpp:7:5: warning: ISO C++ 1998 does not support ‘long long’ [-Wlong-long]


It should be cout << "1\n"; instead because '\n' is a character on its own, and single quotes are used for single characters.

Tip: if you use GCC, you might want to compile with -Wall -Wextra -pedantic for interesting warnings.
@Catfish thank you, so much. Such a stupid mistake. I compile with g++ 4.6.2 , why does it not give me those error messages.
Edit: Only other difference I see is int e=a[a.length()-1]-48; and int aa = a[a.size()-1] - '0';

The ascii value of '0' is 48, so no difference there.
I was referring to the use of size() instead of length()
@ Script Coder: so it works now as expected?

As for the warnings not showing up, I use GCC 4.7.2 but still, do you use the warning flags I mentioned?
g++ -Wall -Wextra -pedantic -std=c++98 Script_Coder.cpp -o Script_Coder.exe

It works perfectly, and no I use no warning flags at all.
g++ Script_Coder.cpp -o Script_Coder.exe
Topic archived. No new replies allowed.