题目描述
一天小c获得了一个字符串,字符串中只有0和1。本来所有的0都在1之前,
但是小c毕竟是个捣蛋的孩子,所以小c把这个字符串打乱了。
有的0在1的后面,有的1在0的前面。如果一个1在一个0的前面
,那个这个1和这个0就形成了一个逆序数对。现在给你这个打乱的字符串,
让你求出这个字符串的逆序数对的个数。
输入
先输入一个T,代表有T组数据。(T<=10)
接下来有T行,每行一个字符串,代表被打乱的字符串(字符串的长度<=100000)
输出
输出T行,每一行代表对应的字符串的逆序数对的个数
示例输入
2
01
11010
示例输出
0
5#include <stdio.h>
#include <string.h>
char str[100010];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int sum=0;
int cnt=0;
scanf("%s",str);
int len=strlen(str);
for(int i=0;i<len;i++){
if(str[i]=='1')
cnt++;
else if(str[i]=='0')
sum+=cnt;
}
printf("%d\n",sum);
}
return 0;
}
#include <stdio.h>
#include <string.h>
char str[100010];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int sum=0;
int cnt=0;
scanf("%s",str);
int len=strlen(str);
for(int i=0;i<len;i++){
if(str[i]=='1')
cnt++;
else if(str[i]=='0')
sum+=cnt;
}
printf("%d\n",sum);
}
return 0;
}