using System; class Progran { public static void Main() { int Size = int.Parse(Console.ReadLine()); sbyte[] FirstNumber = new sbyte[Size + 1]; for (int i = 0; i < Size; i++) { string[] s = Console.ReadLine().Split(' '); FirstNumber[i + 1] = (SByte)(sbyte.Parse(s[0]) + sbyte.Parse(s[1])); } for (int i = Size; i > 0; i--) if (FirstNumber[i] >= 10) { FirstNumber[i - 1]++; FirstNumber[i] -= 10; } for (int i = 0; i < Size; i++) Console.Write(FirstNumber[i + 1]);
} } Edited by author 31.05.2022 12:17 I ported this code to c++ and got AC ?! Edited by author 31.05.2022 15:01 I finally got accepted it on a C#!! Main memory issue here are always any Strings! Awoid using Console.ReadLine() in cycle. For some reason, .net sometimes does not use string interning. I got below accepted using only Console.Read() byte-by-byte: class Program { static int N = 0; static byte[] Digs1 = null; static char m_Zero = '0'; static void _ReadData() { string v_Str = Console.ReadLine(); if (v_Str == null) return false; N = Int32.Parse(v_Str); Digs1 = new byte[N]; int v_TmpVal = 0; // Fill Digs for (int i = 0; i < N; i++) { v_TmpVal = Console.Read(); Digs1[i] = (byte)(Convert.ToChar(v_TmpVal) - m_Zero); Console.Read(); v_TmpVal = Console.Read(); Digs1[i] += (byte)((Convert.ToChar(v_TmpVal) - m_Zero) * 10); Console.Read(); //#13 Console.Read(); //#10 } static void SolveProblem() { bool v_AddOne = false; bool v_NextAddOne = false; byte v_First = 0; byte v_Second = 0; for (int i = Digs1.Length - 1; i >= 0; i--) { v_First = (byte)(Digs1[i] % 10); v_Second = (byte)(Digs1[i] / 10); v_AddOne = (v_First + v_Second + (v_NextAddOne ? 1 : 0)) > 9; Digs1[i] = (byte)((v_First + v_Second + (v_NextAddOne ? 1 : 0)) % 10); v_NextAddOne = v_AddOne; } } static void Main(string[] args) { _ReadData(); SolveProblem();
for (int i = 0; i < N; i++) { Console.Write(Digs1[i]); } } Hi, I`m happy for you. And it`s great that you are sharing your thoughts. However, you are not helping anyone by posting your code. Please don`t do it, and we all will be happier. a=int(input()) b=0 for i in range(a): n,l=map(int, input().split()) b=b*10+n+l print('0'*(a-len(str(b))),b, sep='') Edited by author 05.05.2016 19:23 I think string is: result = str(b) +result During this operation: - new string with size = result.size+1 created; - b contents copied to new string I am not sure about memory but it must lead to TLE. You know your n1, n2, result strings have known size = n. You should rather use efficient byte arrays of size n to store n1, n2, result contents Edited by author 28.04.2016 13:22 if You get TLE verdict, U can try to use short instead of int program text; {$APPTYPE CONSOLE} var n,k,i,j,n1,n2,col,lastlen:integer; num1,num2:array[0..1000001] of byte; t1,t2:longword; Procedure Add; var i,carry:integer; begin carry:=0; for i:=1 to high(num1) do begin carry:=carry+num1[i]+num2[i]; if carry>=10 then begin num1[i]:=carry-10; carry:=1; end else begin num1[i]:=carry; carry:=0; end; end; end; begin { TODO -oUser -cConsole Main : Insert code here } { Assign(input,'Input.txt'); reset(input); Assign(output,'output.txt'); rewrite(output); } fillchar(num1,sizeof(num1),0); fillchar(num2,sizeof(num2),0); readln(n); col:=0; k:=0; for i:=n downto 1 do begin readln(n1,n2); num1[i]:=n1; num2[i]:=n2; end; ADD; k:=high(num1); while num1[k]=0 do dec(k); if k=0 then writeln(0) else for i:=k downto 1 do write(num1[i]); end. I think you should be more careful when you read the task. Test 4 0 0 1 1 1 1 1 1 Answer 0222 And your program gives 222. Edited by author 06.09.2006 15:38 //but why me #include<bits/stdc++.h> using namespace std; int main() { long n,i,j=0; scanf("%ld",&n); int a[2000003],b=0; string s; n*=2; for(i=0;i<n;i++){ scanf("%d",&a[i]); } for(i=n-1;i>=0;i=i-2){ b=(b+a[i]+a[i-1]); s[j]=(b%10)+48; b=b/10; j++; } //if(b!=0){ //cout<<b; //} for(i=j-1;i>=0;i--){ printf("%c",s[i]); } } I have made the same mistake. #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; int a; int b; long double c=0; for (unsigned int i = 0; i < n; i++) { cin >> a; cin >> b; if (i != 0 ) { c = c * 10 + a + b; } else c = a + b;
}
cout <<c<< endl; } 3 1 1 9 0 9 1 try this this test helped me My code's producing 300. It's right, isn't it? What is double mantissa size? About 17 decimal digits? What is required number size? About 1,000,000 digits? So, how your solution is supposed to work? //wa on test 2 .. whats the problem with my code #include<bits/stdc++.h> using namespace std; int main() { long n,i,j=0; cin>>n; int a[2000003],b=0; string s; n*=2; for(i=0;i<n;i++){ cin>>a[i]; } for(i=n-1;i>=0;i=i-2){ b=(b+a[i]+a[i-1]); s[j]=(b%10)+48; b=b/10; j++; } for(i=j-1;i>=0;i--){ cout<<s[i]; } } well the problem is pretty simple and straight forward but when I try to solve this in python I tried out all algos like adding them that is N iteration then carrying them another N iteration. then print... or store them in a array then add from last digit that is also 2N iterations.... but is there any iteration which is less than 2N iteration including reading the values please help me!! #include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ int k; cin>>k; vector<int> line1; vector<int> line2; vector<int> sum; while(k--){ int m,n; cin>>m>>n; line1.push_back(m); line2.push_back(n); } bool plus_1=false; for(int i=line1.size()-1;i>=0;i--){ int temp=line1[i]+line2[i]; if(plus_1){ temp++; } if(temp>=10){ plus_1=true; temp-=10; }else{ plus_1=false; } sum.push_back(temp);
} for(vector<int>::reverse_iterator iter=sum.rbegin();iter!=sum.rend();++iter){ cout<<*iter; } return 0; } use ios_base::sync_with_stdio(false); cin.tie(0); with cin/cout. Don't forget to add last two digit's sum carry if it has. 5M is too much. It isn't necessary to get whole numbers in memory. N=int(input()) s=0 for i in range(N): x,y=map(int,input().split()) s*=10 s+=(x+y) print(s) s is one digit by definition But if a+b>=10 s can be expressed only using two digits Ohh, wait... Try print(int(s)) I suppose you print in scientific notation when s is huge That is my AC solve. #include <stdio.h> char a[1000005], b[1000005]; int Count, min; int main() { scanf("%d", &Count); min=1; for (int i=1; i<=Count; i++) scanf("%d %d", &a[i], &b[i]); // summing for (int i=Count; i>=0; i--) { int temp = a[i] + b[i]; a[i] = temp%10; a[i-1] += temp/10; } if (a[0] == 1) min--; for (int i=min; i<=Count; i++) printf("%d", a[i]); return 0; } I wrote the same code on C#, but it was TLE on test#4! So, i send your code. Thank you. In c++ use scanf, or write this: #define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) And in main func write sync; -- use fread | fwrite; -- do not use << if >> statement or minimize it; -- minimize div and mod operations ( % , / operators); -- instead of % , / operators may to use pre-calculated arrays. -- and last, need lucky :) 10^6 numbers --> input file size ~4 Mbytes, and output file size ~1 Mbytes. MY PC i-core 3 , 3 GB RAM, gcc 5.4.0 with option: g++ -Wall -O3 -std=c++14 reads/writes took 6-9 milliseconds. So there need something magic tricky to solve this with time < 1 milliseconds !!!!! I got 0.001s AC!! buffered i/o helped me. Your program ate 12 Mb memory. So I suppose you are reading the whole numbers and then do addition. It isn't necessary. It's possible to add numbers digit by digit (yes, from high digits to low) without any arrays usage - 0.3s even using scanf/printf. And yes, use fread/fwrite. Edited by author 13.12.2016 22:15 import sys n = input() arr = bytearray(n) i = 0 while i < n: line = raw_input().split() arr[i] = (int(line[0]) + int(line[1])) i += 1 result = "" i = n-1 while i > 0: if arr[i] > 9: arr[i] -= 10 arr[i-1] += 1 result = str(arr[i]) + result i -= 1
arr[0] = arr[0]%10 result = str(arr[0]) + result print result result = str(arr[i]) + result Run this code 1M iterations locally. import java.util.Scanner; public class T1048 { public static void main(String[] args){ Scanner sc=new Scanner(System.in); int a=sc.nextInt(); int[] mas=new int[a]; int[] mas1=new int[a]; String b=""; String b1=""; for(int i=0;i<a;i++) { mas[i]=sc.nextInt(); b+=mas[i]; mas1[i]=sc.nextInt(); b1+=mas1[i]; } int v=Integer.parseInt(b); int v1=Integer.parseInt(b1); System.out.println(v+v1); }} Timus text "Runtime error" №2 Task: > The first line contains a single integer N that is the length of the given integers (1 ≤ N ≤ 1 000 000) Your code: > int v=Integer.parseInt(b); How many digits can be placed into int v? here's my code.Please give me some advise or test data! int n = int.Parse(Console.ReadLine()); int nCopy = n; int sum = 0; string[] array = new string[2]; short digitSum = 0; while(n-- != 0) { digitSum = 0; array = Console.ReadLine().Split(' '); foreach(var i in array) { digitSum += short.Parse(i); } sum += digitSum * (int)Math.Pow(10, n); } Console.WriteLine(sum.ToString().Length > nCopy ? sum.ToString().Substring(1):sum.ToString().PadLeft(nCopy,'0')); From task description: > (1 ≤ N ≤ 1 000 000) Why do you think you are able to put result into 32-bit (10 digits max) int variable? i have wa 4 A test: 5 0 0 0 0 0 0 9 9 9 9 Answer:00198 bits/stdc++.h -> TLE on Test #4 cstdio -> TLE on Test #4 stdio.h -> AC Compiled with: G++ 4.9 C++11 Can anyone give an explanation? in.nextToken(); int n = (int) in.nval; byte ch1[] = new byte[n]; byte ch2[] = new byte[n]; while (n > 0) { in.nextToken(); ch1[n - 1] = (byte) in.nval; in.nextToken(); ch2[n - 1] = (byte) in.nval; n--; } int ost = 0; String answer = ""; for (int i = 0; i < ch1.length; i++) { int a = ost + ch1[i] + ch2[i]; if (a > 9) { answer = (a % 10) + answer; ost = a / 10; } else { answer = a + answer; ost = 0; } } out.print(answer); Could someone help with time optimization? For example if I take N =4. then i write: 10 12 9 3 15 10 11 13 What will I get? i think that no such input like yours. ( my ac solution does not handle it properly. ) you should write it in this way (only one digit for one number per line): 1 1 0 2 9 3 1 1 5 0 1 1 1 3 and therefore N = 7 |
|