题解 | #简单错误记录#
简单错误记录
https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
#include <stdio.h>
#include <string.h>
/* Function: 數組內元素全部置零 */
void init(char *str)
{
memset(str,0,sizeof((*str)*strlen(str)));
}
int main() {
char str[100][100]={0}, str0[100][17]={0};
int row[100]={0};
int count[100]={[0 ... 99]=1};//這種賦值方式值得學習!!
int i=0,j=0,k=0;
while ( scanf("%s %d",&str[i++][0],&row[j++])!=EOF );
int COUNT=i-1; //這裡注意是i-1,因為在掃描完最後一行數據後i++了;然後再次執行了一次掃描,此時掃描到是EOF,同時又執行了i++;因此總共執行了兩次i++,即係超出數據行數一次、超出行坐標兩次
// printf("str0=%s,row0=%d\n",&str[10][0],row[10]);
// printf("COUNT0 = %d\n",COUNT);
/* 提取字符串的最後16個字符 */
for (i=0; i<COUNT; i++)
{
for (j=0; j<strlen(&str[i][0]); j++)
{
if (str[i][j]=='\\') //C語言中'\'符號本身要雙杠,否則會被編譯成為轉義符號!!!!!!
{
init(&str0[i][0]);
// printf("count[i]=%d,i=%d\n",count[i],i);
if(strlen(&str[i][j+1])>16)
{
strcpy(&str0[i][0],&str[i][j+1+(strlen(&str[i][j+1])-16)]);
}
else {
strcpy(&str0[i][0],&str[i][j+1]);
}
}
else continue;
}
}
/* 刪除重複數據 */
for (i=0; i<COUNT-1; i++)
{
for (j=i+1; j<COUNT; j++)
{
if ((strcmp(&str0[i][0],&str0[j][0])==0)&&(row[i]==row[j]))
{
count[i]++;
for (k=j; k<COUNT; k++)
{
if (k==COUNT-1)
{
init(&str0[k][0]);
row[k]=0;
count[k]=0;
}
else{
strcpy(&str0[k][0],&str0[k+1][0]);
row[k]=row[k+1];
count[k]=count[k+1];
}
}
COUNT--;
j--;
// printf("COUNT=%d\n",COUNT);
}
}
}
/* 打印輸出 */
int start_index=0;
if (COUNT>8) start_index=COUNT-8;
// printf("count=%d,st_index=%d\n",COUNT,start_index);
for (i=start_index; i<COUNT; i++)
{
printf("%s %d %d\n", &str0[i][0], row[i], count[i]);
}
return 0;
}
小天才公司福利 1199人发布