第八次实验任务

第1题:

#include<stdio.h>     
int f(int n)    
{  int x = 0,i;    
   while(n!=0)      
   {    
    i=n%10;    
    x=x*10+i;    
    n=n/10;    
   }    
   return x;    
}    
 void main()    
{    int a,b,c,d,sum;  
     printf("输入两个-1000~10000的数\n");  
     scanf("%d,%d",&a,&b);    
     if(a<=0) c=-f(-a)  ;  
     else   c=f(a ) ;  
     if(b<=0) d=-f(-b);  
     else   d=f(b );  
     sum=d+c;  
     if(sum<0)printf("%d\n",-f(-sum));  
  
     else         printf("%d\n",f(sum));}  

第2题:
#include<stdio.h>
int f(int i)    
{    int y;    
     if(i==0||i==1) y=1;    
     else y=f(i-1)*i;    
     return(y);    
}    
int sum(int n)    
{   int i;int sum=0; 
    for(i=1;i<=20;i++){   
    sum=sum+f(i);}    
    return sum;}    
void main()    
{    int n;  
     printf("1!+2!+…+20!=%d\n",sum(n));    
}    

第3题:

#include<stdio.h>   
void main()
  
{
	int x;
	while(x!=0){  
      printf("请输入一个正整数\n");  
      int a,i;  
      scanf("%d",&a);  
      if(a<2)  printf("质数是不能小于2,请重新输入");  
      else for(i=2;i<=a;i++)  
      {if(a%i==0)  break;}  
      if(i>=a)   printf("Yes\n");  
      else      printf("No\n");  
      }  
}  


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第一次实验代码: # 导入结巴分词库 import jieba # 打开待分词文件 with open('CDIAL-BIAS-race.txt', 'r', encoding='utf-8') as f: text = f.read() # 使用结巴分词对文本进行分词 seg_list = jieba.cut(text) # 将分词结果用空格连接成字符串 seg_text = ' '.join(seg_list) # 将分词结果写入文件 with open('CDIAL-BIAS-race-seg.txt', 'w', encoding='utf-8') as f: f.write(seg_text) # 打开敏感词汇文件 with open('race.txt', 'r', encoding='utf-8') as f: keywords = f.read().splitlines() # 统计分词结果中的敏感词汇出现次数 freq_dict = {} for word in seg_list: if word in keywords: if word in freq_dict: freq_dict[word] += 1 else: freq_dict[word] = 1 # 输出敏感词汇出现次数 for word in freq_dict: print(word, freq_dict[word]) 第二次实验代码: # 导入结巴分词库 import jieba # 打开待分词文件 with open('CDIAL-BIAS-race.txt', 'r', encoding='utf-8') as f: text = f.read() # 使用结巴分词对文本进行分词 seg_list = jieba.cut(text) # 去重得到词表 word_set = set(seg_list) # 将词表写入文件 with open('vocab.txt', 'w', encoding='utf-8') as f: for word in word_set: f.write(word + '\n') # 读取词表 with open('vocab.txt', 'r', encoding='utf-8') as f: vocab = [line.strip() for line in f.readlines()] # 编写正向最大匹配分词程序 def forward_matching(text, vocab): result = [] while text: for i in range(len(text), 0, -1): if text[:i] in vocab: result.append(text[:i]) text = text[i:] break else: result.append(text[0]) text = text[1:] return result # 对文本进行正向最大匹配分词 seg_list2 = forward_matching(text, vocab) # 将分词结果用空格连接成字符串 seg_text = ' '.join(seg_list2) # 将分词结果写入文件 with open('CDIAL-BIAS-race-seg2.txt', 'w', encoding='utf-8') as f: f.write(seg_text) # 导入评价库 from sklearn.metrics import precision_recall_fscore_support # 转换分词结果为列表 seg_list = seg_text.split() # 转换结巴分词结果为列表 with open('CDIAL-BIAS-race-seg.txt', 'r', encoding='utf-8') as f: jieba_seg_list = f.read().split() # 计算评价指标 precision, recall, f1_score, _ = precision_recall_fscore_support(jieba_seg_list, seg_list, average='macro') # 输出评价结果 print('Precision:', precision) print('Recall:', recall) print('F1-score:', f1_score)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值