标题:那位高手帮我将这个pascal程序翻译成C
只看楼主
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
结帖率:79.37%
已结贴  问题点数:20 回复次数:4 
那位高手帮我将这个pascal程序翻译成C
program ss;

var
   v:array[1..100] of longint;
   s,f,i,j:longint;

begin
   assign(input,'game.in');reset(input);
   assign(output,'game.out');rewrite(output);
   readln(s, f);
   if (f=4)or(f=7) then
   begin
      dec(f);
      for i:=1 to s do read(v[s-i+1]);
   end else
      for i:=1 to s do read(v[i]);
   j:= 0;
   case f of
     1: begin
            j:=s;
            for i:=1 to s-3 do
               if (v[i]=v[i+1])and(v[i]=v[i+2])and(v[i]=v[i+3]) then inc(j);
         end;
     2: begin
            for i:=1 to s-1 do
               if v[i]=v[i+1] then inc(j);
         end;
     3: begin
            for i:=1 to s-1 do
               if v[i]=v[i+1]+1 then inc(j);
            for i:=1 to s-2 do
               if (v[i]=v[i+1])and(v[i]=v[i+2]-1) then inc(j);
         end;
     5: begin
            for i:=1 to s-2 do
               if ((v[i]-v[i+1]=1)or(v[i]-v[i+1]=0))and(v[i]=v[i+2]) then inc(j);
            for i:=1 to s-1 do
               if abs(v[i]-v[i+1])=1 then inc(j);
         end;
     6: begin
            for i:=1 to s-2 do
               if (v[i+1]=v[i+2])and((v[i+1]-v[i]=1)or(v[i+1]-v[i]=0)) then inc(j);
            for i:=1 to s-1 do
               if (v[i]=v[i+1])or(v[i]=v[i+1]+2) then inc(j);
         end;
   end;
   writeln(j);
close(input);
close(output);
end.
搜索更多相关主题的帖子: pascal 翻译 
2010-09-21 18:32
hiroki
Rank: 2
等 级:论坛游民
帖 子:33
专家分:74
注 册:2008-10-27
得分:6 
你这个代码是delphi  有一些是delphi的内部函数 请你解释一下assign这个 什么意思   要不翻译不准确了

我的电脑HK最厉害!
我在电脑上没有阻碍!!
2010-09-21 19:37
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
得分:0 
不是delphi,是pascal代码

欢迎来到我的博客:http://blog..cn/noisunyuhong
2010-09-22 08:40
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
得分:6 
看来没人帮版主翻译啊

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2010-09-23 11:51
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
得分:6 
// 不知道对不对
// program ss

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

#define inc(x) (++(x))
#define dec(x) (--(x))

int main(void)
{
    long v[100];
    long s, f, i, j;

    FILE *finput, *foutput;
    finput = fopen("game.in", "r");
    foutput = fopen("game.out", "w");
    if (finput == NULL)
        exit(1);

    fscanf(finput, "%ld %ld", &s, &f);
    if (f==4 || f==7)
    {
        dec(f);
        for (i=0; i < s; i++)
            fscanf(finput, "%ld", &v[s-i]);
    } else {
        for (i=0; i < s; i++);
            fscanf(finput, "%ld", &v[i]);
    }
    j=0;
    switch (f) {
      case 1:
        j=s;
        for (i=0; i < s-3; i++)
            if(v[i]==v[i+1] && v[i]==v[i+2] && v[i]==v[i+3])
                inc(j);
      case 2:
        for (i=0; i < s-1; i++)
            if (v[i] == v[i+1])
                inc(j);
      case 3:
        for (i=0; i<s-1; i++)
            if (v[i]==v[i+1]+1)
                inc(j);
        for (i=0; i<s-2; i++)
            if (v[i]==v[i+1] && v[i]==v[i+2]-1)
                inc(j);
      case 5:
        for (i=0; i<s-2; i++)
            if ( (v[i]-v[i+1]==1 || v[i]-v[i+1]==0) && v[i]==v[i+2] )
                inc(j);
        for (i=0; i<s-1; i++)
            if (abs(v[i]-v[i+1]) == 1)
                inc(j);
      case 6:
        for(i=0; i<s-2; i++)
            if (v[i+1]==v[i+2] && v[i+1]-v[i]==1 || v[i+1]-v[i]==0)
                inc(j);
        for (i=0; i<s-1; i++)
            if (v[i]==v[i+1] || v[i]==v[i+1]+2)
                inc(j);
    }
    fprintf(foutput, "%ld\n", j);
    fclose(finput);
    fclose(foutput);
    return 0;
}

[ 本帖最后由 cosdos 于 2010-9-23 13:26 编辑 ]

—>〉Sun〈<—
2010-09-23 13:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-321074-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.565931 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved