牛客网初阶练习(11)

x33g5p2x  于2022-02-14 转载在 其他  
字(0.8k)|赞(0)|评价(0)|浏览(182)

1.小乐乐求和

2.小乐乐与欧几里得

3.小乐乐与序列

1.小乐乐求和

#include<stdio.h>
int main()
{
    int n = 0;
    long long sum = 0;
    int i = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        sum += i;
    }
    printf("%lld", sum);//从题目中明确int类型是无法将这个结果存放的,所以要用long long int类型的变量进行存储
    return 0;
}

2.小乐乐与欧几里得

#include<stdio.h>
int main()
{
	long long n = 0;
	long long m = 0;
    scanf("%lld %lld",&n,&m);
	long long n2 = n;
	long long m2 = m;
	long long max = 0;
	long long min = 0;
	long long temp = 0;
	while (temp = n % m )
	{
		n = m;
		m = temp;
	}
	max = m;
	min = n2 * m2 / max;
	printf("%lld", max + min);
	return 0;
}
//这个题,也是必须使用long long int类型的变量来进行存储

3.小乐乐与序列

#include<stdio.h>
int main()
{
    int n = 0;
    int arr[100001] = {0};
    scanf("%d",&n);
    int tmp = 0;
    for(int i = 0;i<n;i++)
    {
        scanf("%d",&tmp);
        arr[tmp] = tmp;
    }
    for(int i = 0;i<n;i++)
    {
        if(arr[i]!=0)
        {
            printf("%d ",arr[i]);
        }
    }
    return 0;
}

相关文章