算法競賽入門【碼蹄集新手村600題】(MT1451-1500)
(文章目錄)
前言
為什麼突然想學算法了?
> 用較為“官方”的語言講,是因為算法對計算機科學的所有分支都非常重要。 在絕大多數的計算機科學分支領域中,要想完成任何實質性的工作,了解算法的基礎知識并掌握與算法密切相關的資料結構知識是必不可少的。
> 但從實際而言,是因為當下快到了考研和找工作的年紀(ಥ_ಥ),無論走哪一條路,都不免需要一些相對豐富的算法知識,是故,便産生了一個暑假速成算法的計劃,可能對于像我這種算法競賽小白而言,幾乎很難,但我仍然還是想嘗試一下,畢竟,夢想還是要有的,萬一實作了呢?~( ̄▽ ̄~)~
為什麼選擇碼蹄集作為刷題軟體?
碼蹄集,是在全國高等學校計算機教學與産業實踐資源建設專家委員會(TIPCC) 指導下建設的,其依托全國各大名校計算機系和清華大學出版社等機關的強大資源,旨在為計算機學習愛好者提供全面和權威的計算機習題。同時,也是我作為百度松果計劃成員的主要OJ題周測平台,另有清華,中科院,北京工業等各知名院校和院所的強力支援,是故選擇了它
目錄
1. MT1451 魔方陣
(1)題目描述
輸出N階奇數階魔方陣。所謂N階魔方陣,就是把1 ~n*n個連續的正整數填到一個N行N列的方陣中,使得每一列、每一行以及兩個對角線的元素和都相等。
格式
輸入格式: 輸入為整型N
.
輸出格式: 輸出N階魔方陣
樣例1
輸入格式: 3
.
輸出格式:
8 1 6
3 5 7
4 9 2
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int row,col,n,a[100][100] = {0};
scanf("%d",&n);
row=0;
col=(n-1)/2;
a[row][col] = 1;
for(int i=2;i<=n*n;i++){
if(a[(row-1+n)%n][(col+1)%n]==0){
row = (row-1+n)%n;
col = (col+1)%n;
}else{
row = (row+1)%n;
}
a[row][col]=i;
}
for(row=0;row<n;row++){
for(col=0;col<n;col++) cout<<a[row][col]<<" ";
cout<<endl;
}
return 0;
}
2. MT1452 替換轉置
(1)題目描述
輸入一個M ×N的整數數組,把原來的負數全部替換成對應的正數,正數替換成負數,比如-5替換成5,再轉置輸出。不考慮非法輸入或者溢出等特殊情況。
格式
輸入格式: 第一行輸入M和N(均小于100),第二行輸入數組元素,空格分隔。
.
輸出格式:輸出矩陣
樣例1
輸入格式:
3 2
8 9
-5 4
-1 -7
.
輸出格式:
-8 5 1
-9 -4 7
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int m,n,a[100][100];
scanf("%d%d",&m,&n);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++) printf("%d ",-a[j][i]);
printf("\n");
}
return 0;
}
3. MT1453 逆時針旋轉矩陣
(1)題目描述
輸入正整數N,把一個大小為NxN的方陣,逆時針旋轉180度後輸出。
格式
輸入格式: 第一行輸入數組長度N,後N行按方陣輸入數組元素,整型,空格分隔。
.
輸出格式: 按方陣輸出,整型,空格分隔。
樣例1
輸入格式:
4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
.
輸出格式:
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n,a[100][100];
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>a[i][j];
}
}
for(int i=n-1;i>=0;i--){
for(int j=n-1;j>=0;j--) cout<<a[i][j]<<" ";
printf("\n");
}
return 0;
}
4. MT1454 字元串輸入輸出方法I
(1)題目描述
輸入一個字元串,輸出字元串。請在循環中%c一個字元一個字元的輸入輸出。
格式
輸入格式: 分2行輸入。第一行輸入字元串長度n,第二行輸入字元串
.
輸出格式: 輸出字元型
樣例1
輸入格式:
5
abcde
.
輸出格式: abcde
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n;
char ch[10005]="";
scanf("%d\n",&n);
for(int i=0;i<n;i++) scanf("%c",&ch[i]);
for(int i=0;i<n;i++) printf("%c",ch[i]);
return 0;
}
5. MT1455 字元串輸入輸出方法ll
(1)題目描述
輸入一個字元串,輸出字元串。請用‰%s輸入輸出整個字元串。本題不考慮空格。
格式
輸入格式: 分2行輸入。第一行輸入字元串長度n,第二行輸入字元串。
.
輸出格式: 輸出字元型
樣例1
輸入格式:
5
abcde
.
輸出格式: abcde
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n;
scanf("%d\n",&n);
string s;
getline(cin,s);
cout<<s<<endl;
return 0;
}
6. MT1456 字元串輸入輸出方法III
(1)題目描述
輸入一個字元串,輸出字元串。本題請調用fgets和fputs實作字元串輸入輸出。
格式
輸入格式: 分2行輸入。第一行輸入字元串長度n (n<80),第二行輸入字元串
.
輸出格式: 輸出字元型
樣例1
輸入格式:
5
abcde
.
輸出格式: abcde
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int n;
char s1[1005];
scanf("%d\n",&n);
fgets(s1,n+1,stdin);
fputs(s1,stdout);
return 0;
}
7. MT1457 字元串輸入輸出方法IV
(1)題目描述
輸入一個字元串,輸出字元串。請用scanf(“%[^\n]%*c”,str);輸入
格式
輸入格式: 輸入字元串
.
輸出格式: 輸出字元串
樣例1
輸入格式: 12345
.
輸出格式: 12345
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s[100];
scanf("%[^\n]%*c",s);
printf("%s",s);
return 0;
}
8. MT1458 字元串數組
(1)題目描述
定義一個字元串數組char A[3][80],按字元串方式來進行輸入和輸出(串中不含空格)。
格式
輸入格式: 分3行輸入,每行輸入一個字元串
.
輸出格式: 分3行輸出,每行輸出一個字元串
樣例1
輸入格式:
aaa
bbbb
cccccc
.
輸出格式:
aaa
bbbb
cccccc
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
string s;
for(int i=0;i<3;i++){
cin>>s;
cout<<s<<endl;
}
return 0;
}
9. MT1459 字元串長度函數
(1)題目描述
定義一個字元數組,長度為80,輸入一個字元串,用字元串長度函數計算有效的長度并輸出,然後用sizeof計算數組長度并輸出,看看有什麼不同。
格式
輸入格式: 輸入字元串
.
輸出格式: 輸出為整型,空格分隔。
樣例1
輸入格式: 12345
.
輸出格式: 5 80
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80];
cin.getline(a, 80);
printf("%d %d",strlen(a),sizeof(a));
return 0;
}
10. MT1460 字元串長度
(1)題目描述
編寫程式,在不使用标準字元串函數的情況下求一個字元串S1的長度。
格式
輸入格式: 輸入字元串(長度小于80)
.
輸出格式:輸出為整型
樣例1
輸入格式: 123456
.
輸出格式: 6
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80];
cin.getline(a, 80);
int n=0;
while(a[n] !='\0') n++;
cout<<n;
return 0;
}
11. MT1461 字元串複制函數
(1)題目描述
用字元串複制函數把b字元串複制到a字元串裡面,輸出結果。
格式
輸入格式: 輸入字元串b
.
輸出格式: 輸出字元串a
樣例1
輸入格式: 12345
.
輸出格式: 12345
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
string s;
cin>>s;
cout<<s;
return 0;
}
12. MT1462 字元串複制
(1)題目描述
編寫程式,在不使用标準字元串函數的情況下将一個字元串S1的内容複制給另一個字元串S2。
格式
輸入格式: 輸入字元串S1(長度小于80)
.
輸出格式: 輸出字元串S2
樣例1
輸入格式: 123456
.
輸出格式: 123456
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s1[1005],s2[1005];
cin.getline(s1, 10005);
int len = strlen(s1);
for(int i=0;i<len;i++){
s2[i] = s1[i];
}
cout<<s2<<endl;
return 0;
}
13. MT1463 字元串連接配接函數
(1)題目描述
用字元串連接配接函數把兩個字元串拼接在一起,輸出結果。
格式
輸入格式: 2個字元串分2行輸入。
.
輸出格式: 輸出字元串
樣例1
輸入格式:
12345
abcde
.
輸出格式: 12345abcde
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s1[1005],s2[1005];
scanf("%s",s1);
scanf("%s",s2);
strcat(s1, s2);
cout<<s1<<endl;
return 0;
}
14. MT1464 字元串連接配接
(1)題目描述
編寫程式,在不使用标準字元串函數的情況下将兩個字元串S1和S2連接配接起來,結果儲存在S1字元串中。
格式
輸入格式: 分兩行輸入字元串S1和S2(長度小于80)
.
輸出格式: 輸出字元串S1
樣例1
輸入格式:
123456
abc
.
輸出格式: 123456abc
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80],b[80];
cin.getline(a, 80);
cin.getline(b, 80);
int lena = strlen(a);
int lenb = strlen(b);
for(int i=lena,j=0;i<lena+lenb;i++,j++){
a[i] = b[j];
}
a[lena+lenb]='\0';
printf("%s",a);
return 0;
}
15. MT1465 字元串比較函數
(1)題目描述
輸入2個字元串,用字元串比較函數比較他們并輸出結果。
格式
輸入格式: 字元串分2行輸入
.
輸出格式: 輸出整型
樣例1
輸入格式:
12345
2
.
輸出格式: -1
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s1[80],s2[80];
scanf("%s",s1);
scanf("%s",s2);
printf("%d",strcmp(s1,s2));
return 0;
}
16. MT1466 字元串比較
(1)題目描述
編寫程式,在不使用标準字元串函數的情況下比較兩個字元串S1和S2,如果S1>S2,輸出1;如果S1=S2,輸出0;如果S1<S2,輸出-1。
格式
輸入格式: 分兩行輸入字元串S1和S2(長度小于80)
.
輸出格式: 輸出為整型
樣例1
輸入格式:
123456
abc
.
輸出格式: -1
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
string s1,s2;
getline(cin, s1);
getline(cin, s2);
if(s1>s2) cout<<1;
else if(s1==s2) cout<<0;
else cout << -1;
cout << endl;
return 0;
}
17. MT1467 字元串比較
(1)題目描述
利用指針完成字元串比較函數int mystrcmp(char *s1,char *s2),比較字元串s1, s2(字元串長度小于10000),如果s1>s2輸出1,小于則輸出-1,相等輸出0。
格式
輸入格式: 分兩行輸入字元串s1,s2
.
輸出格式: 輸出整數
樣例1
輸入格式:
I love C!
C is great!
.
輸出格式: 1
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int mystrcmp(char *s1,char *s2){
while(*s1 !='\0' && *s2 != '\0'){
if(*s1 > *s2) return 1;
else if(*s1 < *s2) return -1;
s1++;
s2++;
}
if(*s1 != '\0') return 1;
else if(*s2 != '\0') return -1;
return 0;
}
int main( )
{
char str1[10000] = "",str2[10000]="";
scanf("%[^\n]%*c",str1);
scanf("%[^\n]%*c",str2);
cout<<mystrcmp(str1, str2);
return 0;
}
18. MT1468 字元串字首
(1)題目描述
兩個字元串可能有相同的字首,輸出這個字首。
格式
輸入格式: 分兩行輸入字元串S1和S2
.
輸出格式: 輸出字元串字首
樣例1
輸入格式:
aa123456
aabc
.
輸出格式: aa
備注:
字元串中可能含有空格
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
void samePre(char s1[],char s2[],char s3[]){
int pos=0;
while(s1[pos] == s2[pos]){
s3[pos]=s1[pos];
pos++;
}
s3[pos]='\0';
return ;
}
int main( )
{
char s1[100],s2[100],s3[100];
cin.getline(s1, 100);
cin.getline(s2, 100);
samePre(s1, s2, s3);
printf("%s\n",s3);
return 0;
}
19. MT1469 間隔輸出串
(1)題目描述
将字元串l love c++賦給一個字元數組,然後從第一個字母開始間隔地輸出該串。
格式
輸入格式: 無
.
輸出格式: 輸出字元串
樣例1
輸入格式: 無
.
輸出格式: Ilv +
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[]="I love c++";
string s="";
for(int i=0;i<10;i++){
if(i%2==0) s+=a[i];
}
cout<<s;
return 0;
}
20. MT1470 字元串切割
(1)題目描述
輸入字元串s1(長度小于80,由大小寫字母構成,不含空格),和正整數N(小于字元串長度),把字元串切割成每N個字元一份,并輸出(最後一份可以小于N)。
格式
輸入格式: 分兩行輸入字元串s1和正整數N
.
輸出格式: 輸出切割後的字元串,每一份一行
樣例1
輸入格式:
adfasdfsadsgsg
3
.
輸出格式:
adf
asd
fsa
dsg
sg
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char s1[80];
int n,len;
scanf("%s",s1);
scanf("%d",&n);
len = strlen(s1);
for(int i=0;i<len;i++){
printf("%c",s1[i]);
if((i+1)%n == 0) printf("\n");
}
return 0;
}
21. MT1471 反轉
(1)題目描述
輸入一個字元串,輸出字元串反轉後的内容
格式
輸入格式: 輸入字元型
.
輸出格式: 輸出字元型
樣例1
輸入格式: i hate u
.
輸出格式: u etah i
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
void reverse(char *start,char *end){
while(start < end){
int tmp = *start;
*start = *end;
*end = tmp;
start++;
end--;
}
}
int main( )
{
char arr[80];
cin.getline(arr,100);
reverse(arr, arr+strlen(arr)-1);
printf("%s",arr);
return 0;
}
22. MT1472 字元串反向
(1)題目描述
輸入字元串s1(不含空格),将字元串反向存放并輸出。
格式
輸入格式: 輸入字元串(長度小于80)
.
輸出格式: 輸出字元,空格分隔
樣例1
輸入格式: water
.
輸出格式: retaw
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
string s1;
string s2="";
getline(cin,s1);
for(int i=s1.length()-1;i>=0;i--){
s2 += s1[i];
}
cout<<s2;
return 0;
}
23. MT1473 左移n位
(1)題目描述
輸入一個字元串(長度不超過80),輸出字元串左移n位後的内容。移出去的内容去掉。
格式
輸入格式: 分2行輸入。第一行輸入字元串,第二行輸入非負整數n,n小于字元串長度
.
輸出格式: 輸出字元串
樣例1
輸入格式:
12345
3
.
輸出格式: 45
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80],b[80];
int n;
cin.getline(a, 80);
scanf("%d",&n);
strcpy(b,a+n);
printf("%s",b);
return 0;
}
24. MT1474 右移n位
(1)題目描述
輸入一個字元串(長度不超過80),輸出字元串右移n位後的内容。移出去的内容去掉。
格式
輸入格式: 分2行輸入。第一行輸入字元串,第二行輸入輸入非負整數n, n小于字元串長度
.
輸出格式: 輸出字元串
樣例1
輸入格式:
12345
3
.
輸出格式: 12
備注:
字元串可能包含空格
(2)參考代碼
#include<bits/stdc++.h>
using namespace std;
int main( )
{
char a[80];
int n;
cin.getline(a,80);
scanf("%d",&n);
a[strlen(a)-n] = '\0';
printf("%s",a);
return 0;
}
25. MT1475 回旋字元串
(1)題目描述
輸入字元串s1,s2(均不含空格),判斷s1是否為s2的回旋字元串,輸出YES或者NO。
格式
輸入格式: 分兩行輸入字元串s1,s2(長度都小于80)
.
輸出格式: 輸出整數
樣例1
輸入格式:
waterbottle
erbottlewat
.
輸出格式: YES
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
string s1,s2,s11,s22;
cin >>s1>>s2;
s11=s1+s1;
s22=s2+s2;
if (s11. find (s2)!= string :: npos &&s22.find(s1)!= string :: npos )
cout <<"YES ";
else
cout <<"NO ";
return 0;
}
26. MT1476 右旋n位
(1)題目描述
輸入一個字元串,輸出字元串右旋n位後的内容
格式
輸入格式: 分2行輸入。第一行輸入字元串,第二行輸入整數n
.
輸出格式: 輸出字元串
樣例1
輸入格式:
12345
3
.
輸出格式: 34512
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
//sour[]數組下标為 beginS 開始,一共複制 n 個字元給 dest[],後者起始下标為 beginD
void arrayCopy ( char sour[], int beginS , int n , char dest [], int beginD ){
for( int i=1; i <= n ;i++) dest[beginD++]= sour[beginS++];
}
int main(){
char a [100], b [100];
int n , m ;
cin.getline (a ,100);
cin >> m ;
n = strlen (a);
for ( int i =0; i < n ; i ++) cin >> a [ i ];
m = m % n ;
arrayCopy(a , n-m , m , b ,0);
arrayCopy(a ,0, n-m , b , m );
for( int i =0; i < n ; i ++){
cout << b [i];
}
return 0;
}
27. MT1477 左旋n位
(1)題目描述
輸入一個字元串,輸出字元串左旋n位後的内容
格式
輸入格式: 分2行輸入。第一行輸入字元串,第二行輸入整數n
.
輸出格式: 輸出字元串
樣例1
輸入格式:
12345
3
.
輸出格式: 45123
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
//sour[數組下标為 beginS 開始,一共複制 n 個字元給 dest [],後者起始下标為 beginD
void arrayCopy ( char sour[], int beginS , int n , char dest [], int beginD ){
for ( int i =1; i<=n ; i ++)
dest [ beginD ++]= sour[ beginS ++];
}
int main ()
{
char a [100], b [100];
int n , m ;
cin . getline ( a ,100);
cin >> m ;
n = strlen ( a );
m = m % n ;
arrayCopy ( a , m , n - m , b ,0);
arrayCopy ( a ,0, m , b , n - m );
for ( int i=0; i<n ; i ++)
cout << b [i];
return 0;
}
28. MT1478 插入A
(1)題目描述
從鍵盤上輸入一個字元串(長度小于10000,以回車作為結束,其餘地方不出現回車),按後按照下面要求輸出一個新字元串,新的字元串是在原來字元串中,每兩個字元之間插入一個A,輸出新産生的字元串。
格式
輸入格式: 輸入字元串
.
輸出格式: 輸出字元串
樣例1
輸入格式: abcd
.
輸出格式: aAbAcAd
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
char s1[10000],s2[20000];
cin . getline (s1,10000);
int len = strlen (s1);
for ( int i =0; i < len -1; i ++){
s2[ i *2]=s1[ i ];
s2[ i *2+1]='A';
}
s2[ len *2-2]=s1[ len -1];
s2[ len *2-1]='\0';
cout <<s2;
return 0;
}
29. MT1479 字元串插入
(1)題目描述
将一個字元串插入到另一個字元串指定位置處。
格式
輸入格式: 分行輸入字元串,最後一行輸入指定位置。
.
輸出格式: 輸出字元串
樣例1
輸入格式:
aaaaa
bbb
2
.
輸出格式: aabbbaaa
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
void insert ( char *s1, char *s2, int pos ){
int len1= strlen (s1);
int len2= strlen (s2);
for ( int i=len1; i>pos-1; i--) s1[i+len2]=s1[i];
for ( int i =0; i <len2; i++) s1[i+ pos]=s2[i];
}
int main (){
char s1[100],s2[100];
int pos=0;
cin.getline (s1,100);
cin.getline (s2,100);
cin>>pos;
insert (s1,s2,pos);
cout<<s1<<endl;
return 0;
}
30. MT1480 替換空格
(1)題目描述
輸入字元串s1,找出字元串的所有空格,把它替換成**輸出。
格式
輸入格式: 輸入字元串(長度小于80)
.
輸出格式: 輸出字元串
樣例1
輸入格式: Wendy love C++! C++ is great!
.
輸出格式: WendyloveC++!**C++wasgreat!
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
char a[80], b [100];
cin . getline ( a ,80);
int len = strlen ( a ), pos =0;
for ( int i =0; i < len ; i ++){
if (a[i]==' '){
b[ pos ++]='*' ;
b[ pos ++]='*' ;
}else
b[ pos ++]= a [ i ];
}
b [ pos ]='\0';
cout << b ;
return 0;
}
31. MT1481 小碼哥的樂譜
(1)題目描述
小碼哥是個小馬虎老是寫錯樂譜,你不得不幫他替換一下。法則是下面這些等式中,左右兩邊的字元互換A#=Bb,C#=Db,D#=Eb,F#=Gb,G#=Ab。
格式
輸入格式: 輸入字元串,其總長度小于80。輸入如樣例所示,樂譜符号後面空一格輸入minor或者major。
.
輸出格式: 輸出字元串
樣例1
輸入格式: Ab minor
.
輸出格式: G# minor
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main (){
char a[80];
cin.getline(a,80);
int len = strlen(a);
for ( int i=0; i<len; i++){
if (a[i]=='A' && a[i+1]=='#') a[i]='B', a[i+1]='b';
else if ( a[i]=='C'&& a[i+1]=='#') a [i]='D', a [i+1]='b';
else if ( a[i]=='D'&& a[i+1]=='#') a [i]='E', a [i+1]='b';
else if ( a[i]=='F'&& a[i+1]=='#') a [i]='G', a [i+1]='b';
else if ( a [i]=='G'&& a [ i +1]=='#') a [ i ]='A', a [ i +1]='b';
else if ( a [ i ]=='B'&& a [ i +1]=='b') a [ i ]='A', a [1+1]='#';
else if ( a [ i ]=='D'&& a [ i +1]=='b') a [ i ]='C', a [ i +1]='#';
else if ( a [ i ]=='E'&& a [ i +1]=='b') a [ i ]='D', a [ i +1]='#';
else if ( a [ i ]=='G'&& a[i+1]=='b') a [ i ]='F', a [1+1]='#';
else if(a[i]=='A'&&a[i+1]=='b') a[i]='G',a[i+1]='#';
}
printf ("%s\n",a);
return 0;
}
32. MT1482 删除空格
(1)題目描述
編寫程式,去掉一個字元串全部空格。
格式
輸入格式: 輸入字元串,長度為n (n<100000)
.
輸出格式: 輸出字元串
樣例1
輸入格式: 1 2 3 4 5 6
.
輸出格式: 123456
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main (){
char s [1000];
char newS [1000];
cin.getline ( s ,1000);
int j =0;
for ( int i =0; i < strlen ( s ); i ++)
if ( s [ i ]!=' ')
newS [ j ++]= s [ i ];
newS [ j ]='\0';
cout << newS << endl ;
return 0;
}
33. MT1483 首尾空格
(1)題目描述
編寫程式,去掉一個字元串(長度小于80)首尾空格。
格式
輸入格式: 輸入字元串
.
輸出格式: 輸出字元串
樣例1
輸入格式: 123456
.
輸出格式:123456
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
char s [80];
cin . getline ( s ,80);
int start =0, end = strlen ( s )-1;
for ( int i =0; i < strlen ( s ); i ++)
if ( s [i]==' ') start ++;
else break ;
for ( int i = strlen ( s )-1; i >=0; i--)
if ( s [ i ]==' ') end --;
else break ;
for ( int i = start ; i <= end ; i ++)
printf ("%c", s [ i ]);
return 0;
}
34. MT1484 删除空格标點
(1)題目描述
輸入字元串s1,找出字元串的所有空格和标點符号删除後輸出。(假定标點符号隻包含英文感歎号,逗号和句号)
格式
輸入格式: 輸入字元串(長度小于80)
.
輸出格式: 輸出字元串
樣例1
輸入格式: Wendy love C++! C++ is great!
.
輸出格式: WendyloveC++C++isgreat
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main (){
char s [80];
char newS [80];
cin.getline ( s ,80); int j =0;
for ( int i =0; i < strlen ( s );i++)
if (s[i]!=' '&&s[ i ]!='!'&& s[i]!='.'&& s[i]!=',')
newS[j++]=s[i];
newS[j]='\0';
cout << newS << endl ;
return 0;
}
35. MT1485 删除字元
(1)題目描述
輸入2個字元串A和B,删除第一個字元串中包含的第二個字元串中的字元。如果不包含則原樣輸出第一個字元串。
格式
輸入格式: 輸入2個字元串,長度為n (n<100)
.
輸出格式: 轉換後的字元串
樣例1
輸入格式:
123456
123
.
輸出格式: 456
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
bool findChar ( char c , char a []){
for ( int j =0; j < strlen ( a ); j ++)
if ( c ==a [j])
return true ;
return false ;
}
int main (){
char s1[100],s2[100], newS [100];
cin . getline (s1,100);
cin . getline (s2,100);
int k =0;
for ( int i =0; i < strlen (s1); i ++)
if (! findChar (s1[ i ],s2))
newS [ k ++]=s1[ i ];
newS [ k ]='\0';
cout << newS ;
return 0;
}
36. MT1186 字元搜尋
(1)題目描述
編寫程式,在不使用标準字元串函數的情況下搜尋一個字元在字元串中的位置。如果沒有搜尋到位置為-1。從0開始計數。
格式
輸入格式: 分兩行輸入,第一行輸入要搜尋的字元,第二行輸入字元串(長度小于80)
.
輸出格式: 輸出為整型
樣例1
輸入格式:
3
123456
.
輸出格式: 2
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main (){
char c , a [80];
scanf ("%c",&c);
getchar ();
cin.getline ( a ,80);
for ( int i =0; i < strlen ( a );i ++)
if ( a [ i ]== c){
cout << i ;
return 0;
}
cout <<-1;
return 0;
}
37. MT1487 檢查子串
(1)題目描述
輸入字元串(長度小于80,不含空格)S,如果在字元串S中找到字母則輸出YES,沒有輸出NO。
格式
輸入格式: 輸入字元串(不含空格)
.
輸出格式: 輸出YES或者NO
樣例1
輸入格式: C++!
.
輸出格式: YES
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main (){
char s [80]; scanf ("%s ", s );
int len = strlen ( s );
for ( int i =0; i < len ; i ++)
if (( s [ i ]>='A'&& s [ i ]<='Z')||( s [ i ]>='a'&& s [ i ]<='z')) {
printf ("YES\n");
return 0;
}
printf ("NO\n");
return 0;
}
38. MT1488 最大和最小字元
(1)題目描述
輸入字元串s1(不含空格),查找字元串中最大和最小(ASCII值,區分大小寫)字元并輸出。
格式
輸入格式: 輸入字元串(長度小于80)
.
輸出格式: 輸出最大和最小字元,空格分隔
樣例1
輸入格式: waterbottle
.
輸出格式: w a
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
char s1[80];
scanf ("%s",s1);
int len = strlen (s1);
char max =s1[0], min =s1[0];
for ( int i =0; i < len ; i ++)
if (s1[ i ]> max ) max =s1[ i ];
else if (s1[i]< min ) min =s1[ i ];
printf ("%c %c", max , min );
return 0;
}
39. MT1489 字元串的大小寫轉換
(1)題目描述
給定一個字元串,将所有的大寫字母轉換成小寫,并把所有小寫的轉換為大寫。
格式
輸入格式: 一個長度為100000以内的字元串
.
輸出格式: 轉換後的字元串
樣例1
輸入格式: AbCdEf
.
輸出格式: aBcDeF
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main (){
char s [100000];
cin.getline ( s ,100000);
for ( int i =0; i < strlen ( s ); i ++){
if ( s [ i ]>='a'&& s [ i ]<='z')
s[i]-=32;
else if ( s [ i ]>='A'&& s [ i ]<='Z')
s[i]+=32;
}
cout << s << endl ;
return 0;
}
40. MT1490 修改字元串
(1)題目描述
輸入1個字元串,如果其中小寫字元多于大寫字元,則将其全部轉換為小寫字元,如果大寫字元多于小寫字元,則全部轉換為大寫字元。
格式
輸入格式: 輸入1個字元串,長度為n (n<100000)
.
輸出格式: 轉換後的字元串
樣例1
輸入格式: abcdefGHi
.
輸出格式: abcdefghi
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main (){
char s [100000];
cin.getline ( s ,100000);
int num_a =0, num_A =0;
for ( int i =0; i < strlen ( s ); i ++){
if ( s[i]>='a'&& s [i]<='z')
num_a ++;
else if ( s[i]>='A'&& s[i]<='Z')
num_A ++;
}
for( int i =0; i < strlen(s);++ i ){
if (num_a < num_A && s[i]>='a'&& s[i]<='z')
s[i]-=32;
if ( num_a > num_A & s[i]>='A'& s[i]<='Z')
s[i]+=32;
}
cout << s << endl ;
return 0;
}
41. MT1491 标點符号
(1)題目描述
假定一個字元串(長度小于80)存在的标點符号有感歎号、逗号、句号、問号這幾種英文符号,分别計算這些符号有多少個并輸出結果。
格式
輸入格式: 輸入字元串
.
輸出格式: 輸出整型,空格分隔
樣例1
輸入格式: Good Morning! Miss Wendy Potter, have you seen your mom? She love u.
.
輸出格式: 1 1 1 1
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
char a [80];
cin.getline ( a ,80);
int len = strlen ( a );
int na =0, nb =0, nc =0, nd =0;
for ( int i =0; i < len ; i ++){
if (a[i] == '!') na++;
if (a[i] == ',') nb++;
if (a[i] == '.') nc++;
if (a[i] == '?') nd++;
}
printf ("%d %d %d %d ", na , nb , nc , nd );
return 0;
}
42. MT1492 元音計數
(1)題目描述
輸入1個字元串(不含空格),統計其中出現的元音字母的個數。
格式
輸入格式: 一個字元串,長度n (n<100000)
.
輸出格式: 其中元音字母的個數
樣例1
輸入格式: aeiou
.
輸出格式: 5
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
char s [100000];
char check[11]="aeiouAEIOU";
scanf ("%s",s);
int len = strlen (s);
int cnt =0;
for ( int i =0; i < len ;i++)
for ( int j=0; j<10; j ++)
if ( check [j]== s [i])
cnt++;
printf ("%d", cnt );
return 0;
}
43. MT1493 出現最多的首字母
(1)題目描述
輸入1個字元串,是空格分隔的一個個單詞。統計其中出現次數最多的首字母。
格式
輸入格式: 輸入1個字元串,長度為n (n<100000)
.
輸出格式: 出現次數最多的首字母(如果有多個,輸出ASCII碼最小的字母)注意輸出小寫字母!
樣例1
輸入格式: aword bword cword aword
.
輸出格式: a
(2)參考代碼
#include <iostream>
using namespace std ;
int main ()
{
char s[10000][10];
int num =0;
while(~scanf("%s",&s[num])) num++;
int cnt [26]={0};
for ( int i=0; i<num;i++){
if(s[i][0]<='Z'&& s[i][0]>='A')
s[i][0]+=32;
cnt[s[i][0]-'a']++;
}
int tmp =-1, id =-1;
for ( int i=0; i <26; i ++){
if ( tmp < cnt [i]){
tmp = cnt [ i ];
id = i ;
}
}
printf ("%c",'a'+ id );
return 0;
}
44. MT1494 出現最多的單詞
(1)題目描述
輸入1個字元串(不含數字或者其他字元,隻含大小寫字母和空格,單詞不區分大小寫),該字元串是空格分隔的一個個單詞,這些單詞長度相等都包含5個字元。統計其中出現次數最多的單詞。
格式
輸入格式: 輸入1個字元串,長度為n (n<100)
.
輸出格式: 出現次數最多的單詞(若有多個單詞滿足條件,輸出原輸入順序最靠前的滿足條件的單詞,單詞輸出小寫即可)
樣例1
輸入格式: worda wordb wordc worda
.
輸出格式: worda
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
char s[20][6]={0};
int number[20]={0};
int cnt =0;
while (scanf("%s", s[cnt])!= EOF){
for ( int i =0; i <5; i ++){
if (s[cnt][i]<'a')
s[cnt][i]+=32;
}
cnt ++;
}
int res =0;
for ( int i =0; i < cnt ; i ++){
for ( int j = i ; j < cnt ; j ++)
if ( strcmp(s[i], s[j])==0)
number[i]++;
res = max(res,number[i]);
}
for ( int i =0; i < cnt ; i ++)
if ( res == number [ i ]){
printf("%s", s[i]);
break;
}
return 0;
}
45. MT1495 單詞
(1)題目描述
編寫程式,輸入一個字元串,統計其中有多少個單詞(單詞用空格、逗号、小數點分隔)。
格式
輸入格式: 輸入字元串
.
輸出格式: 輸出為整型
樣例1
輸入格式: i love u
.
輸出格式: 3
備注:
本題中,所有符号均為英文符号。非空格、非逗号、非小數點的符号,均視為單詞的正确組成部分。
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
bool judge ( char a ){
if ( a ==' '|| a =='.'|| a ==',') return true ;
else return false ;
}
int main ()
{
char a[80];
cin.getline(a ,80);
int len = strlen (a), cnt =0;
if (! judge (a[0])) cnt ++;
for ( int i =1; i < len ; i ++)
if ( judge ( a[i -1])&! judge ( a[i]))
cnt ++;
cout << cnt ;
return 0;
}
46. MT1496 重複字元
(1)題目描述
輸入字元串s1(隻包含字母和空格),查找字元串中的重複字元(指26個英語字母,區分大小寫),從小到大(ASCII值)排序他們并輸出。
格式
輸入格式: 輸入字元串(長度小于80)
.
輸出格式: 輸出字元,空格分隔
樣例1
輸入格式: Wendy is a great girl
.
輸出格式: a e g i r
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
char a[80], b[26]={0}, c[26];
cin.getline(a,80);
int len = strlen(a);
for(int i=0; i<len; i++){
if(a[i]>='A' && a[i]<='z')
b[a[i]-'A']++;
else if (a[i]>='a' && a[i]<='z')
c[a[i]-'a']++;
}
for(int i=0; i<26; i++)
if(b[i]>1) printf("%c ",'A'+i);
for(int i=0; i<26; i++)
if(c[i]>1) printf("%c ",'a'+i);
return 0;
}
47. MT1497 字元串中唯一字元
(1)題目描述
給定一個字元串(均為小寫字母組成,不含空格),找到它的第一個不重複的字元,并傳回它的索引(從O開始)。如果不存在,則傳回 -1。
格式
輸入格式: 輸入字元串s
.
輸出格式: 輸出索引,不存在輸出-1
樣例1
輸入格式: lovenwpulovenpu
.
輸出格式: 153
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
char a [80], b [26]={0};
cin.getline ( a ,80);
int len = strlen(a);
for( int i =0; i<len ; i ++)
if( a[i]>='a'&& a[i]<='z')
b[a[i]-'a']++;
for ( int i =0; i < len ; i ++)
if ( b [ a [ i ]-'a']==1){
printf ("%d", i );
return 0;
}
printf ("%d",-1);
return 0;
}
48. MT1498 整數轉換字元串
(1)題目描述
将一個整型資料(有正負号)轉換成字元串。
格式
輸入格式: 輸入為整型
.
輸出格式: 輸出字元串
樣例1
輸入格式: -123
.
輸出格式: -123
樣例2
輸入格式: +34
.
輸出格式: +34
備注:
數值較大,需要定義為long類型。不考慮輸入為0的特殊情況。
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
long n , i =0;
char a[10];
scanf ("%ld", &n );
if(n>0) printf ("%c",'+');
else if ( n <0)
n = -n , printf ("%c",'-');
while ( n ){
a[i++]= n%10+'0';
n/=10;
}
for (int j = i -1; j >=0; j --)
printf ("%c", a[j]);
return 0;
}
49. MT1499 無符号整數轉換
(1)題目描述
将一個無符号整型資料轉換成字元串形式的二進制。
格式
輸入格式:輸入為整型
.
輸出格式: 輸出為字元串
樣例1
輸入格式: 35
.
輸出格式: 100011
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
int a [32];
unsigned int num ;
scanf("%u",& num );
int cnt =0;
while (num){
a[cnt++]= num & 1;
num /=2;
}
for ( int i=cnt-1; i>=0;i--)
printf ("%d", a[i]);
if (cnt ==0) printf ("0");
return 0;
}
50. MT1500 有符号數轉換
(1)題目描述
将一個有符号整型資料轉換成字元串形式的八位二進制
(負數使用其補碼)
例如,若為-2則輸出11111110
格式
輸入格式:輸入為整型
.
輸出格式: 輸出為字元串
樣例1
輸入格式: 35
.
輸出格式: 00100011
(2)參考代碼
#include<bits/stdc++.h>
using namespace std ;
int main ()
{
int a [8];
int num ;
scanf ("%d",&num);
for ( int i =0; i <8; i ++){
a[i]= num &1;
num >>=1;
}
for ( int i =7; i >=0; i --)
printf ("%d", a [ i ]);
return 0;
}
小結
- 矩形例題:MT1451-1453
- 字元長度:MT1459、MT1460