本文共 1068 字,大约阅读时间需要 3 分钟。
题意:
在一个字符串(N≤100)中,子串LSC,PCMS谁出现的次数多输出谁,相等输出Tie。(1)直接法
#include#include intmain() { char n[105]; int i, len, lsc = 0, pcms = 0; gets(n); len = strlen(n); for( i = 0; i < len; i++ ) { if( n[i] == 'L' && n[i + 1] == 'S' && n[i + 2] == 'C' ) { lsc++; } if( n[i] == 'P' && n[i + 1] == 'C' && n[i + 2] == 'M' && n[i + 3] == 'S' ) { pcms++; } } if( lsc > pcms ) { puts("LSC"); } else if( lsc < pcms ) { puts("PCMS"); } else { puts("Tie"); } return 0;}
(2)库函数
#include#include intmain() { char n[105]; int i, len, lsc = 0, pcms = 0; gets(n); len = strlen(n); for( i = 0; i < len; i++ ) { if( strncmp(n + i, "LSC", 3) == 0 ) { lsc++; } if( strncmp(n + i, "PCMS", 4) == 0 ) { pcms++; } } if( lsc > pcms ) { puts("LSC"); } else if( lsc < pcms ) { puts("PCMS"); } else { puts("Tie"); } return 0;}
转载地址:http://fvkxi.baihongyu.com/