P1320压缩技术(续集版
感觉这题还是蛮难的对我来说,通过这题我才知道原来字符串输入不碰到空格就会一起输进来
我参考了一写题解自己又写了自己的解法,vs中的scanf_s和scanf()用法不太一样,之前按scanf写法写一直在报错,心态有点被搞崩了
下面是洛谷的解法
#include <stdio.h>
#include <math.h>
#include <string.h>
int n;//存储点阵是几行几列
int a[200][2];//用来存储每一位
int now = 0;//用来储存现在是第几位数
char x[200];
char y[200];
int m;
int k;//储存上一位的y值
int many = 0;//存储一共有几个数
int f;
int main() {
scanf("%s", &x);
if (x[0] == '1') m = 1;
//int p = 1;
while (x[many] == '0'|| x[many]=='1') {
k = m;
m = x[many] - '0';
many++;
if (k == 1 && m == 0) now++;
a[now][m]++;
}
/*while (scanf_s("%s", y, 1)) {
m = y - "0";
a[now][m]++;
k = m;
//m = 3;
//scanf_s("%d", &y);
many++;
if (k == 1 && m == 0) now++;
}*/
for (int l = 1; l < many; l++) {
scanf("%s", &y);
for (int o = 0; o < many; o++) {
k = m;
m = y[o] - '0';
if (k == 1 && m == 0) now++;
a[now][m]++;
}
//m = 3;
//scanf_s("%d", &y);
//many++;
}
//f = many;
printf("%d ", many);
for (int i=0; i <= now; i++) {
for (int j=0; j < 2; j++) {
if(i==now&&a[i][j]==0&&a[i][j+1]==0)break;
printf("%d ", a[i][j]);
}
}
}
下面是vs中的写法
#include <stdio.h>
#include <math.h>
#include <string.h>
int n;//存储点阵是几行几列
int a[200][2];//用来存储每一位
int now = 0;//用来储存现在是第几位数
char x[200];
char y[200];
int m;
int k;//储存上一位的y值
int many = 0;//存储一共有几个数
int f;
int main() {
scanf_s("%s", x,200);
if (x[0] == '1') m = 1;
//int p = 1;
while (x[many] == '0'|| x[many]=='1') {
k = m;
m = x[many] - '0';
many++;
if (k == 1 && m == 0) now++;
a[now][m]++;
}
/*while (scanf_s("%s", y, 1)) {
m = y - "0";
a[now][m]++;
k = m;
//m = 3;
//scanf_s("%d", &y);
many++;
if (k == 1 && m == 0) now++;
}*/
for (int l = 1; l < many; l++) {
scanf_s("%s", y, 200);
for (int o = 0; o < many; o++) {
k = m;
m = y[o] - '0';
if (k == 1 && m == 0) now++;
a[now][m]++;
}
//m = 3;
//scanf_s("%d", &y);
//many++;
}
//f = many;
printf("%d ", many);
for (int i=0; i <= now; i++) {
for (int j=0; j < 2; j++) {
if (i == now && a[i][2]==0)break;
printf("%d ", a[i][j]);
}
}
}
突然想起来就是还有一个坑就是如果最后一位是0就不用输出了