题目1055:数组逆置
-
题目描述:
-
输入一个字符串,长度小于等于200,然后将数组逆置输出。
-
输入:
-
测试数据有多组,每组输入一个字符串。
-
输出:
-
对于每组输入,请输出逆置后的结果。
-
样例输入:
-
hdssg
-
样例输出:
-
gssdh
#include<iostream>
#include<string.h>
using namespace std;
char str[1000];
int main()
{
while(cin>>str)
{
int left,right;
left=0;
right=strlen(str)-1;
while(left<=right)
{
char tmp;
tmp=str[left];
str[left]=str[right];
str[right]=tmp;
left++;
right--;
}
cout<<str<<endl;
}
return 0;
}
题目1056:最大公约数
-
题目描述:
-
输入两个正整数,求其最大公约数。
-
输入:
-
测试数据有多组,每组输入两个正整数。
-
输出:
-
对于每组输入,请输出其最大公约数。
-
样例输入:
-
49 14
-
样例输出:
-
7
//递归
#include<stdio.h>
#include<stdlib.h>
int gcd(int a,int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);//把上一次的除数当做这一次的被除数,把上一次的余数当做这一次的除数
}
int main(){
int a,b;
while(~scanf("%d %d",&a,&b)){//总体上就是,如果有正确输入,就退出循环,如果没有正确输入,就执行
//没有~就超时,因为没有办法处理非法输入。
printf("%d\n",gcd(a,b));
}
return 0;
}
非递归:
#include<stdio.h>
#include<stdlib.h>
int gcd(int a,int b)
{
int temp = a;
while(b){
a = b;
b = temp % b;
}
return a;
}
int main(){
int a,b;
while(~scanf("%d %d",&a,&b)){
printf("%d\n",gcd(a,b));
}
return 0;
}
题目1057:众数
-
题目描述:
-
输入20个数,每个数都在1-10之间,求1-10中的众数(众数就是出现次数最多的数,如果存在一样多次数的众数,则输出权值较小的那一个)。
-
输入:
-
测试数据有多组,每组输入20个1-10之间的数。
-
输出:
-
对于每组输入,请输出1-10中的众数。
-
样例输入:
-
5 1 5 10 3 5 3 4 8 6 8 3 6 5 10 7 10 2 6 2
-
样例输出:
-
5
#include<iostream>
#include<string.h>
using namespace std;
int a,num[100];
int main()
{
int maxs;
int maxi;
//重定向输入输出时候,只要在前面写上这两个语句而已,其他的和正常程序没有变化 ,可以用于循环输入,但是提交要注释
freopen("datain.txt","r",stdin);
freopen("dataout.txt","w",stdout);
while(cin>>a)
{
memset(num,0,sizeof(num));
num[a]++;
for(int i=1;i<20;i++)//因为之前输入过一个a
{
cin>>a;
num[a]++;
}
maxs=0;
for(int i=1;i<=10;i++)//因为num[i]中的i就是输入的元素,而输入的元素的范围是1-10
{
if(num[i]>maxs)
{
maxs=num[i];
maxi=i;
}
}
cout<<maxi<<endl;
}
return 0;
}