2019-09-24
2019-09-24 本文已影响0人
Distance_code
Answer 02:
//参考答案 1
#include<stdio.h>
int main()
{
int n,i = 0,sum = 0;
scanf("%d",&n);
for(;i<=n;i+=2)
{
sum+=i;
}
printf("%d",sum);
return 0;
}
//参考答案 2 利用等差数列的方法求解(时间复杂度较低,推荐做法)
#include<stdio.h>
int main()
{
int n,i = 0,sum = 0;
scanf("%d",&n);
n /= 2;
sum = n*(n+1);
printf("%d",sum);
return 0;
}
Answer 03:
# include <stdio.h>
int main()
{
char s;
scanf("%c" , &s);
if (s >= 'A' && s <= 'Z')
{
s=s+32;
printf("%c" , s);
}
else if (s >= 'a' && s <= 'z')
{
s = s - 32;
printf("%c" , s);
}
else printf("0",s);
return 0;
}
Answer 04:
### 找规律 合理套循环
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
if(n%2==0){
printf("0\n");
}
else{
for(int i=0;i<n/2+1;i++){
for(int j=0;j<n/2-i;j++){
printf(" ");
}
for(int j=0;j<(2*i+1);j++){
printf("*");
}
printf("\n");
}
for(int k=0;k<n/2;k++){
for(int j=0;j<k+1;j++){
printf(" ");
}
for(int j=k+1;j<n-k-1;j++){
printf("*");
}
printf("\n");
}
}
return 0;
}
##*
#***
*****
#***
##*
//主要了解 外层循环控制行 内层循环控制列
i=0 2 #
i=1 1 j < 3-i
i = 2 0
i = 0 1 *
i = 1 3
i = 2 j5
2*i+1
一个找规律的题
Answer 05:
// 基本排序 : 冒泡排序的运用
#include <stdio.h>
int main()
{
int n,a[128],tmpValue;
scanf("%d",&n);
for(int i = 0;i < n;i++)
{
scanf("%d",&a[i]);
}
for(int i = 0;i <= n-1;i++)
{
for(int j = 0;j <=n-i-1;j++)
{
if(a[j]>a[j+1])
{
tmpValue = a[j];
a[j] = a[j+1];
a[j+1] = tmpValue;
}
}
}
for(int i = 0;i < n;i++)
{
printf("%d",a[i]);
if(i < n-1) printf(" ");
}
return 0;
}
//以上代码可运行 但还可以再改善 优化
Answer 06:
#include<stdio.h>
int primeDataJudge(int value);
int main()
{
int n;
scanf("%d",&n);
if((n == 0) || (n == 1)) printf("No prime data");
else
{
for (int i = 2; i <n; i++)
{
if(primeDataJudge(i) == 1)
{
printf("%d",i);
if(i < n-1) printf(" ");
}
}
}
return 0;
}
int primeDataJudge(int value)
{
if(value == 2) return 1;
else
{
for(int i = 2;i < value;i++)
{
if((value % i) == 0) return 0;
}
}
return 1;
}
Answer 07:
#include<stdio.h>
int main()
{
int n,sum = 0,tmpValue;
scanf("%d",&n);
while (n--)
{
scanf("%d",&tmpValue);
while (tmpValue != 0)
{
sum = sum * 10 + tmpValue % 10;
tmpValue /= 10;
}
printf("%d\n", sum);
sum = 0;
}
return 0;
}
Answer 08:
//来自高中知识 每一行是一个组合数()时间复杂度较高(自行编写)
//上一行的左边和右边之和 空间复杂度较高(数组操作,有记忆性)
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int a[14][14];
a[0][0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
if (j == 1 || j == i)
{
a[i][j] = 1;
}
else
{
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
if(j < i){
printf("%d ", a[i][j]);
}else if(j == i)
{
printf("%d", a[i][j]);
}
}
printf("\n");
}
return 0;
}
Answer 11:
#include <stdio.h>
int main() {
char ch;
int num=0;
int ischar=0;
int newed=0;
int n=0;
while ((ch=getchar()) != '\n')
{
if (ch>='A'&&ch<='z')
{
ischar=1;
printf("%c",ch);
}else if (ch>='0'&&ch<='9')
{
if(ischar){
printf(" ");
ischar=0;
}
num=num*10+(ch-'0');
}
}
printf("\n");
printf("%d",num);
n=num;
newed=0;
do
{
newed=newed*10+n%10;
n/=10;
}while(n>0);
if(num==newed)
printf(" Yes\n");
else
printf(" No\n");
}
high 01:
//暴力递归解法 不建议 但是这是思想的体现
#include <iostream>
using namespace std;
void f(int n);
int count = 0;
int main(int, char**) {
int x;
cin >>x;
f(x);
cout << count;
}
//核心操作
void f(int n){
if(n == 0){
count ++;
}
else if(n < 0) return;
f(n-1);
f(n-2);
f(n-3);
}
规律法:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|
1 | 2 | 4 | 7 | 13 | 24 | 44 | 81 | 149 | 274 |
除起始三 项 第四项起:F(N) = F(N-1)+F(N-2)+F(N-3)
类似斐波那契数列
high 02:
void change(int source,char * target,int radix)
{
/************************begin********/
int tmpValue, count = 0, source_tmp = source;
char decimal[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V'};
while (source_tmp)
{
tmpValue = source_tmp % radix;
source_tmp /= radix;
count++;
}
target[count] = '\0';
while (source)
{
target[--count] = decimal[source % radix];
source /= radix;
}
/***************end********************/
}
[广告插播]CSDN博客,点击这里