Sunday, 30 September 2018

uva 424 Integer Inquiry solution

  1. #include<bits/stdc++.h>
  2. #include<string.h>
  3. #include<list>
  4. #include<iterator>
  5. using namespace std;
  6. char rr[102];
  7. int main()
  8. {
  9.     list<int>lst;
  10.     int mx=0;
  11.     list<int>:: iterator it;
  12.     char str[102][102];
  13.     int ii=0;
  14.     while(cin>>str[ii] ){
  15.         if(strlen(str[ii])==1 && str[ii][0]=='0'){
  16.             break;
  17.         }
  18.         int stln=strlen(str[ii]);
  19.         mx=max(mx,stln);
  20.         ii++;
  21.     }
  22.     for(int i=0;i<ii;i++){
  23.             int p=0;
  24.         if(mx>strlen(str[i])){
  25.             for(int j=strlen(str[i])-1;j>=0 ;j-- ){
  26.                     rr[p++]=str[i][j];
  27.             }
  28.             int ln=strlen(str[i]);
  29.             while(ln<mx){
  30.                 rr[ln++]='0';
  31.             }
  32.             for(int l=0;l<mx;l++){
  33.                 str[i][l]=rr[mx-1-l];
  34.             }
  35.             for(int k=0;k<102;k++) rr[k]='0';
  36.         }
  37.     }
  38.     int sum,r=0;
  39.     for(int j=mx-1;j>=0;j--){
  40.             sum=r;
  41.         for(int i=ii-1;i>=0;i--){
  42.             sum+= str[i][j]-'0';
  43.         }
  44.         lst.push_front(sum%10);
  45.         r=sum/10;
  46.     }
  47.     if(r>0) lst.push_front(r);
  48.     for(it=lst.begin();it!=lst.end();it++){
  49.         cout<<*it;
  50.     }
  51.     puts("");
  52.     lst.clear();
  53.     return 0;
  54. }

variable declaration of c program

#include <stdbool.h> #include<bits/stdc++.h> #include <stdio.h> #include <string.h> #include <std...