鱼C论坛

 找回密码
 立即注册
查看: 4494|回复: 13

题目42:这个英语词列表里共有多少个三角形词?

[复制链接]
发表于 2020-8-11 17:29:32 | 显示全部楼层
162

Process returned 0 (0x0)   execution time : 0.054 s
Press any key to continue.
又可以利用Excel VBA处理源文件使之易于输入
  1. Public Sub PE42()
  2.     Open "C:\Users\zcfcf\Desktop\words.txt" For Input As #1
  3.     Dim s As String
  4.     Dim cnt As Integer
  5.     cnt = 1
  6.     Do Until EOF(1)
  7.         Input #1, s
  8.         Range("a" & CStr(cnt)).Value = s
  9.         cnt = cnt + 1
  10.     Loop
  11.     Close #1
  12. End Sub
复制代码

接着利用ASCII码天然的编码特性计算单词的值,并加以统计
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<string>
  4. using namespace std;

  5. const int a[] = {1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136,
  6.                 153, 171, 190, 210, 231, 253, 276, 300, 325, 351};

  7. int calc(const string & s){
  8.   int res = 0;

  9.   for (int i = 0;i < s.length();i++)
  10.     res += s[i] - 'A' + 1;

  11.   return res;
  12. }

  13. int main(){
  14.   freopen("i.in","r",stdin);
  15.   string s;
  16.   int cnt = 0;

  17.   while(cin >> s){
  18.     int t = calc(s);
  19.     for (int i = 0;a[i] <= 351;i++)
  20.       if (t == a[i])  { cnt++;    break; }
  21.   }

  22.   cout << cnt << endl;
  23.   return 0;
  24. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-26 02:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表