计算机历年考研复试上机基础题(二)

特殊乘法

题目描述

写个算法,对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>
 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];
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 }
View Code

守形数

题目描述

守形数是这样一种整数,它的平方的低位部分等于它本身。 比如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!
");
26     }else{
27         printf("No!
");
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("
");
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
");
32         }else{
33             printf("YES
");
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
",i);
30                 break;
31             }
32         }
33         if(!m){
34             printf("-1
");
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!='') p++;
21     while(*src!='')*p++=*src++;
22     *p='';
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 //            //‘’:‘’是一个“空字符”常量,它表示一个字符串的结束,它的ASCII码值为0,
31 //            //注意它与空格' '(ASCII码值为32)及'0'(ASCII码值为48)不一样的。
32 //            for(int i=0;a[i]!='';i++){
33 //                c[k++]=a[i];
34 //            }
35 //            for(int i=0;b[i]!='';i++){
36 //                c[k++]=b[i];
37 //            }
38 //            c[k]='';
39 //            printf("%s
",c);
40     //string str=Mystrcat(a,b);
41     printf("%s
",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
",sum);
30         }else{
31             int sum=0;
32             for(int i=1;i<=n;i++){
33                 sum+=(i*i);
34             }
35             printf("%d
",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
",i,ss);
28         }
29 
30     }
31 
32     return 0;
33 }
View Code


原文地址:https://www.cnblogs.com/z-712/p/10544249.html