計算機歷年考研複試上機基礎題(二)
阿新 • • 發佈:2020-08-18
特殊乘法
題目描述
寫個演算法,對2個小於1000000000的輸入,求結果。 特殊乘法舉例:123 * 45 = 1*4 +1*5 +2*4 +2*5 +3*4+3*5輸入描述:
兩個小於1000000000的數
輸出描述:
輸入可能有多組資料,對於每一組資料,輸出Input中的兩個數按照題目要求的方法進行運算後得到的結果。示例1
輸入
123 45
輸出
54
emmm,暴力吧
1 #include <stdio.h> 2 #include <algorithm> 3 #include <stdlib.h> 4View Code#include <string> 5 #include <string.h> 6 #include <math.h> 7 #include <iostream> 8 #include <map> 9 #define maxn 100 10 11 using namespace std; 12 13 map<string,int> m; 14 char a[maxn]; 15 char b[maxn]; 16 17 int main(){ 18 scanf("%s%s",&a,&b);19 int len1=strlen(a); 20 int len2=strlen(b); 21 int sum=0; 22 for(int i=0;i<len1;i++){ 23 for(int j=0;j<len2;j++){ 24 sum+=(a[i]-'0')*(b[j]-'0'); 25 } 26 } 27 printf("%d",sum); 28 return 0; 29 }
守形數
題目描述
守形數是這樣一種整數,它的平方的低位部分等於它本身。 比如25的平方是625,低位部分是25,因此25是一個守形數。 編一個程式,判斷N是否為守形數。輸入描述:
輸入包括1個整數N,2<=N<100。
輸出描述:
可能有多組測試資料,對於每組資料, 輸出"Yes!”表示N是守形數。 輸出"No!”表示N不是守形數。示例1
輸入
25 4
輸出
Yes! No!
求出平方後與平方前對應長度的數,然後進行比較
1 #include <stdio.h> 2 #include <algorithm> 3 #include <stdlib.h> 4 #include <string> 5 #include <string.h> 6 #include <math.h> 7 #include <iostream> 8 #include <map> 9 #define maxn 100 10 11 using namespace std; 12 13 map<string,int> m; 14 char a[maxn]={0}; 15 char b[maxn]; 16 17 int main(){ 18 int n; 19 scanf("%d",&n); 20 int ans=1; 21 while(n/ans!=0){ 22 ans*=10; 23 } 24 if((n*n)%ans==n){ 25 printf("Yes!\n"); 26 }else{ 27 printf("No!\n"); 28 } 29 return 0; 30 }View Code
反序輸出
題目描述
輸入任意4個字元(如:abcd), 並按反序輸出(如:dcba)輸入描述:
題目可能包含多組用例,每組用例佔一行,包含4個任意的字元。
輸出描述:
對於每組輸入,請輸出一行反序後的字串。 具體可見樣例。示例1
輸入
Upin cvYj WJpw cXOA
輸出
nipU jYvc wpJW AOXc
1 #include <stdio.h> 2 #include <algorithm> 3 #include <stdlib.h> 4 #include <string> 5 #include <string.h> 6 #include <math.h> 7 #include <iostream> 8 #include <map> 9 #define maxn 100 10 11 using namespace std; 12 13 map<string,int> m; 14 char a[maxn]={0}; 15 char b[maxn]; 16 17 int main(){ 18 while(scanf("%s",&a)!=EOF){ 19 for(int i=3;i>=0;i--){ 20 printf("%c",a[i]); 21 } 22 printf("\n"); 23 } 24 return 0; 25 }View Code
比較奇偶數個數
題目描述
第一行輸入一個數,為n,第二行輸入n個數,這n個數中,如果偶數比奇數多,輸出NO,否則輸出YES。輸入描述:
輸入有多組資料。 每組輸入n,然後輸入n個整數(1<=n<=1000)。
輸出描述:
如果偶數比奇數多,輸出NO,否則輸出YES。示例1
輸入
5 1 5 2 4 3
輸出
YES
判斷奇偶,用&會快一點,奇數二進位制最後一位為1
1 #include <stdio.h> 2 #include <algorithm> 3 #include <stdlib.h> 4 #include <string> 5 #include <string.h> 6 #include <math.h> 7 #include <iostream> 8 #include <map> 9 #define maxn 100 10 11 using namespace std; 12 13 map<string,int> m; 14 char a[maxn]={0}; 15 int b[maxn]={0}; 16 17 int main(){ 18 int n=0; 19 int tmp=0; 20 int m=0,k=0; 21 while(scanf("%d",&n)!=EOF){ 22 for(int i=0;i<n;i++){ 23 scanf("%d",&tmp); 24 if(tmp&1){ 25 m++; 26 }else{ 27 k++; 28 } 29 } 30 if(k>m){ 31 printf("NO\n"); 32 }else{ 33 printf("YES\n"); 34 } 35 } 36 return 0; 37 }View Code
找x
題目描述
輸入一個數n,然後輸入n個數值各不相同,再輸入一個值x,輸出這個值在這個陣列中的下標(從0開始,若不在陣列中則輸出-1)。輸入描述:
測試資料有多組,輸入n(1<=n<=200),接著輸入n個數,然後輸入x。
輸出描述:
對於每組輸入,請輸出結果。示例1
輸入
2 1 3 0
輸出
-1
1 #include <stdio.h> 2 #include <algorithm> 3 #include <stdlib.h> 4 #include <string> 5 #include <string.h> 6 #include <math.h> 7 #include <iostream> 8 #include <map> 9 #define maxn 100 10 11 using namespace std; 12 13 map<string,int> m; 14 char a[maxn]={0}; 15 int b[maxn]={0}; 16 17 int main(){ 18 int n=0; 19 while(scanf("%d",&n)!=EOF){ 20 for(int i=0;i<n;i++){ 21 cin>>b[i]; 22 } 23 int key=0; 24 cin>>key; 25 int m=0; 26 for(int i=0;i<n;i++){ 27 if(b[i]==key){ 28 m=1; 29 printf("%d\n",i); 30 break; 31 } 32 } 33 if(!m){ 34 printf("-1\n"); 35 } 36 } 37 return 0; 38 }View Code
字串連結
題目描述
不用strcat 函式,自己編寫一個字串連結函式MyStrcat(char dstStr[],charsrcStr[])輸入描述:
兩個字串,字串由小寫字母組成。
輸出描述:
連結後的字串示例1
輸入
hello world good morning
輸出
helloworld goodmorning
自定義Mystrcat和直接判斷原理一樣
1 #include <stdio.h> 2 #include <algorithm> 3 #include <stdlib.h> 4 #include <string> 5 #include <string.h> 6 #include <math.h> 7 #include <iostream> 8 #include <map> 9 #define maxn 100 10 11 using namespace std; 12 13 map<string,int> m; 14 char a[maxn]={0}; 15 char b[maxn]={0}; 16 char c[maxn]={0}; 17 18 char *Mystrcat(char *a, char *src){ 19 char *p=a; 20 while(*p!='\0') p++; 21 while(*src!='\0')*p++=*src++; 22 *p='\0'; 23 return a; 24 } 25 26 int main(){ 27 int n=0; 28 while(scanf("%s %s",a,b)!=EOF){ 29 // int k=0; 30 // //‘\0’:‘\0’是一個“空字元”常量,它表示一個字串的結束,它的ASCII碼值為0, 31 // //注意它與空格' '(ASCII碼值為32)及'0'(ASCII碼值為48)不一樣的。 32 // for(int i=0;a[i]!='\0';i++){ 33 // c[k++]=a[i]; 34 // } 35 // for(int i=0;b[i]!='\0';i++){ 36 // c[k++]=b[i]; 37 // } 38 // c[k]='\0'; 39 // printf("%s\n",c); 40 //string str=Mystrcat(a,b); 41 printf("%s\n",Mystrcat(a,b)); 42 43 } 44 45 return 0; 46 }View Code
與7無關的數
題目描述
一個正整數,如果它能被7整除,或者它的十進位制表示法中某個位數上的數字為7, 則稱其為與7相關的數.現求所有小於等於n(n<100)的與7無關的正整數的平方和。輸入描述:
案例可能有多組。對於每個測試案例輸入為一行,正整數n,(n<100)
輸出描述:
對於每個測試案例輸出一行,輸出小於等於n的與7無關的正整數的平方和。示例1
輸入
複製21
輸出
複製2336
兩種情況過濾一下,
1 #include <stdio.h> 2 #include <algorithm> 3 #include <stdlib.h> 4 #include <string> 5 #include <string.h> 6 #include <math.h> 7 #include <iostream> 8 #include <map> 9 #define maxn 100 10 11 using namespace std; 12 13 map<string,int> m; 14 char a[maxn]={0}; 15 char b[maxn]={0}; 16 17 int main(){ 18 int n=0; 19 while(scanf("%d",&n)!=EOF){ 20 if(n>=7){ 21 int sum=91; 22 for(int i=8;i<=n;i++){ 23 int ans=i%10;//個位數是否為7 24 int tmp=i/7; 25 if(ans==7 ||tmp*7==i ) continue; 26 if(i/7==0) continue; 27 sum+=i*i; 28 } 29 printf("%d\n",sum); 30 }else{ 31 int sum=0; 32 for(int i=1;i<=n;i++){ 33 sum+=(i*i); 34 } 35 printf("%d\n",sum); 36 } 37 } 38 39 return 0; 40 }View Code
小白鼠排隊
題目描述
N只小白鼠(1 <= N <= 100),每隻鼠頭上戴著一頂有顏色的帽子。現在稱出每隻白鼠的重量,要求按照白鼠重量從大到小的順序輸出它們頭上帽子的顏色。帽子的顏色用“red”,“blue”等字串來表示。不同的小白鼠可以戴相同顏色的帽子。白鼠的重量用整數表示。輸入描述:
多案例輸入,每個案例的輸入第一行為一個整數N,表示小白鼠的數目。 下面有N行,每行是一隻白鼠的資訊。第一個為不大於100的正整數,表示白鼠的重量,;第二個為字串,表示白鼠的帽子顏色,字串長度不超過10個字元。 注意:白鼠的重量各不相同。
輸出描述:
每個案例按照白鼠的重量從大到小的順序輸出白鼠的帽子顏色。示例1
輸入
複製3 30 red 50 blue 40 green
輸出
複製blue green red
STL知識有點淡忘:
map,vector,set排序
map定義優先順序排序;vector可用sort
map<int,string,greater<int> >m;
set<int, greater<int> > s;
s.insert(1);
bool cmp(int a,int b){ return a>b; } vector<int>v; v.push_back(1); v.push_back(2); sort(v.begin(),v.end(),cmp);
1 #include <stdio.h> 2 #include <algorithm> 3 #include <stdlib.h> 4 #include <string> 5 #include <string.h> 6 #include <math.h> 7 #include <iostream> 8 #include <map> 9 #define maxn 100 10 11 using namespace std; 12 13 map<int,string,greater<int> >m; 14 char a[maxn]={0}; 15 char b[maxn]={0}; 16 17 int main(){ 18 int n=0; 19 while(scanf("%d",&n)!=EOF){ 20 for(int i=0;i<n;i++){ 21 int a; 22 string str; 23 cin>>a>>str; 24 m[a]=str; 25 } 26 map<int ,string>::iterator iter; 27 for(iter=m.begin();iter!=m.end();iter++) 28 cout<<iter->second<<endl; 29 } 30 31 return 0; 32 }View Code
字母統計
題目描述
輸入一行字串,計算其中A-Z大寫字母出現的次數輸入描述:
案例可能有多組,每個案例輸入為一行字串。
輸出描述:
對每個案例按A-Z的順序輸出其中大寫字母出現的次數。示例1
輸入
DFJEIWFNQLEF0395823048+_+JDLSFJDLSJFKK
輸出
A:0 B:0 C:0 D:3 E:2 F:5 G:0 H:0 I:1 J:4 K:2 L:3 M:0 N:1 O:0 P:0 Q:1 R:0 S:2 T:0 U:0 V:0 W:1 X:0 Y:0 Z:0
65至90為大寫字母的ASCII值,,+32為小寫字母
1 #include <stdio.h> 2 #include <algorithm> 3 #include <stdlib.h> 4 #include <string> 5 #include <string.h> 6 #include <math.h> 7 #include <iostream> 8 #include <map> 9 #define maxn 100 10 11 using namespace std; 12 13 map<int,string,greater<int> >m; 14 char a[maxn]; 15 char b[maxn]={0}; 16 17 int main(){ 18 while(scanf("%s",a)!=EOF){ 19 for(int i=0;a[i];i++){ 20 int tmp=a[i]; 21 if(tmp<=90&&tmp>=65){ 22 b[tmp]++; 23 } 24 } 25 for(int i=65;i<=90;i++){ 26 int ss=b[i]; 27 printf("%c:%d\n",i,ss); 28 } 29 30 } 31 32 return 0; 33 }View Code