Posts

UVA - 623 500! Solution in C++

UVA - 623 500! Solution in C++ /*  uva id: shoaib05  Accepted Time= 0.460 */ #include<iostream> #include<string> using namespace std; string sumOfTwoStringAsNumber(string str1, string str2){ string res; int len1 = str1.length(); int len2 = str2.length(); if (len1 == 0 && len2 == 0) return "0"; else if (len1 != 0 && len2 == 0) return str1; else if (len1 == 0 && len2 != 0) return str2; 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 >...

Multiply two large number as string in C++

Multiply two large number as string in C++ #include<iostream> #include<string> using namespace std; string sumOfTwoStringAsNumber(string str1, string str2){ string res; int len1 = str1.length(); int len2 = str2.length(); if (len1 == 0 && len2 == 0) return "0"; else if (len1 != 0 && len2 == 0) return str1; else if (len1 == 0 && len2 != 0) return str2; 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){ ...

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; } ...

Sum of two large number as string in C++

Sum of two large number as string in C++ #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...

UVA 10684- The Jackpot Solution

UVA 10684- The Jackpot  Solution /* uva id: shoaib05 Accepted Time: 0.000 */ #include<iostream> using namespace std; int input[10001],temp[10001]; int main() { int n,i,j,max; while(cin>>n) { if(n==0) break; for(i=0;i<n;i++) cin>>input[i]; max=temp[0]=input[0]; for(i=1;i<n;i++) { j=temp[i-1]+input[i]; if(j>input[i]) temp[i]=j; else temp[i]=input[i]; if(max<temp[i]) max=temp[i]; } if(max>0) cout<<"The maximum winning streak is "<<max<<"."<<endl; else cout<<"Losing streak."<<endl; } return 0; }

UVA 11094 Solution- Continents

UVA 11094 Solution- Continents /* uva id: shoaib05 Accepted Time: 0.000 Algorithm: DFS */ #include<iostream> #include<string.h> #include<stack> using namespace std; char arr[25][25]; char ch,ch1; typedef pair<int,int> pii; pii pr; int m,n; int dfs(int x,int y) { pr=pii(x,y); stack<pii> st; st.push(pr); int count=0; arr[x][y]=ch1; while(!st.empty()) { pr=st.top(); st.pop(); x=pr.first; y=pr.second; count++; if(x>0 && arr[x-1][y]==ch) //Traverse to Up { pr=pii(x-1,y); st.push(pr); arr[x-1][y]=ch1; } if(x<m && arr[x+1][y]==ch) //Traverse to Down { pr=pii(x+1,y); st.push(pr); arr[x+1][y]=ch1; } if(y>0 && arr[x][y-1]==ch) //Traverse to Left { pr=pii(x,y-1); st.push(pr); arr[x][y-1]=ch1; } if(y<n && arr[x][y+1]==ch) //Traverse to Right { pr=pii(x,y+1); st.push(pr); arr[x][y+1]=ch1; }...

11747 - Heavy Cycle Edges Solution

11747 - Heavy Cycle Edges Solution /* uva id: shoaib05 Accepted Time: 0.003 Algorithm: Kruskal .  */ #include<iostream> #include<vector> #include<algorithm> using namespace std; typedef pair<int,int> pii; typedef pair<pii,int>ppi; vector<ppi> myvec; int parent[1000],node; bool mySort(ppi p1,ppi p2) { return p1.second<p2.second; } int getParent(int n) { if(n==parent[n]) return n; else return getParent(parent[n]); } int main() { int u,v,wt,edge,i,j; pii p1; ppi p2; bool flag; while(cin>>node>>edge) { if(node==0 && edge==0) break; for(i=0;i<node;i++) parent[i]=i; for(i=0;i<edge;i++) { cin>>u>>v>>wt; p1=pair<int,int>(u,v); p2=pair<pii,int>(p1,wt); myvec.push_bac...