目录
3. A = "Hello" B=Hell 设计一个算法,输出结果为C = 0;
1. 二进制->十进制->十六进制
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int main(){
string ch;
cin>>ch;
int length = ch.length();
int sum = 0;
char jinzhi[20]="0123456789ABCDEF";
char result[50];
int count = 0;
for(int i = 0;i < length;i++){
sum += int(ch[length-i-1] - '0')*pow(2,i);
}
cout<<sum<<endl;
while(sum != 0){
result[count++]=jinzhi[sum%16];
sum=sum/16;
}
for(int i=count;i>=0;i--)
cout<<result[i];
cout<<endl;
}
2. 去掉字符中的0
int a[] = {5,4,0,1,0,6,2,0,5,0};
int length = sizeof(a)/sizeof(a[0]);
//cout<<length<<endl;
//if(a[length - 1 ] == 0) length-=1;
for(int i = 0; i < length; i++){
if(a[i] == 0){
for(int j = i;j <length;j++){
a[j] = a[j+1];
}
length-=1;
}
}
for(int i = 0; i < length; i++){
cout<<a[i]<<" ";
}
cout<<endl;
3. A = "Hello" B=Hell 设计一个算法,输出结果为C = 0;
string yuans ;
string results;
int index = 0;
cin>>yuans;
char inputs[yuans.length()];
cin>>inputs;
for(int i = 0;i < yuans.length();i++){
if( inputs[i] != yuans[i]){
index = i;
break;
}
}
results = yuans.substr(index, yuans.length());
cout<<results<<endl;
}
4. 输时间戳如12:45:59
int main(){
time_t timep; //定义时间变量
time(&timep);//将工作时间赋值给时间变量
char tmp[64];
strftime(tmp, sizeof(tmp), "%H:%M:%S", localtime(&timep));
cout << "currentTime is " << tmp << "." << endl;
return 0;
}
5. 判断序列是否有序
int main()
{
int n=0;
int arr[50]={0};
cin>>n;
int flag1=0;
int flag2=0;
int i=0;
for(i=0;i<n;i++)
{
cin>>arr[i];
if(i>0)
{
if(arr[i]>arr[i-1])
{
flag1=1;
}
else
{
flag2=1;
}
}
}
if(flag1+flag2==1)
{
cout<<"sorted"<<endl;
}
else
{
cout<<"unsorted"<<endl;
}
return 0;
}
6. 字符串插入
int main(){
char str1[100],str2[100];
int n,t1,t2;
cin>>str1;
cin>>str2;
cin>>n;
t1=strlen(str1);//算出字符串的长度
t2=strlen(str2);
for(int i =t1+t2-1; i >n ;i--){
str1[i] = str1[i-t2];
}
int k = 0;
for(int j = n; j < n+t2;j++){
str1[j] = str2[k];
k++;
}
for(int i = 0; i <t1+t2 ;i++){
cout<<str1[i];
}
return 0;
}
7. 找最大和次大的两个数
int main(){
int a[21] = {21,4,7,102,9,98,31};
int i,Max = 0,SecondMax = -1;
int length = sizeof(a)/sizeof(a[0]);
for(i = 0;i < length;i++){
if(a[i] > Max){
if(Max > SecondMax) SecondMax =Max;
Max = a[i];
}else{
if(a[i] > SecondMax) SecondMax =a[i];
}
}
cout<<"Maxax:"<<Max<<"SecondMax:"<<SecondMax<<endl;
return 0;
}
8. 不用map实现在一个整数数组中找到各个数字出现的次数
int main(){
int a[] = {2,5,5,2,3,4,5,2,1};
int length = sizeof(a)/sizeof(a[0]);
int i = 0,b;
while(i < length){
b = a[i];
if(a[i] > 0){
if(a[b-1] > 0){
a[i] = a[b-1] ;
a[b-1] = -1;
}else{
a[i] = 0;
a[b-1]-=1;
}
}else i++;
}
for(int i=0 ;i < length ;i++){
cout<<i+1<<":"<<abs(a[i])<<endl;
}
return 0;
}
9. 大数相乘
#include <iostream>
#include <string.h>
using namespace std;
#define MAXSIZE 1000
string a,b;//输入
int x[MAXSIZE+1],y[MAXSIZE+1],z[MAXSIZE*2+1];//积的位数最多是因数位数的两倍
int main()
{
int lena,lenb,i,j;
cin>>a>>b;
lena = a.length();
lenb = b.length();
for(j=0,i=lena-1;i>=0;i--)//将字符串中字符转化为数字,并倒序储存
x[j++]=a[i]-'0';
for(j=0,i=lenb-1;i>=0;i--)
y[j++]=b[i]-'0';
for(i=0;i<lena;i++)//将因数各个位上的数字与另一个各个位上的数字相乘
{
for(j=0;j<lenb;j++)
z[i+j]=z[i+j]+x[i]*y[j];//先乘起来,后面统一进行进位
}
for(i=0;i<MAXSIZE*2;i++)//进行进位
{
if(z[i]>=10) //若>=10
{
z[i+1]=z[i+1]+z[i]/10; //将十位上数字进位
z[i]=z[i]%10; //将个位上的数字留下
}
}
for(i=MAXSIZE*2;i>=0;i--){
if(z[i] !=0){
for(;i>=0;i--){
cout<<z[i];
}
}
}
return 0;
}
参考链接:https://blog.csdn.net/lisp1995/article/details/52316466
10. 删除非空链表的重复节点
//删除非空链表的重复节点
LinkList repeat(LinkList &head){
LNode *p = head ->next;
LNode *q ,*r;
while(p != NULL){
q = p;
while(q->next != NULL){
if(q->next->data == p->data){
r = q->next;
q->next = r->next;
free(r);
}
else
q = q->next;
}
p = p->next;
}
return head;
}
附加全代码:(其实LinkList 强调是个链表 也可以写成 LNode *的,但node一般指节点)
#include <iostream>
using namespace std;
typedef struct LNode //typedef用来重命名struct node这种数据类型,将其命名为LNode
{
int data;
struct LNode* next;
}LNode,*LinkList;
//初始化链表
bool init(LinkList &head){
head = (LNode*)malloc(sizeof(LNode));
if(head == NULL)
return false ; //内存不足,分配失败
head ->next = NULL;
return true;
}
//新建一个长度为n的链表并返回首节点的地址
LinkList create(LinkList &head,int n)
{
int x ;
LNode *s ,*r = head ;
for (int i = 0; i < n; i++)
{
cin>>x;
s = (LNode *)malloc(sizeof(LNode));
s->data =x;
r->next=s;
r=s;
}
r->next = NULL;
return head;
}
//删除非空链表的重复节点
LinkList repeat(LinkList &head){
LNode *p = head ->next;
LNode *q ,*r;
while(p != NULL){
q = p;
while(q->next != NULL){
if(q->next->data == p->data){
r = q->next;
q->next = r->next;
free(r);
}
else
q = q->next;
}
p = p->next;
}
return head;
}
void print(LinkList &head){
LNode *p = head ->next;
while(p != NULL){
cout<<p->data<<" ";
p = p ->next;
}
cout<<endl;
}
int main()
{
LinkList head;
init(head);
create(head,5);
print(head);
repeat(head);
print(head);
return 0;
}
11. 从文件中读入字符,并判断数字、字母、其他字符的个数
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
int main ()
{
ifstream fin;
fin.open("C:\\Users\\LENOVO\\Desktop\\test.txt",ios::in);
if (fin.is_open()) {
cout<<"open"<<endl;
int count = 0;
int row_count = 0;
int bigword = 0,lowword = 0,number = 0,elsechar = 0;
char ch = 0;
while((ch= fin.get())!=EOF){
//cout<<ch;
count++;
if(count % 5 == 0){ // 以5个字符位一行,看一共几行
row_count++;
}
if (ch <= 'z' && ch >= 'a') {//属于字母范围并且之前不是一个单词
lowword++;
cout<<"hh"<<endl;
}
else if (ch <= 'Z' && ch >= 'A') {
bigword++;
}
else if (ch <= '9' && ch >= '0') {
number++;
}
else{
elsechar++;
}
}
cout<<row_count<<" "<<bigword<<" "<<lowword<<" "<<number<<" "<<elsechar;
}
return 0;
}
12. 递归逆序输出整数
int getnumber(int n){
if(n/10 == 0) return n;
else {
cout<<n%10;
return getnumber(n/10);
}
}
int main ()
{
int n ;
cin>>n;
cout<<getnumber(n)<<endl;
return 0;
}
13.十进制转十九进制
int teto(int num ,string &str){
if(num == 0)
return 1;
int temp = num%19;
teto(num/19,str);
if(temp > 9){
char c = 'a' + (temp -10);
str = str +c;
}
else{
str = str +to_string(temp);
}
}
int main ()
{
string s1 = " " ;
teto(1024,s1);
cout<<s1;
return 0;
}
14.
15.
16.
17.
18.
19.
20.