本系列博客为个人刷题思路分享,有需要借鉴即可。
2.题目链接:
3.详解思路:
#include
int main()
{
int n = 0;
scanf("%d",&n);
double min = 100;
double max = 0;
double average = 0;
double sum = 0;
double x = 0;
for(int i = 0;i<n;i++)
{
scanf("%lf",&x);
if(x>max)
max = x;
if(x<min)
min = x;
sum+=x;
}
average = sum/n;
printf("%.2lf %.2lf %.2lf\n",max,min,average);
return 0;
}
#include
#include
int main() {
char ch1[20] = {0};
char ch2[20] = {0};
//读取
scanf("%s",ch1);
scanf("%s",ch2);
//比较
if(strcmp(ch1,ch2)==0)
{
printf("same\n");
}
else {
{
printf("different\n");
}
}
return 0;
}
#include
int main() {
int n = 0;
int m = 0;
scanf("%d %d",&n,&m);
int i = 0;
int sum = 0;
int data = 0;
//读入数据
for(i = 0;i<n*m;i++)
{
scanf("%d",&data);
if(data>=0)
{
sum+=data;
}
}
printf("%d\n",sum);
return 0;
}
#include
int main() {
int i = 0;
int arr[10] = {0};
//读入
for(i = 0;i<10;i++)
{
scanf("%d",&arr[i]);
}
//输出
for(i = 0;i<10;i++)
{
printf("%d ",arr[10-i-1]);
}
return 0;
}
#include
int main()
{
int arr[10] = {0};
int i = 0;
int p = 0;
int n = 0;
for(i = 0;i<10;i++)
{
scanf("%d",&arr[i]);
if(arr[i]>0)
p++;
else
n++;;
}
printf("positive:%d\nnegative:%d\n",p,n);
return 0;
}
#include
int main() {
int n = 0;
scanf("%d",&n);
int i = 0;
int data = 0;
int sum = 0;
for(i = 0;i<n;i++)
{
scanf("%d",&data);
sum+=data;
}
printf("%d\n",sum);
return 0;
}
#include
int main() {
int n = 0;
scanf("%d",&n);
int max = 0;
int min = 100;
int data = 0;
//读入
int i = 0;
for(i = 0;i<n;i++)
{
scanf("%d",&data);
if(data>max)
max = data;
if(data<min)
min = data;
}
printf("%d\n",max-min);
return 0;
}
#include
int main() {
int n = 0;
scanf("%d",&n);
int arr[50] = {0};
int on = 0;//升序标识符
int under = 0;//降序标识符
int i = 0;
for(i = 0;i<n;i++)
{
scanf("%d",&arr[i]);
if(i>0)
{
if(arr[i]>arr[i-1])
on = 1;
else if(arr[i]<arr[i-1])
under = 1;
}
}
//if on+under>1 乱序
//if on+under ==1 升序/降序
if(on+under>1)
printf("unsorted\n");
else
{
printf("sorted\n");
};
return 0;
}
#include
int main() {
int n = 0;
scanf("%d",&n);
//读入数据
int i = 0;
int arr[50] = {0};
for(i = 0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int m = 0;
scanf("%d",&m);
int count = n;//计数器
//遍历数组
for(i = 0;i<count;i++)
{
//遍历数组找到了要删除的数据
if(m==arr[i])
{
//删除数据(后面的数据往前挪动一位)
int j = 0;
for(j = i+1;j<n;j++)
{
arr[j-1] = arr[j];
}
i--;
count--;
}
}
//打印
for(i = 0;i<count;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
#include
int main() {
int n = 0;
scanf("%d",&n);
int arr[n];
//读取数据
int i = 0;
for(i = 0;i<n;i++)
{
scanf("%d",&arr[i]);
}
//去重
for(i = 0;i<n;i++)
{
int j = 0;
for(j = i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
//挪动数据
int k = 0;
for(k = j+1;k<n;k++)
{
arr[k-1] = arr[k];
}
n--;//防止遗漏
j--;//计数
}
}
}
for(i = 0;i<n;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
//思路1:存入数据,之后排序
#include
int main()
{
int n, m;
scanf("%d %d\n", &n, &m);
int i, j, arr[2000];
int tmp;
//读入两个数组的数据
for (i = 0; i < n; i++)
{
scanf("%d ", &arr[i]);
}
for (i = n; i < n + m; i++)
{
scanf("%d ", &arr[i]);
}
//排序
for (i = 0; i < n + m; i++)
{
for (j = 0; j < n + m - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
//打印
for (i = 0; i < n + m; i++)
{
printf("%d ", arr[i]);
}
}
//思路2:选择性放入一个新的数组
#include
int main() {
int n = 0;
int m = 0;
scanf("%d %d", &n, &m);
int arr1[n];
int arr2[m];
int arr[n + m];
//读入数据
int i = 0;
for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
for (i = 0; i < m; i++) {
scanf("%d", &arr2[i]);
}
//选择性放入
int j = 0;
int k = 0;
i = 0;
while (j < n && k < m) {
if (arr1[j] < arr2[k])
{
arr[i++] = arr1[j];
j++;
}
else
{
arr[i++] = arr2[k];
k++;
}
}
if(j==n)
{
while(k!=m)
arr[i++] = arr2[k++];
}
if(k==m)
{
while(j!=n)
{
arr[i++] = arr1[j++];
}
}
i = 0;
for(i = 0;i<m+n;i++)
{
printf("%d ",arr[i]);
}
}
完。