|
|
in python: -5 % 7 == 2 in c++: -5 % 7 == -5 a = int(input()) c= a//7 d=c*7 print(a-d) I got my code running due to the solutions posted.. But i want to understand the logic behind the division.. Any one? #include<bits/stdc++.h> using namespace std; int main(){ unsigned long long int n; try{ cin >> n; cout << n%7 << endl; }catch(exception e){ cout << 1 << endl; } return 0; } #include <stdio.h> int main() { unsigned long long int a; scanf("%llu", &a); if (a/7!=0) printf("%llu\n", a%7); else if (a < 7) printf("%d\n", a); } #include <cstdio> char ch, res; int main (){ while(scanf(" %c", &ch)!= EOF||(printf("%d\n", res)&0)) res= (res*10+ ch-'0')%7; } I'm too!!! import java.math.BigInteger; import java.util.Scanner; public class T_1243 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); BigInteger n = sc.nextBigInteger(); System.out.print(n.mod(new BigInteger("7"); )); }} public class Timus_1243 { public static void main(String[] args) { System.out.print(new java.util.Scanner(System.in).nextBigInteger().mod(java.math.BigInteger.valueOf(7))); } } you used long arithmetic, it is not best solution, read first post more accurately, it doesn't using any long arythmetic and arrays, collections, etc. I used the same algoritm! But on Pascal it takes a litle more lines(11 to be exact). But on Pascal my program works 0.015 seconds, but yours on C - 0.031 Edited by author 25.03.2011 22:55 My C solution works the same time as yours, but it used 104 КB memory instead of 154 КB in your case. #include <stdio.h> int main(void){ int c,a=0; for(;(c=getchar())!='\n';a=(a*10+c-'0')%7); printf("%u\n",a); return 0; } super #include <iostream> int main(void){ int c,a=0; for(;(c=getchar())!='\n';a=(a*10+c-'0')%7); printf("%u\n",a); return 0; } 0.015s. Why does it work? I just don't understand the logic. AC in 2 lines: n = int(input()) print(n%7) Just 1 line :) print(int(input())%7) Edited by author 29.08.2018 01:51 #include <iostream> using namespace std; int main() { int k = 0; long long int s = 0; char num[52]; int arr[51]; bool flag = true; cin >> num; for(int i = 0; flag; i++) { switch(num[i]) { case '0': { arr[i] = 0; k++; break; } case '1': { arr[i] = 1; k++; break; } case '2': { arr[i] = 2; k++; break; } case '3': { arr[i] = 3; k++; break; } case '4': { arr[i] = 4; k++; break; } case '5': { arr[i] = 5; k++; break; } case '6': { arr[i] = 6; k++; break; } case '7': { arr[i] = 7; k++; break; } case '8': { arr[i] = 8; k++; break; } case '9': { arr[i] = 9; k++; break; } case '\0': { flag = false; break; } } } for(int i = k-1; i+1 > 6;) { s = s + arr[i]; s = s + arr[i-1]*3; s = s + arr[i-2]*2; s = s + arr[i-3]*6; s = s + arr[i-4]*4; s = s + arr[i-5]*5; i = i - 6; k = k - 6; } if(k > 0) { s = s + arr[k-1]; k--; } if(k > 0) { s = s + arr[k-1]*3; k--; } if(k > 0) { s = s + arr[k-1]*2; k--; } if(k > 0) { s = s + arr[k-1]*6; k--; } if(k > 0) { s = s + arr[k-1]*4; k--; } if(k > 0) { s = s + arr[k-1]*5; k--; } cout << s%7; return 0; } Edited by author 16.11.2013 14:57 Edited by author 16.11.2013 14:57 Thats all ;) char c,ans; main(){ for(;(c=getchar())!='\n';ans=(ans*10+c-'0')%7); printf("%u\n",ans);
return 0; } компилятор visual studio 2010 - 1wa c++11 -4wa c++14 -4wa КАК???!!! код #include <iostream> #include <string> using namespace std; int main() { long int a; cin >> a; if(a < 7){ cout << a; return 0; } if(a>=7) cout << a%7; } I think number's type long more. You don't used long long. #include <iostream> #include <math.h> using namespace std; int main(){ double x; int i; cin >> x; x = fmod (x, 7.0); printf("%.0f",x); system("pause"); return 0; } Edited by author 18.08.2014 21:49 it's ovbious that a number of 50 digits won't fit in a double. Such number wouldn't even fit in long long. You need other method... Just use it to solve this problem. del Edited by author 06.01.2015 21:37 var a:array[1..20] of integer; n,n1:string; x,k,i,s,code:integer; begin read(n); x:=length(n); k:=x div 3; if x mod 3>0 then k:=k+1; for i:=1 to k do begin n1:=copy(n,x-3*i+1,3); val(n1,k,code); a[i]:=k; delete(n,x-2*i,3); end; k:=x div 3; if x mod 3>0 then k:=k+1; if k div 2=0 then begin a[1]:=-1*a[1]; a[3]:=-1*a[3]; a[5]:=-1*a[5]; a[7]:=-1*a[7]; a[9]:=-1*a[9]; a[11]:=-1*a[11]; a[13]:=-1*a[13]; a[15]:=-1*a[15]; a[17]:=-1*a[17]; end else begin a[2]:=-1*a[2]; a[4]:=-1*a[4]; a[6]:=-1*a[6]; a[8]:=-1*a[8]; a[10]:=-1*a[10]; a[12]:=-1*a[12]; a[14]:=-1*a[14]; a[16]:=-1*a[16]; end; for i:=1 to 17 do s:=s+a[i]; write(abs(s mod 7)); end. what's that means? Output limit exceeded. Dear Java Coders, you will solve this problem so easy, if you know what is BigInteger means. And? As you can see from the problem statistics, many people did it in 0.001s and some of them - with 117 kb =) If you use BigInteger the program is e-a-s-y :) Got AC from the first try :) Not only Java, but Python and Ruby as well. |
|
|