有谁能帮我解这道ACM,我都试了N次了

youzelin 2008-06-01 09:46:03
原题:http://acm.timus.ru/problem.aspx?space=1&num=1001

有谁能把这道题用 C# 做出来?我都试了 N 次了,真不知道还有什么用例没测到。

下面是我的代码:


using System;
using System.Text;
using System.Text.RegularExpressions;

namespace UralACM
{
class Program
{
static void Main(String[] args)
{
Problems.ReverseRoot_1001();
}
}
static class Problems
{
public static void ReverseRoot_1001()
{
MatchCollection matchCollection = Regex.Matches(Console.In.ReadToEnd(), @"\d+(\.\d+)?");
for (int i = matchCollection.Count - 1; i >= 0; i--)
{
Console.WriteLine("{0:F4}", Math.Sqrt(Double.Parse(matchCollection[i].Value)));
}
}
}
}
...全文
314 41 打赏 收藏 转发到动态 举报
写回复
用AI写文章
41 条回复
切换为时间正序
请发表友善的回复…
发表回复
youzelin 2008-06-09
  • 打赏
  • 举报
回复
呵呵,谢谢!
youzelin 2008-06-07
  • 打赏
  • 举报
回复
thank you!
microblue 2008-06-07
  • 打赏
  • 举报
回复
[Quote=引用 36 楼 wuyi8808 的回复:]
整理了一下,这样就Accepted了:
(看来关键在这一句:Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;)

[/Quote]

看来关键在这一句:Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

俄罗斯乌拉尔大学在线题库的服务器是俄罗斯的服务器,如果省略了这一句,就相当于将该行改为:
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("ru-RU");
这样,该程序的输出将使用逗号(,)而不是句号(.)作为小数点。
wuyi8808 2008-06-06
  • 打赏
  • 举报
回复
奇怪,这也 Accepted
(这和36楼的代码对于 999999999999899072 ~ 999999999999900000 之间的输入,输出是不同的,看来acm的测试集中没有这之间的输入)

using System;
using System.Threading;
using System.Globalization;
using System.Text.RegularExpressions;

class Program
{
static void Main()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
string[] nums = Regex.Split(Console.In.ReadToEnd().Trim(), @"\s+");
for (int i = nums.Length - 1; i >= 0; i--)
Console.WriteLine("{0:F4}", Sqrt(ulong.Parse(nums[i])));
}

static decimal Sqrt(ulong x)
{
decimal r1 = (decimal)Math.Sqrt((double)x);
r1 = decimal.Round(r1, 4);
decimal r2 = r1 * r1;
if (r2 == x) return r1;
decimal r3 = r1 + (r2 < x ? 0.0001M : -0.0001M);
decimal r4 = r3 * r3;
return Math.Abs(r2 - x) <= Math.Abs(r4 - x) ? r1 : r3;
}
}
wuyi8808 2008-06-06
  • 打赏
  • 举报
回复
保留4位小数的话,

已知:
Sqrt(999999999999899071) = 999999999.9999
Sqrt(999999999999899072) = 999999999.9999
Sqrt(999999999999900000) = 999999999.9999
Sqrt(999999999999900001) = 1000000000.0000

然而:
System.Math.Sqrt(999999999999899071) = 999999999.9999
System.Math.Sqrt(999999999999899072) = 1000000000.0000
System.Math.Sqrt(999999999999900000) = 1000000000.0000
System.Math.Sqrt(999999999999900001) = 1000000000.0000


也就是说,对于 999999999999899072 ~ 999999999999900000 之间的输入,acm的判断是为正确的。
wuyi8808 2008-06-06
  • 打赏
  • 举报
回复
整理了一下,这样就 Accepted 了:
(看来关键在这一句:Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;)

using System;
using System.Threading;
using System.Globalization;
using System.Text.RegularExpressions;

class Program
{
static void Main()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
string[] nums = Regex.Split(Console.In.ReadToEnd().Trim(), @"\s+");
for (int i = nums.Length - 1; i >= 0; i--)
Console.WriteLine("{0:F4}", Math.Sqrt(ulong.Parse(nums[i])));
}
}
wuyi8808 2008-06-06
  • 打赏
  • 举报
回复
楼上的太有帮助了,这样就 Accepted 了:

using System;
using System.Text.RegularExpressions;

class Program
{
static void Main()
{
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
string[] nums = Regex.Split(Console.In.ReadToEnd().Trim(), @"\s+");
for (int i = nums.Length - 1; i >= 0; --i)
try { Console.WriteLine("{0:F4}", Math.Sqrt(ulong.Parse(nums[i]))); }
catch (Exception) {}
}
}
lawbc 2008-06-06
  • 打赏
  • 举报
回复
还不知道题目是什么意思,能不能用中文说说,谢谢
wuyi8808 2008-06-06
  • 打赏
  • 举报
回复
已知:
Sqrt(999999999999900000) = 999999999.99994999999999999875
Sqrt(999999999999900001) = 999999999.99995000049999999875003

保留4位小数:
Sqrt(999999999999900000) = 999999999.9999
Sqrt(999999999999900001) = 1000000000.0000

如果用double,达不到这种精度,不知acm的标准答案是不是按这个来计算的。
22楼Accepted的C代码是直接用double计算的,似乎精度不够。
youzelin 2008-06-06
  • 打赏
  • 举报
回复
在这个ACM题目的讨论中有这样一个帖子,此人称他后来是过了的,但没有贴出最后的代码,之前的代码不知是否有点帮助?
http://acm.timus.ru/forum/thread.aspx?space=1&num=1001&id=14480&upd=633038635821350258
darkarthur 2008-06-06
  • 打赏
  • 举报
回复
做ACM一直觉得C的效率高
用IO流的语句直接改成scanf printf可能就过了
用cout cin就是不能过
估计C#要过这题也是有点这样的味道
youzelin 2008-06-05
  • 打赏
  • 举报
回复
谢谢 wuyi8808,大家继续,希望能有人做出来,谢谢大家!
yoyoalphax 2008-06-05
  • 打赏
  • 举报
回复
所以说...做acm题用C#提交我还是第一次听说...以前的提交系统都不支持的说...
wuyi8808 2008-06-05
  • 打赏
  • 举报
回复
这个版本也不对,真没办法了。

using System;
using System.Collections.Generic;

class Test
{
static void Main()
{
int c;
List<ulong> a = new List<ulong>();
while ((c = Console.In.Read()) != -1)
{
if (c >= '0' && c <= '9')
{
ulong n = 0;
do
{
n = n * 10UL + (ulong)(c - '0');
} while ((c = Console.In.Read()) != -1 && c >= '0' && c <= '9');
a.Add(n);
if (c == -1) break;
}
}
for (int i = a.Count - 1; i >= 0; i--)
{
Console.WriteLine("{0:F4}", Sqrt(a[i]));
}
}

static decimal Sqrt(ulong x)
{
decimal r1 = (decimal)Math.Sqrt((double)x);
r1 = decimal.Round(r1, 4, MidpointRounding.AwayFromZero);
if (x < 1E14M) return r1;
decimal r2 = r1 * r1;
if (r2 == x) return r1;
decimal r3 = r1 + (r2 < x ? 0.0001M : -0.0001M);
decimal r4 = r3 * r3;
return Math.Abs(r2 - x) < Math.Abs(r4 - x) ? r1 : r3;
}
}
wuyi8808 2008-06-05
  • 打赏
  • 举报
回复
C#到底怎么了,这个程序逐个从标准输入读入字符,还是 Wrong answer,郁闷。

using System;
using System.IO;
using System.Collections.Generic;

class Test
{
static void Main()
{
int c;
TextReader stdin = Console.In;
List<ulong> a = new List<ulong>();
while ((c = stdin.Read()) != -1)
{
if (c >= '0' && c <= '9')
{
ulong n = 0;
do
{
n = n * 10UL + (ulong)(c - '0');
} while ((c = stdin.Read()) != -1 && c >= '0' && c <= '9');
a.Add(n);
if (c == -1) break;
}
}
for (int i = a.Count - 1; i >= 0; i--)
{
Console.WriteLine("{0:F4}", Sqrt(a[i]));
}
}

static double Sqrt(ulong x)
{
return Math.Sqrt((double)x);
}
}
wuyi8808 2008-06-04
  • 打赏
  • 举报
回复
这段C代码也是Accepted.

#include <stdio.h>
#include <math.h>

main()
{
double *a = malloc(300000*sizeof(double));
double temp;
int n = 0;
while (scanf("%lf", &temp) != EOF)
{
*a = sqrt(temp);
a++;
n++;
}
while (n--)
{
a--;
printf("%.4lf\n", *a);
}
}
wuyi8808 2008-06-04
  • 打赏
  • 举报
回复
var a:array[1..1000000] of int64;
i,j:longint;
begin
i:=0;
while not seekeof do begin
inc(i);
read(a[i]);
end;
for j:=i downto 1 do
writeln(sqrt(a[j]):0:4);
end.
长弓大侠 2008-06-04
  • 打赏
  • 举报
回复
关注
wuyi8808 2008-06-04
  • 打赏
  • 举报
回复
这是Pascal的,我试了一下,Accepted.
Posted by hleb December 12, 2007 14:22

var a:array[1..1000000] of int64;
i,j:longint;
begin
{$IFNDEF ONLINE_JUDGE}
assign(input,'input.txt');
reset(input);
assign(output,'output.txt');
rewrite(output);
{$ENDIF}
i:=0;
while not seekeof do begin
inc(i);
read(a[i]);
end;
for j:=i downto 1 do
writeln(sqrt(a[j]):0:4);
{$IFNDEF ONLINE_JUDGE}
close(input);
close(output);
{$ENDIF}
end.
youzelin 2008-06-03
  • 打赏
  • 举报
回复
提交网址:http://acm.timus.ru/submit.aspx?space=1&num=1001
(你需要先随便注册一个,注册之后有一个JudgeID)
加载更多回复(17)

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧