约瑟夫环,语言翻译一下,有没有更但疼的翻译?

旭子 2012-09-11 07:50:52
以下是java实现的版本:


import java.io.*;
import java.util.*;

public class YSF {

static LinkedList<Integer> ysf = new LinkedList<Integer>();
static int m;
static int pos = -1;

public static void main(String[] args) throws Exception {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));

String line = rd.readLine();

m = Integer.parseInt(rd.readLine());

for (String str : line.split(" ")) {
try {
ysf.add(Integer.parseInt(str));
} catch (Exception e) {
}
}

for (;ysf.size() != 1; ysf.remove(pos--)) {
pos = (pos + m) % ysf.size();
m = ysf.get(pos);
System.out.print(m + ",");
}

System.out.print(ysf.get(0) + "\n");
}
}


用C语言翻译以后(注释的每一句话是java的后面紧接着是C语言实现):


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int ysf[100];
int total = 0;

char line[256];
char line2[256];
int m;

int pos;

int ysf_get(int index)
{
return ysf[index];
}

void ysf_remove(int index)
{
int *p = ysf + index;
int *q = p + 1;
memmove(p, q, (total - index - 1) * sizeof (int));
total--;
}

int main(void)
{
//BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
// none

//String line = rd.readLine();
fgets(line, sizeof (line), stdin);

//m = Integer.parseInt(rd.readLine());
m = strtol(fgets(line2, sizeof (line2), stdin), NULL, 10);

//for (String str : line.split(" ")) {
// try {
// ysf.add(Integer.parseInt(str));
// } catch (Exception e) {
// }
//}
char *s, *str;
for (s = line; NULL != (str = strtok(s, " ")); s = NULL)
{
ysf[total++] = strtol(str, NULL, 10);
}

//for (;ysf.size() != 1; ysf.remove(pos--)) {
// pos = (pos + m) % ysf.size();
// m = ysf.get(pos);
// System.out.print(m + ",");
//}
for (;total != 1; ysf_remove(pos))
{
pos = (pos + m) % total;
m = ysf_get(pos);
printf("%d,", m);
}

//System.out.print(ysf.get(0) + "\n");
printf("%d\n", ysf_get(0));

return 0;
}
...全文
292 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dtvsking 2012-09-14
  • 打赏
  • 举报
回复
旭子 2012-09-12
  • 打赏
  • 举报
回复
C#吐血实现。。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class YSF
{

static List<int> ysf = new List<int>();
static int m;
static int pos = -1;

public static void Main(string[] args)
{

string line = System.Console.ReadLine();

m = Convert.ToInt32(System.Console.ReadLine());

foreach (string str in line.Split(' '))
{
try
{
ysf.Add(Convert.ToInt32(str));
}
catch (Exception)
{
}
}

for (; ysf.Count != 1; ysf.RemoveAt(pos--))
{
pos = (pos + m) % ysf.Count;
m = ysf[pos];
System.Console.Write(m + ",");
}

System.Console.Write(ysf[0] + "\n");

System.Console.ReadKey(true);
}
}
}

旭子 2012-09-12
  • 打赏
  • 举报
回复
所以等高手啊。。

[Quote=引用 6 楼 的回复:]

都不好。
[/Quote]
鱼欲遇雨 2012-09-11
  • 打赏
  • 举报
回复
这。。。。。。c的都出来了, 还怕没有c++De!
旭子 2012-09-11
  • 打赏
  • 举报
回复
有翻译成C++ C# javascript python perl lua 的吗?
z569362161 2012-09-11
  • 打赏
  • 举报
回复
都不好。
旭子 2012-09-11
  • 打赏
  • 举报
回复
C#实现


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class YSF
{

static List<int> ysf = new List<int>();
static int m;
static int pos = -1;

public static void Main(string[] args)
{

string line = System.Console.ReadLine();

m = Convert.ToInt32(System.Console.ReadLine());

foreach (string str in line.Split(' '))
{
try
{
ysf.Add(Convert.ToInt32(str));
}
catch (Exception)
{
}
}

for (; ysf.Count != 1; ysf.RemoveAt(pos--))
{
pos = (pos + m) % ysf.Count;
m = ysf[pos];
System.Console.Write(m + ",");
}

System.Console.Write(ysf[0] + "\n");

System.Console.ReadKey(true);
}
}
}
旭子 2012-09-11
  • 打赏
  • 举报
回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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