Я уже просто не понимаю, что не так... #include <iostream> #include <iomanip> using namespace std; int main() { long double arr[100000]; for (int x = 0; x<100000; x++) { cin >> arr[x]; } for (int y = 100000; y--;) { arr[y] = sqrt(arr[y]); cout << fixed << setprecision(4) << (arr[y]) << endl; } return 0; } How did you receive a magic number "100000"? Task example, for example, contains 4 numbers. Your program should process example. My program processes the example of 4 digits, but still wrong answer Could you read task description? It should help a bit. Amount of numbers in the input isn't fixed. You should read numbers from input until EOF. By task description maximum input size IN BYTES is 256Kb. You should estimate maximum input size in numbers yourself. #include<math.h> #include<iostream> #include <iomanip> using namespace std; #include<cstring> #include<stdlib.h> template <class T> class lstack { public: lstack(int k) { blink=(T*)malloc(sizeof(T)*k); len=k; cnt=0; } ~lstack() { free(blink); } T* pop(T* ptr) { if(cnt==0) return NULL; cnt--; memcpy(ptr,(blink+cnt),sizeof(T)); return ptr; } bool push(const T* ptr) { if(cnt==len) return false; memcpy((blink+cnt),ptr,sizeof(T)); cnt++; return true; } private: T* blink; int len,cnt; }; int main() { lstack<double> s(1000000);//When I use 100000,it's wrong.... double a; while(cin>>a) { s.push(&a); } while(s.pop(&a)!=NULL) { cout<<setprecision(4) <<fixed<<sqrt(a)<<endl; } return 0; } 1) We don't need. By task description max input size is 256Kb. It means max numbers count is 128K. 2) google STL containers - vector, queue, deque, stack. I have a hard time submitting my first piece of code. Compilation error appeared and I have no idea why. The following is my code. import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Created by Tanya on 2016/9/5. */ public class ReverseRoot2 { Scanner in; PrintWriter out; public void work()throws Exception { List<Double> numlist = new ArrayList<Double>(); in = new Scanner(System.in); String line = ""; while(in.hasNextLine() && !(line = in.nextLine()).equals("")) { String[] strnums = line.split(" "); for(String strnum : strnums) { if(strnum.matches("[0-9]+\\.*[0-9]*")) { numlist.add(Double.parseDouble(strnum)); } } } out = new PrintWriter(System.out); int len = numlist.size(); for(int i = len - 1;i>=0;i--) { out.print(Math.sqrt(numlist.get(i).doubleValue()) + "\t"); } out.flush(); } static public void main(String[] args)throws Exception { ReverseRoot2 rr = new ReverseRoot2(); rr.work(); } } After you submit your code, if it gets "compilation error", within the next 15 minutes it is underlined — you can click on it and see the error message. #include<iostream> #include<cmath> #include<iomanip> using namespace std; int main() { long long a[100000] = {0}; int i = 0; while (cin >> a[i]) { i++; } for (int j = i-1; j >= 0; j--) { cout << fixed << setprecision(4) << sqrt(a[j]) << endl; } } How did you think "100,000" is enough for the input? You shouldn't allocate big arrays on stack. You should allocate big array in heap or use vector. Program Pr1; var i:longint; A:real; k,n:longint; begin readln(n,k); for i:= n downto k do begin A:=sqrt(i); writeln(A:0:4) end; end. Sample numbers have different value of math.sqrt() in last digit. Why? This task is not so big level for hidden problem. sample result: 2297.0716 936297014.1164 0.0000 37.7757 my result: 2297.0715 936297024.0000 0.0000 37.7757 import java.io.PrintWriter; import java.util.Scanner; import java.util.*; public class AlgorithmTAsk { public static void main(String[] arg){ Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); ArrayList<Float> f = new ArrayList<Float>(); while (in.hasNextLong()) { f.add((float) Math.sqrt(in.nextLong())); } for(int i = f.size()-1;i>=0;i--){ out.printf("%8.6f\n",f.get(i)); } out.flush(); } } Edited by author 19.04.2016 19:45 Try double instead of float import java.util.Scanner; import java.text.DecimalFormat; import java.lang.StringBuffer; import java.lang.Math; public class Timus2{ public static void main(String[] args){ Scanner S = new Scanner(System.in); DecimalFormat df = new DecimalFormat("#.0000"); StringBuffer result = new StringBuffer(); while(S.hasNext()){ result.append(df.format(Math.sqrt(S.nextDouble())) + "\n"); } S.close(); System.out.println(result); } } 1) Any advantages of using "StringBuffer result"? Is it really faster then just print result line by line? 2) Show here expected output, your program output, compare. #include <iostream> #include <math.h> #include <algorithm> #include <vector> using namespace std; int main() { vector<long long int>v; long long int b; while (cin >> b) v.push_back(b); reverse(v.begin(),v.end()); for (long long int k: v) cout << fixed << sqrt(k) << "\n"; return 0; } Can you please explain - why the code below generates Runtime error during checking ? Thanks, import java.util.*; public class Task0002 { public static void main(String[] args) { Scanner scin = new Scanner(System.in); ArrayList<Double> values = new ArrayList<Double>(); String strIn; int lenScin = 0; do { strIn = scin.nextLine(); lenScin += strIn.length(); strIn = strIn.trim(); strIn = strIn.replaceAll(" ", " ");
// Process the line if(strIn.length()>0) { for(String s: strIn.split(" ")) { values.add(Double.parseDouble(s)); }; };
} while(scin.hasNextLine() && lenScin <= 262144); scin.close();
for(int i = values.size()-1; i>=0; i--) { double val = (double)values.get(i); System.out.println(Math.sqrt(val) + " "); };
} } Edited by author 02.08.2016 12:29 May be you also need to replace '\n' ? #include<iostream> #include<iomanip> #include<math.h> using namespace std; int main() { unsigned long long n; double a[128*1024]; int i=0; while(cin>>n) { a[i]=sqrt(n); i++; } cout<<setprecision(4)<<fixed; for(int b=i-1;b>=0;b--) { cout<<a[b]<<endl; } return 0; } Edited by author 01.08.2016 20:26 #include <stdio.h> #include<math.h> int main() { int n; double t[100]; scanf("%d", &n); int i; for(i=1; i<=n;i++){ scanf("%lf", &t[i]); } for(i=n; i>0; i--){ printf("%lf\n",sqrt(t[i])); } return 0; } Please read task and example carefully. 1) scanf("%d", &n); - do you see n in example input? 2) double t[100]; - where in task did you find "100" limitation? #include <iostream> #include <math.h> #include <iomanip> using namespace std; int main() { double a[100000]; int i, n; cin >>n; for (i=1; i<=n; ++i) { cin>>a[i]; } for (i=1; i<=n; ++i) { a[i] = sqrt(a[i]); } for (i=n; i>=1; --i) { cout << fixed << setprecision(4) << (a[i]) << endl; } } Прочитай внимательно графу исходные данные, у тебя первое число задаёт количество чисел которое ты будешь вводить, а там такого нету using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Problem_1001 { class Program { static void Main(string[] args) { ulong[] input = new ulong[4]; for (int i = 0; i < input.Length; i++) { input[i] = ulong.Parse(Console.ReadLine()); } for (int i = input.Length - 1; i >= 0; i--) {
Console.WriteLine(String.Format("{0:F4}",Math.Sqrt(input[i]))); } //Console.ReadLine(); } } } You think there are exactly 4 numbers in the input. Also you think there is 1 number per line. Please show task fragment describing input format as you think. Edited by author 16.06.2016 21:02 import sys; import math; class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items) - 1] def size(self): return len(self.items) size = 0 intSize = sys.getsizeof(int()); s = Stack(); while (size<100): line = input(); if (not line.isspace()): values = [int(s) for s in line.split() if s.isdigit()]; size += intSize; for value in values: s.push(math.sqrt(value)); while (s.size()>0): print("%.4f" % (s.pop())); > while (size<100): How did you receive magic number "100"? Shouldn't you read input until EOF? var a, x: extended; begin readln (a); x:= sqrt (a); writeln (x); end. Выдаёт неправильный ответ. Firstly you should read task carefully. How many numbers are in input? How many numbers your program process? var a,b,c,d,e,f,g,h : real begin read(a,b,c,d); e := sqrt(a); f := sqrt(b); g := sqrt(c); h := sqrt(d); writeln(h:0:4); writeln(g:0:4); writeln(f:0:4); writeln(e:0:4); end. why is my answer wrong? Have you read task? Why no? You should try. >Input > The input stream contains a set of integer numbers. The numbers are > separated by any number of spaces and line breaks. A size of the input > stream does not exceed 256 KB. Edited by author 13.04.2016 13:14 i racked my brain, but i unable found solution so can i look your source code in pascal? Sure no. 1) Why do you think I know pascal? 2) Problem is 2nd easiest here. You should be able to solve it yourself. What problem do you have? How to read numbers from stream? http://acm.timus.ru/help.aspx?topic=pascal"How to read input data until the end of stream" topic How to estimate max input size? It's about 256K/2 numbers. How to print results in reverse order? Pff. Give up for a while and learn programming. Edited by author 13.04.2016 14:00Where the stop position if there is only one input? I am confused, please explain the problems. #include<stdio.h> #include<math.h> double str[300000]; using namespace std; int main() { double a; int w=0; while(~scanf("%lf",&str[w])) w++; for(int i=w-1;i>=0;i--) printf("%.6f\n",sqrt(str[i])); return 0; } //Don't use cin or cout //str[N],Nshould>=262144 Задача простая. Но вопрос таков: Из правил сайта я понял, что входные данные вводятся только и только через консоль, не считываются с файла. Если это так, то как определить что входные данные уже вбиты?? Из задания понятно, что вводятся целые числа не больше 10 в степени 18, до каких пор будут вводится входные данные? есть какой нибудь стоп кондишн? Чтобы у себя тестировать, можно либо сделать выход при встрече данных, которые заведомо не встречаются, например while not eof do begin ... read(x); if x < 0 then break; ... end; Либо, более хороший способ, использовать IFNDEF ONLINE_JUDGE. По ссылке "руководство" для большинства языков описано, что нужно делать. Is your language is java? You can assume that System.In is stream to real existing file with input data. You should read from System.in (directly or using Tokenizer/Scanner) until you reach EOF. The only stop condition for this task is you've reached EOF. My program works fine in console, but for some reason I get wrong answer on test 9. Could someone point out where is the problem in my code? #include <iostream> #include <vector> #include <math.h> #include <iomanip> using namespace std; int main() { double num; vector<long long> input; int limit = 256 * 1024 / sizeof(long long); for (int k = 0; (cin >> num) && k <= limit; k++) { if (num != (long long)num || num < 0 || num > pow(10.0, 18)) return 0; input.push_back((long long)num); } for (int i = input.size() - 1; i >= 0; i--) cout << fixed << setprecision(4) << sqrt(input[i]) << endl; return 0; } Edited by author 28.03.2016 23:17 Edited by author 28.03.2016 23:18 Edited by author 28.03.2016 23:18 Limit is wrong. Min number size is 2 bytes - "1 ". In common, you shouldn't check if input is correct. It's supposed to be always correct. Reading like: while (cin >> number) { input.push_back(number); } should be enough. If you want to check input (for lulz?) you should fail your program in more visible way - via return not 0, throw something etc. Judge system will return another error, not WA. Edited by author 28.03.2016 23:43 Another note. You read number into double, convert it into long long, convert it back to double and finally call sqrt. Why do you need 2 conversions in the middle? Shouldn't you better use vector<double> input? Sorry for stupid question: what is WA? USE while(cin >> number), DO NOT USE cin.eof() |
|