24
所有块的总长度就是字符串的长度,数一下有多少块就行了。 #include
char str[55];
void read() {
scanf("%s", str);
}
void work() {
int cnt = 1, i;
for (i = 1; str[i]; ++i) {
if (str[i] != str[i-1]) {
++cnt;
}
}
double res = (double)(i) / cnt;
printf("%.2lf\n", res);
}
int main() {
read();
work();
return 0;
}
编辑于 2017-09-13 03:26:54
回复(6)
21
只需一行
思路,用字符串长度除以去重字符串长度。去重用正则 var readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line) {
console.log((line.length / line.replace(/(.)\1+/g, "$1").length).toFixed(2));
});
编辑于 2017-09-12 14:53:23
回复(3)
6
首先我感觉这题描述有问题,求碎片的平均长度应该去掉字符串中重复的碎片,按这道题的输出描述它的意思就是求:(字符串的总长度)/(相同字母团构成的字符串的个数)。 public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.next();
float len = s.length();//总长
int count = 1;//个数至少为一个
for (int i = 0; i < len - 1; i++) {
if (s.charAt(i) != s.charAt(i + 1)) {
count++;
}
}
System.out.println(len / count);
}
}
编辑于 2018-03-12 15:03:00
回复(1)
3
语言:C++ 运行时间: 2 ms 占用内存:376K 状态:答案正确
水题,所有碎片的总长就是字符串的长度,遍历记录碎片数量即可。
本套8道题的C++代码已挂到了我的GitHub(https://github.com/shiqitao/NowCoder-Solutions)上,持续更新。 #include
#include
#include
using namespace std;
int main()
{
string str; cin >> str;
int len = str.size();
char pre = '0';
int num = 0;
for (int i = 0; i < len; i++) {
if (str[i] != pre) {
num++;
pre = str[i];
}
}
cout << setiosflags(ios::fixed) << setprecision(2) << (double)len / num;
return 0;
}
发表于 2017-10-10 10:19:22
回复(1)
5
#include
#include
#include
#include
using namespace std;
int main()
{
char s[50];
float b = 0;
cin >> s;
float n = strlen(s);
for (int i = 0; i < n; i++)
{
if (s[i] != s[i + 1])
++b;
}
float a = n/b;
cout << setiosflags(ios::fixed);
cout.precision(2);
cout << a << endl;
}
发表于 2017-09-10 12:34:41
回复(2)
6
遍历一下s计算前后不一致位置的数量k,k+1就是碎片的数量,而总长度总等于s的长度
s=raw_input()
n=len(s)
k=0
for i in range(n-1):
if s[i]!=s[i+1]:
k+=1
print '%.2f'%(float(n)/(k+1))
发表于 2017-12-28 18:14:10
回复(1)
4
import java.util.Scanner;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
System.out.printf("%.2f\n", getAveLen(line));
}
public static float getAveLen(String s) {
ArrayList list = new ArrayList<>();
Pattern p = Pattern.compile("([a-z])\\1*");
Matcher m = p.matcher(s);
while(m.find()) {
list.add(m.group());
}
float sumLen = 0;
for(String str : list) {
sumLen += str.length();
}
return sumLen/list.size();
}
}
发表于 2017-09-10 20:15:02
回复(6)
1
java的正则运用
思路如下:
1.记录原来字符串的长度
2.用Java的正则表达式,把重复的字符全部变为一个(如“aabbccaa”变为“abca”)
3.原来字符串的长度,除以新的字符串长度
4.结果保留两位小数输出(不保留两位小数通不过,试出来的) import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String str = input.nextLine();
float len = str.length();
System.out.printf("%.2f",len / str.replaceAll("(.)\\1+","$1").length());
}
}
编辑于 2020-08-03 22:33:57
回复(0)
1
#每次碎片改变增加一个间隔,总碎片长度等于输入长度,最后加1类似于植树问题,树的数目(碎片数目)等于间隔+1
x = raw_input() num = 0
for i in range(1,len(x)):
if x[i] == x[i-1]:
pass
else:
num = num + 1
print ('%.2f' %(float(len(x))/(num+1)))
发表于 2019-08-30 16:34:34
回复(0)
1
import sys
k=0
count=1
stringlist=list(sys.stdin.readline().strip())
for i in range(1,len(stringlist)):
if stringlist[i]!=stringlist[k]:
count+=1
k+=1
stringlist[k]=stringlist[i]
print('%.2f'%(float(len(stringlist))/float(count)))
发表于 2018-05-16 08:52:10
回复(0)
1
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
double rate = calTheRate(str);
System.out.println(String.format("%.2f", rate)); //保留两位小数
}
private static double calTheRate(String str) {
String reg = "([a-z])\\1*";
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(str);
List list = new ArrayList();
double sum = 0;
while(matcher.find()){
int itemLen = matcher.group().toString().length();
list.add(itemLen);
sum +=itemLen;
}
return sum / list.size();
}
}
发表于 2018-04-25 19:49:19
回复(0)
1
我来说一种不用正则表达式的吧,因为正则表达式毕竟要记得导的什么包。
我的思路很简单,只要当前位置的字符与下一个位置的字符不一样,碎片数就加1.
最后用**字符串的长度**除以碎片数。
importjava.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc =newScanner(System.in);
String str = sc.nextLine();
ans(str);
}
public static void ans(String str){
intcount =0;
for(inti=0;i
if(str.charAt(i)!=str.charAt(i+1)){
count++;
}
}
count++;
double len = str.length();
System.out.println(String.format("%.2f", len/count));
}
}
编辑于 2018-03-08 21:42:49
回复(2)
1
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//System.out.print("请输入一个字符串:");
String test = input.nextLine();
System.out.print(avgLength(test));
}
public static String avgLength(String s){
double result = 0;
int count = 1;
int sum = 1;
char[] chars = s.toCharArray();
for(int i = 0; i < chars.length - 1; i++){
if(chars[i] == chars[i + 1]){
count++;
if(i == chars.length - 2)
result += count;
}else {
result += count;
if(i == chars.length - 2)
result++;
count = 1;
sum++;
}
}
return String.format("%.2f", result/sum);
}
}
发表于 2017-10-09 21:11:13
回复(1)
1
#include
int main(){
char s[100000];
int i;
while(scanf("%s",s)!=EOF){
int cnt=1,pre=1;
double sum=0;
for(i=1;s[i]!='\0';i++)
if(s[i]==s[i-1]) pre++;
else{
sum+=(double)pre;
pre=1;
cnt++;
}
sum+=(double)pre;
sum/=cnt;
printf("%.2lf\n",sum);
}
}
发表于 2017-09-18 19:49:43
回复(0)
1
var arr = readline().match(/[a-z]/g);
var total = 1, flag = arr[0];
for(let val of arr) {
if(val == flag) continue;
else {
total++;
flag = val;
};
}
print( (arr.length / total).toFixed(2) );
发表于 2017-09-12 10:20:26
回复(0)
1
while(str = readline()){
var len = str.length;
if(len == 1){
print(len.toFixed(2));
}
var cnt = 1;
var result = 0;
for(var i=1; i
if(str[i] !== str[i-1]){
cnt++;
}
}
result = len/cnt;
print(result.toFixed(2));
}
发表于 2017-09-11 21:40:08
回复(0)
1
from functools import reduce
def str_pieces(s):
pattern = s[0]
l = []
num = 0
for i in s :
if i == pattern :
num += 1
else :
l.append(num)
pattern = i
num = 1
l.append(num)
return l
while True:
try:
s = input()
l = str_pieces(s)
sum = reduce(lambda x,y:x+y,l)
print('{:.2f}'.format(sum/len(l)))
except:
break
发表于 2017-09-10 10:05:41
回复(0)
2
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String s = sc.next();
int len = s.length();
int c=0;
int i=0;
char[] a = new char[len+1];
for(int j = 0;j
a[j]=s.charAt(j);
}
for(i=0;i
if(a[i]!=a[i+1]){
c++;
}
}
double q = (double)len/c;
System.out.println(String.format("%.2f", q));
}
}
发表于 2017-09-18 19:03:21
回复(2)
1
发表于 2018-01-25 09:36:18
回复(1)
1
import java.util.ArrayList;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
char[] arr = s.toCharArray(); Character t = ' ';
String r = "";
ArrayList list = new ArrayList<>();
for (char c : arr) {
if (t!=c) {
t = c;
if (!r.equals(""))
list.add(r);
r="";
}
r += c;
}
list.add(r);
int sum = 0;
for (String string : list) {
sum+=string.length();
}
double p =(double)(sum)/list.size();
System.out.println(String.format("%1$.2f", p));
}
}
编辑于 2017-09-13 20:23:18
回复(1)