蓝桥杯【基础训练】部分
BASIC-1 闰年判断
/* 基础训练:BASIC-1 闰年判断 条件判断 */ #include<iostream> using namespace std; int main() { int year; cin>>year; while(year<=2050&&year>=1990) { if(year%4==0) if(year%100==0) if(year%400==0) {cout<<"yes";break;} else {cout<<"no";break;} else {cout<<"yes";break;} else {cout<<"no"; break;} cin>>year; } return 0; }
BASIC-2 01串
/* 基础训练: BASIC-2 01字串 循环 */ #include <stdio.h> int main(void) { int a, b, c, d, e; for(a = 0; a < 2; a++) for(b = 0; b < 2; b++) for(c = 0; c < 2; c++) for(d = 0; d < 2; d++) for(e = 0; e < 2; e++) { printf("%d%d%d%d%d\n", a, b, c, d, e); } return 0; }
BASIC-3 字母图形
/* BASIC-3 字母图形 循环 字符串 字母的序号与两个坐标的差的绝对值有关,26个字母对应的序号分别是0到25. */ #include<iostream> #include<math.h> using namespace std; char toStr(int i, int j, char str[26]){ char tostr; int num, k; num = abs(i - j); return str[num]; } int main(void){ int n, m, i, j; //n >= 1 , m <= 26 char str[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W',X','Y','Z'}; int a[n][m]; cin >> n >> m; for(i = 0; i < n; i++){ for(j = 0 ; j < m; j++){ printf("%c", toStr(i, j, str)); } printf("\n"); } return 0; }
BASIC-4 数列特征
/* BASIC-4 数列特征 循环 最大值 最小值 累加 */ #include <stdio.h> #define MaxSize 10000 main() { int n; int sum=0,min=MaxSize,max=-MaxSize; scanf("%d",&n); while(n--) { int temp,st; scanf("%d",&temp); if(temp>max) { max=temp; } if(temp<min) { min=temp; } sum+=temp; } printf("%d\n%d\n%d\n",max,min,sum); return 0; }
BASIC-5 查找整数 循环 判断
/* BASIC-5 查找整数 循环 判断 */ #include<iostream> using namespace std; int main(void){ int n, b, i, flag = -1; unsigned int a[1000]; cin >> n; for(i = 0; i < n; i++){ cin >> a[i]; } cin >> b; for(i = 0; i < n; i++){ if(b == a[i]){ flag = 0; cout<<(i + 1); break; } } if(flag){ cout << flag; } return 0; }
BASIC-6 杨辉三角形
/* BASIC-6 杨辉三角形 基础练习 二维数组 */ #include<iostream> #include<iomanip> using namespace std; int main(void){ int num, i, j; //num <= 34 int a[100][100]; //将杨辉三角存储与二维数组 cin >> num; if(num >= 1 && num <= 34){ //初始化 for(i = 0; i < num; i++){ //杨辉三角的每行的第一位和最后一位为一 a[i][0] = a[i][i] = 1; for(j = 0; j < i; j++){ //除第一,二行,其余行是前一行与之对应的计算和 a[i][j] = a[i - 1][j - 1] + a[i - 1][j]; } } //输出 for(i = 0; i < num; i++){ for(j = 0; j <= i; j++){ cout << a[i][j] << " "; //每行的每个数之间空一格 // cout << std::left << setw(5) << a[i][j]; //这种方法不和题意,且显示效果不必上个输出好 } cout << endl; } } return 0; }
BASIC-7 特殊的数字
/* BASIC-7 特殊的数字 循环 判断 数位 */ #include<iostream> using namespace std; int main(void){ int num, a, b, c; for(num = 100; num <= 999; num++){ a = num % 10; b = num % 100 / 10; c = num / 100; if(a*a*a + b*b*b + c*c*c == num){ cout << num << endl; } } return 0; }
BASIC-8 回文数
/* BASIC-8 回文数 循环 判断 回文数 */ #include<iostream> using namespace std; int main(void){ unsigned int num, a, b, c, d; for(num = 1000; num <= 9999; num++){ a = num % 10; //个位 b = num % 100 / 10; //十位 c = num % 1000 / 100; //百位 d = num / 1000; //千位 if(a == d && b == c){ // cout << a << b << c << d << " "; cout << num << endl; } } return 0; }
BASIC-9 特殊回文数
/* BASIC-9 特殊回文数 回文数 循环 条件语句 */ #include<iostream> using namespace std; int main(void){ long int numb; int a, b, c, d, e, f, num; cin >> num; for(numb = 10000; numb <= 999999; numb++){ if(numb <= 99999){ a = numb % 10; b = numb % 100 / 10; c = numb % 1000 / 100; d = numb % 10000 / 1000; e = numb / 10000; if((num == a + b + c + d + e) && (a == e) && (b == d)){ cout << numb << endl; } }else{ a = numb % 10; b = numb % 100 / 10; c = numb % 1000 / 100; d = numb % 10000 / 1000; e = numb % 100000 / 10000; f = numb / 100000; if((num == a + b + c + d + e + f) && (a == f) && (b == e) && (c == d)){ cout << numb << endl; } } } return 0; }
BASIC-10 十进制转十六进制
/* BASIC-10 十进制转十六进制 循环 整除 求余 判断 */ #include <iostream> #include <cstring> using namespace std; string decimalToHexadecimal(int num); string decimalToHexadecimal(int num){ int ans,k;k=0; string result=""; if(num == 0){ cout << "0" << endl; return result += "0"; } while(num>0){ ans = num % 16; if(ans <= 9 && ans >= 0){ result[k++] = ans + 48; }else if(ans >= 10 && ans < 16){ result[k++] = ans + 55; } num = num / 16; } for(int i = k-1; i >= 0; i--) cout << result[i]; cout << endl; return result; } int main(void) { int num; while(cin >> num) { decimalToHexadecimal(num); } return 0; }
BASIC-12 十六进制转八进制 进制转换 字符 循环
(这道题不是我ac的,是这个人!)
/*BASIC-12 十六进制转八进制 进制转换 字符 循环*/
#include <iostream> #include <string> using namespace std; int arr[10000001]; int main() { int n,len_str,i,j; string str,str2; cin>>n; while(n--) { cin>>str; len_str=str.length(); str2=""; // 十六进制转换为二进制 for(i=0;i<len_str;++i) { switch(str[i]) { case '0':str2+="0000";break; case '1':str2+="0001";break; case '2':str2+="0010";break; case '3':str2+="0011";break; case '4':str2+="0100";break; case '5':str2+="0101";break; case '6':str2+="0110";break; case '7':str2+="0111";break; case '8':str2+="1000";break; case '9':str2+="1001";break; case 'A':str2+="1010";break; case 'B':str2+="1011";break; case 'C':str2+="1100";break; case 'D':str2+="1101";break; case 'E':str2+="1110";break; case 'F':str2+="1111";break; default:break; } } // 修正位数 if(len_str%3==1) str2="00"+str2; else if(len_str%3==2) str2="0"+str2; len_str=str2.length(); // 二进制转换八进制 j=0; for(i=0;i<=len_str-2;i+=3) { arr[j]=(str2[i]-'0')*4+(str2[i+1]-'0')*2+(str2[i+2]-'0'); ++j; } for(i=0;i<j;++i) { if(i==0 && arr[i]==0) continue; cout<<arr[i]; } cout<<endl; } return 0;
}
BASIC-13 数列排序
/* BASIC-13 数列排序 数组 排序 */ #include<iostream> using namespace std; void quickSort(int num[], int low, int hight); int quickPass(int num[], int low, int hight); void quickSort(int num[], int low, int hight){ int pos; if(low < hight){ pos = quickPass(num, low, hight); quickSort(num, low, pos - 1); quickSort(num, pos + 1, hight); } } int quickPass(int num[], int low, int hight){ int x; x = num[low]; while(low < hight){ while(low < hight && num[hight] >= x){ hight--; } if(low < hight){ num[low] = num[hight]; low++; } while(low < hight && num[low] <= x){ low++; } if(low < hight){ num[hight] = num[low]; hight--; } } num[low] = x; return low; } int main(void){ unsigned int n; int num[10000], i; cin >> n; for(i = 0; i < n; i++){ cin >> num[i]; } quickSort(num, 0, i - 1); for(i = 0; i < n; i++){ cout << num[i] << " "; } return 0; }
转载本博请联系作者! 如有问题请在评论区评论或者发邮件:@libras