UVA- 10334 Ray Through Glasses Solution in C++
UVA- 10334 Ray Through Glasses Solution
/*
uva id : shoaib05
Accepted Time: 0.000
*/
#include<iostream>
#include<string>
using namespace std;
string sumOfTwoStringAsNumber(string str1, string str2){
string res;
int len1 = str1.length();
int len2 = str2.length();
string temp1, temp2;
if (len1 >= len2){
temp1 = str1;
temp2 = str2;
}else{
temp1 = str2;
temp2 = str1;
int temp = len2;
len2 = len1;
len1 = temp;
}
int diff = len1 - len2;
int i,j,rem;
char ch;
bool flag = false;
j = len1 - 1;
res = "";
for (i = len2 - 1; i >= 0; i--){
ch = temp1[j] + temp2[i] - 48;
if (flag)
ch++;
if (ch > 57)
flag = true;
else
flag = false;
ch = ((ch - 48) % 10) + 48;
res = ch + res;
j--;
}
while (j >= 0){
ch = temp1[j];
if (flag){
ch++;
if (ch > 57){
flag = true;
ch = ((ch - 48) % 10 )+ 48;
}
else{
flag = false;
}
}
res = ch + res;
j--;
}
if (flag){
res = '1' + res;
}
return res;
}
int main(){
string arr[1002];
arr[0] = "1";
arr[1] = "2";
arr[2] = "3";
int n;
for (int i = 3; i <= 1000; i++){
arr[i] = sumOfTwoStringAsNumber(arr[i-1],arr[i-2]);
}
while (cin>>n)
cout << arr[n] << endl;
return 0;
}
Comments
Post a Comment