sicily 6274. Mispelling


Description
Misspelling is an art form that students seem to excel at. Write a program that removes the n th character from an input string.
 
Input

The first line of input contains a single integer N , (1≤N≤1000) which is the number of datasets that follow.

Each dataset consists of a single line of input containing M , a space, and a single word made up of uppercase letters only. M will be less than or equal to the length of the word. The length of the word is guaranteed to be less than or equal to 80.

Output
For each dataset, you should generate one line of output with the following values: The dataset number as a decimal integer (start counting at one), a space, and the misspelled word. The misspelled word is the input word with the indicated character deleted.
 
算是五道作業里最難的了,因為還完全沒學過字符串和字符的處理。
還好這道題流傳很廣,找了幾個答案琢磨了一下,研究了字符串的處理方式,自己組織出了解法。
題目意思很簡單,先輸入要處理幾個詞,然后是每個詞要抽掉的字母的位置和單詞。
 
答案
View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char word[100]; /* 用來存字符串的數組,100是隨意設的,夠用就行 */
 6     int n, mis, i, j;
 7     
 8     scanf("%d", &n);/* 輸入要處理幾個詞 */
 9     
10     for( i=1; i<=n; i++ )/* 當單詞還沒全部處理完的時候 */ 
11     {
12         scanf("%d %s", &mis, &word);/* 輸入要處理的字母的位置和整個單詞 */ 
13         
14         printf("%d ", i);/* 輸出這是第幾個要處理的單詞 */ 
15         for ( j=0; j<strlen(word); j++ )/* 當沒有處理完單詞的每個字母(沒到全長)的時候 */ 
16         {                                /* 因為數組序號的特性,j初識值是0,
17                                         循環到j=單詞長度即處理到單詞后一個單元時跳出 */ 
18             if ((j+1)!= mis)    /* 如果這個字母不在要抽掉的位置上 */ 
19             {
20                 printf("%c", word[j]); /* 輸出這個字母 */
21             }                    /* 如果這個字母在要抽掉的位置上就會被跳過不輸出 */
22         }
23         printf("\n")        ;/* 輸出一個空行 */
24     }
25     return 0;
26 }

 

今天看了一點C PRIMER PLUS里關於字符串的部分,貌似可以利用字符串最后帶一個/0解決,不過試了幾遍編譯出來都是一堆代碼然后報錯,暫時作罷

 

后記

發現為什么之前不能利用/0解決了……原來是斜杠方向錯了,應該是\0

修改過后的代碼如下,可以不用strlen函數和string.h庫,更加簡單

View Code
 1 // Problem#: 6274
 2 // Submission#: 1609796
 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
 6 #include<stdio.h>
 7 int main()
 8 {
 9     char word[100];
10     int n, mis, i, j;
11     
12     scanf( "%d", &n );
13     
14     for( i=1; i<=n; i++ )
15     {
16         scanf( "%d %s", &mis, &word );
17         
18         printf( "%d ", i );
19         
20         for ( j=0; word[j] != '\0'; j++ )
21         {
22             if ( (j+1) != mis )
23             {
24                 printf( "%c", word[j] );
25             }
26         }
27         
28         printf("\n");
29     }
30     
31     return 0;
32 }

 

網上還有一種解法是從要misspell的位置開始,用后一個字符逐個向前覆蓋,不過在這題中並沒有要求改變字符串的值,直接用跳過輸出的方法更加簡便易懂

除了用if(j+1 != mis)+輸出語句以外,應該也可以用輸出語句+if(j+1 == mis)+continue來跳過。個人傾向於前一種,畢竟continue不符合結構化的原則。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM