simon3322 发表于 2012-8-20 08:55:44

ACM趣题天天练 1 Quicksum

本帖最后由 simon3322 于 2012-8-20 09:06 编辑

QuicksumTime Limit: 2 Seconds      Memory Limit: 65536 KB A checksum is an algorithm that scans a packet of data and returns a singlenumber. The idea is that if the packet is changed, the checksum will alsochange, so checksums are often used for detecting transmission errors,validating document contents, and in many other situations where it is necessaryto detect undesirable changes in data.
For this problem, you will implement a checksum algorithm called Quicksum. AQuicksum packet allows only uppercase letters and spaces. It always begins andends with an uppercase letter. Otherwise, spaces and letters can occur in anycombination, including consecutive spaces.
A Quicksum is the sum of the products of each character's position in thepacket times the character's value. A space has a value of zero, while lettershave a value equal to their position in the alphabet. So, A=1, B=2, etc.,through Z=26. Here are example Quicksum calculations for the packets "ACM" and "MID CENTRAL":
      ACM: 1*1+ 2*3 + 3*13 = 46
      MID CENTRAL: 1*13 + 2*9 + 3*4 + 4*0 + 5*3 + 6*5 + 7*14 + 8*20 + 9*18 + 10*1 + 11*12 = 650
Input: The input consists of one or more packets followed by a linecontaining only # that signals the end of the input. Each packet is on a line byitself, does not begin or end with a space, and contains from 1 to 255characters.
Output: For each packet, output its Quicksum on a separate line in theoutput.

Example Input:Example Output:
ACM
MID CENTRAL
REGIONAL PROGRAMMINGCONTEST
ACN
A C M
ABC
BBC
#
46
650
4690
49
75
14
15


源自:http://acm.zju.edu.cn/ 2812题

ACM趣题天天练,热烈欢迎论坛各位程序达人果断AC。每天前十名AC并提交自己代码都有红包相送!更有可能认识更多热爱编程喜欢编程的朋友!期待你的代码!

注:如果有提交代码的请在编辑中使用高级—>添加代码段





maxOrder石 发表于 2012-8-20 10:01:54

本帖最后由 maxOrder石 于 2012-8-20 10:36 编辑

检测出non-zero exit code 求建议
#include<stdio.h>
#define Max 20
int main()
{
   int value(char);
   int lines={0};
   char c;
   int i=1;
   int position=1;
   printf("intput:\n");
   while((c=getchar())!='#')
   {
      if(' '==c||c>'=A'&&c<='Z'&&c!='\n')
      {
      lines=lines+position*value(c);
      position++;
      }
      else
      if('\n'==c)
      {
         position=1;
         i++;
      }
      else
      {
          printf("error:input maybe include other invalid words\n");
          break;
      }
   }
printf("output:\n");
int j=1;
for(;j<i;j++)
{
   printf("%d\n",lines);
}
   

}
int value(char c)
{
   if(' '==c)
   {
    return 0;
    }
    else
   {
    return c-'A'+1;
   }
}

maxOrder石 发表于 2012-8-20 10:06:30

那个什么时间是什么?还有memory?我的怎么看?

simon3322 发表于 2012-8-20 10:18:47

maxOrder石 发表于 2012-8-20 10:01 static/image/common/back.gif
楼主,你看怎么样?还行的话,记得给奖励哦!
#include
#define Max 20


第15行我这边编译时候报错,请再查一下

maxOrder石 发表于 2012-8-20 10:23:12

浙大检测说non-zero exitcode

maxOrder石 发表于 2012-8-20 10:30:13

能替我解释一遍要求吗?

simon3322 发表于 2012-8-20 10:30:49

maxOrder石 发表于 2012-8-20 10:23 static/image/common/back.gif
浙大检测说non-zero exitcode

不清楚啊,你可以在浙大网上先AC

maxOrder石 发表于 2012-8-20 10:34:02

maxOrder石 发表于 2012-8-20 10:01 static/image/common/back.gif
检测出non-zero exit code 求建议
#include
#define Max 20


我用gcc的,然后手打出来的,我运行没错啊

maxOrder石 发表于 2012-8-20 10:35:45

本帖最后由 maxOrder石 于 2012-8-20 10:41 编辑

simon3322 发表于 2012-8-20 10:18 static/image/common/back.gif
第15行我这边编译时候报错,请再查一下
lines中括号i=lines中括号i+...
原来用虚拟机的gcc,然后手打上去,固然出错了

extlpf 发表于 2012-8-20 11:54:34

ZOJ上已AC

import java.util.Scanner;

public class Main {
      public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                int quicksum;
                String pkg;
                char ch; int code;
                while(sc.hasNext())
                {
                        pkg = sc.nextLine();
                        if(pkg.charAt(0) == '#') break;
                        quicksum = 0;
                        for(int i = 0; i < pkg.length(); i++)
                        {
                              ch = pkg.charAt(i);
                              if(ch == ' ')
                              {
                                        code = 0;
                              }
                              else
                              {
                                        code = (int)(ch - 'A' + 1);
                              }
                              quicksum += code * (i + 1);
                        }
                        System.out.println(quicksum);
                }
      }
}

yel_hb 发表于 2012-8-20 12:56:37

#include <iostream>
#include <string>
using namespace std;

int main()
{
        string str;
        int i,len,sum;
        while(getline(cin,str))
        {
                if(str == '#')
                {
                        break;
                }
                else
                {
                        sum = 0;
                        len = str.length();
                        for(i = 0; i < len; ++i)
                        {
                                if(str != ' ')
                                {
                                        sum = sum + (i + 1) * (str - 64);
                                }
                        }

                        cout << sum << endl;
                }
        }
        return 0;
}

maxOrder石 发表于 2012-8-20 12:59:22

我也太面向过程了

sunhongbo 发表于 2012-8-20 16:58:04

自从复试不要求上机之后还久。。好久没练过ACM了。。罪过

lavorange 发表于 2012-8-20 19:04:09

红包送什么
来自:软院网·中科大 Android客户端

寄居蟹a 发表于 2012-8-20 20:58:57

extlpf 发表于 2012-8-20 11:54 static/image/common/back.gif
ZOJ上已AC

import java.util.Scanner;


哇~~~{:10_458:}很给力嘛!

primi 发表于 2012-8-20 21:48:22

ZOJ好慢啊,提交十几分钟了还不出结果

primi 发表于 2012-8-20 22:04:08

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string str;
    int ans;
    while(getline(cin, str))
    {
      if(str == "#")
            break;
      ans = 0;
      for(int i = 0; i < str.length(); i++)
            if(str <= 90 && str >= 65)
                ans += (i + 1) * (str - 'A' + 1);
      cout<<ans<<endl;
    }
    return 0;
}

WXL 发表于 2012-8-20 23:23:19

额,看到这亚历山大~

fghhslk 发表于 2012-8-21 00:59:07

庆祝电脑回来了。。。AC一记
#include <iostream>
#include <string>
using namespace std;

int main()
{
        string str;
        int sum;
        while(getline(cin,str)) {
                sum = 0;
                if (str == "#")
                        break;
                for (int i = 0; i < str.length(); i++) {
                        if (str < 'A' && str > 'Z')
                                cout << "input error!" <<endl;
                        if (str != ' ') {
                                sum += (str - 'A' + 1) * (i + 1);
                        }
                }
                cout << sum << endl;
        }
        return 0;
}

justcx 发表于 2012-8-21 23:30:05


#include<iostream>
using namespace std;
int main()
{
char c;
int sum=0;
while(cin.getline(c,256))
{
if(c=='#')
break;
for(int i=0;c!='\0';i++)
{
if(c!=' ')
sum+=(i+1)*(c-64);
}
cout<<sum<<endl;
sum=0;
}
return 0;
}

在家没事做,感谢楼主,已ACfile:///C:/Documents%20and%20Settings/Administrator/Application%20Data/Tencent/Users/894114106/QQ/WinTemp/RichOle/7B0%25QGB5WU]4%25VTI4281@5W.jpg

rxgalpha 发表于 2012-8-22 15:48:53

正在学perl,来个perl版本的~
#!/user/bin/perl
use warnings;
use diagnostics;

while(<STDIN>){
        chomp;
        last if($_ eq '#');
        my $sum;
        for(my $i=1;$i<=length;$i++){
                next if charAt($_,$i-1)=~/^\s$/;
                $sum+=$i*(ord(charAt($_,$i-1))-ord('A')+1);
        }
        message($sum);
}

sub charAt{
        my ($str, $n) = @_;
        return substr($str,$n,1);
}
sub message{
    my $m = shift or return;
    print("$m\n");
}


页: [1]
查看完整版本: ACM趣题天天练 1 Quicksum