基础练习30
来源:互联网 发布:丰城市网络问政网 编辑:程序博客网 时间:2024/04/28 00:46
首先是:坑爹的VIP
数列排序
第二行包含n个整数,为待排序的数,每个整数的绝对值小于10000。
8 3 6 4 9
#include <stdio.h>#include <stdlib.h>#include <string.h>int main() { int n,next,already = 0,i = 0; int sort[200] ; memset(sort,0,200*sizeof(int)); scanf("%d",&n); while( already < n ) { scanf("%d",&next); int position = 0; while(next >= sort[position]&&position<already) { position++; } for(i = already; i>position; i--){sort[i] = sort[i-1];} sort[i] = next;already++; } for(already = 0;already <n;already++) {printf("%d ",sort[already]); } return 0;}
还以为时间会有问题,但是还是过了.....好吧.......排序还是要好好看看啊....16进制转8进制
接下来n行,每行一个由0~9、大写字母A~F组成的字符串,表示要转换的十六进制正整数,每个十六进制数长度不超过100000。
输出的八进制数也不能有前导0。
39
123ABC
4435274
16进制转为10进制
注:十六进制数中的10~15分别用大写的英文字母A、B、C、D、E、F表示。
#include <math.h>#include <stdio.h>int main() {int n = 0;__int64 result=0;char now;int temp = 0;while( (now=getchar()) != '\n' ){result *= 16;if(now<='9' && now>='0'){temp = now -48;}else{temp = now - 'A' +10;}result +=temp;}printf("%I64d\n",result);return 0;}
一直超时= = .....那用字符串吧,感觉还是C++ 的string用的舒服,所以还是用C++,好吧OK了!#include <iostream>#include <math.h>#include <string.h>using namespace std;int main(int argc, char *argv[]) {long long result = 0;string str;cin>>str; for(int i=0; i<str.length(); i++){int temp;if( str[i] <='9'){temp = str[i] - '0';}else{temp = str[i] - 'A' + 10;}result += temp * pow (16,str.length()-1-i);}cout<<result<<endl; return 0;}
十进制转16进制
给出一个非负整数,将它表示成十六进制的形式。
#include <iostream>using namespace std;int main(int argc, char *argv[]) {int number,temp,now = 0;cin>>number;if(number == 0){cout<<0;return 0;} char result[10],ctemp;while( number >0 ){temp = number % 16;if(temp > 9){ctemp = 'A' + temp -10; }else{ctemp = '0' + temp;}result[now] = ctemp;now++;number /= 16;}for(int j=now-1; j>=0; j--){cout<<result[j];}cout<<endl;return 0;}
0要单独处理特殊回文数
输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
989989
998899
#include <iostream>#include <math.h>using namespace std;int main(int argc, char *argv[]) {int n;cin>>n;for(int i=1; i<10&&i>0; i++){for(int j=0; j<10; j++){int k = n -2*(i+j);if(k<10 && k>=0){cout<<i<<j<<k<<j<<i<<endl;}}}for(int i=1; i<10&&i>0; i++){for(int j=0; j<10; j++){int k = n -2*(i+j);if(k%2==0 && k<20&& k>=0){cout<<i<<j<<k/2<<k/2<<j<<i<<endl;}}}return 0;}
杨辉三角形又称Pascal三角形,它的第i+1行是(a+b)i的展开式的系数。
它的一个重要性质是:三角形中的每个数字等于它两肩上的数字相加。
下面给出了杨辉三角形的前4行:
1
1 1
1 2 1
1 3 3 1
给出n,输出它的前n行。
输入包含一个数n。
1 1
1 2 1
1 3 3 1
#include <iostream>#include <math.h>using namespace std;/* run this program using the console pauser or add your own getch, system("pause") or input loop */int main(int argc, char *argv[]) {int n;cin>>n;long long below[34] = {0};for(int i=0; i<n; i++){if(i==0){cout<<1<<endl; below[0]=below[2]=0;below[1]=1;}else{long long before = 0,before2;for(int j=0; j<i+1; j++){cout<<before+below[j+1]<<" ";before2 = before;before = below[j+1];below[j+1] = before2+below[j+1];}cout<<endl;}}return 0;}
但是不找到为什么一直表示只有90分Σ(⊙▽⊙"a... ....但是找不到问题o(╯□╰)o
给出一个包含n个整数的数列,问整数a在数列中的第一次出现是第几个。
第一行包含一个整数n。
第二行包含n个非负整数,为给定的数列,数列中的每个数都不大于10000。
第三行包含一个整数a,为待查找的数。
1 9 4 8 3 9
9
#include <iostream>using namespace std;int main(int argc, char *argv[]) {int n,find;int number[10005],i = 0;cin>>n;for( i = 0; i<n; i++ ){cin>>number[i];}cin>>find;for(int j = 0; j<i; j++){if( number[j] == find ){cout<<j+1<<endl;return 0;}}cout<<-1<<endl;return 0;}
给出n个数,找出这n个数的最大值,最小值,和。
第一行为整数n,表示数的个数。
第二行有n个数,为给定的n个数,每个数的绝对值都小于10000。
1 3 -2 4 5
-2
3
#include <iostream>using namespace std;int main(int argc, char *argv[]) {int n,temp,max,min,sum;cin>>n; cin>>temp;max = min = sum = temp;for(int i=0; i<n-1; i++){cin>>temp;if(max < temp){max = temp;}else if(min > temp){min = temp;}sum += temp;}cout<<max<<endl<<min<<endl<<sum<<endl; return 0;}
利用字母可以组成一些美丽的图形,下面给出了一个例子:
ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC
这是一个5行7列的图形,请找出这个图形的规律,并输出一个n行m列的图形。
BABCDEF
CBABCDE
DCBABCD
EDCBABC
#include <iostream>using namespace std;int main(int argc, char *argv[]) {int line,row;char Print;cin>>line>>row;for(int j=0; j<line; j++){for(int k=0; k<j&&k<row; k++){Print = 'A' + j - k;cout<<Print;}for(int k = 0; k<row-j; k++){Print = 'A' + k;cout<<Print;}cout<<endl;} return 0;}
对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:
00000
00001
00010
00011
00100
请按从小到大的顺序输出这32种01串。
00001
00010
00011
<以下部分省略>
#include <iostream>using namespace std;int main(int argc, char *argv[]) {char result[6] = "00000";for(int i=0; i<32; i++){cout<<result<<endl;if(result[4] == '0'){result[4] = '1';continue;}for(int j=4; j>-1; j--){if(result[j] == '0'){result[j] = '1';break;}else{result[j] = '0';}}}return 0;}
给定一个年份,判断这一年是不是闰年。
当以下情况之一满足时,这一年是闰年:
1. 年份是4的倍数而不是100的倍数;
2. 年份是400的倍数。
其他的年份都不是闰年。
说明:当试题指定你输出一个字符串作为结果(比如本题的yes或者no,你需要严格按照试题中给定的大小写,写错大小写将不得分。
唉,这明显是C入门题-________-''
#include <iostream>using namespace std;int main(int argc, char *argv[]) {int year;cin>>year;if( !(year%400) || (!(year%4)&&(year%100))){cout<<"yes"<<endl; return 0; } cout<<"no"<<endl; return 0;}
总之,这个就先告一段落....接下来是算法部分罗~~~~
- 基础练习30
- 基础练习
- 基础练习
- 基础练习
- 基础练习
- 基础练习
- 蓝桥杯 BASIC-30 基础练习 阶乘计算
- Java经典基础练习21-30
- Java:基础练习-循环练习
- 【c基础练习】文件练习
- 【c基础练习】数组基础练习
- C++基础练习一
- 基础练习KMP算法
- 基础练习一
- 基础练习二
- c基础练习
- WinForm基础:综合练习
- WinForm基础:文本框练习
- Windows驱动开发——WDM驱动
- 【四圣龙神录的编程教室】第16章、来制作洩矢大人的弹幕吧
- hdu 1850 Being a Good Boy in Spring Festival
- NYOJ34韩信点兵
- Java学习系列(一)Java的运行机制、JDK的安装配置及常用命令详解
- 基础练习30
- JS删除字符串中重复字符
- awgn函数与高斯白噪声
- 越界
- Ubuntu 12.04 root用户登录设置
- maven教程一:maven与web project项目转换
- 九度OnlineJudge-搬水果(1107)
- 讲故事学设计模式-装饰者(Decorator)模式
- 求最大公约数