Hang electrical re-examination written questions: 2006-2009

2006 Topic 1: Enter a decimal number, turn it into octal. Similar transferred from decimal to hexadecimal, the hexadecimal to decimal conversion and the like.

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
//2006年
//题目1:输入一个十进制的数,把它转成八进制。类似的把十进制转成16进制,把十六进制转变为十进制等。
int main()
{
    int n;//表示输入的数值
    int a[100]={0};//用一个数组来存对应的八进制位数
    scanf("%d",&n);
    int i=0;
    int j;
    while(n!=0){
        a[i]=n%8;
        n/=8;
        i++;
    }
    //然后从后往前输入
    for(j=i-1;j>=0;j--)
        printf("%d",a[j]);


}

Problem 2: Enter two very large integer (completely out int, long range representation), this length may be an integer of more than 100, and outputs the calculation results of these two numbers together. (
HDU acm 1002 with a string handle better)

This question is quite good: 1. The first choice is to choose #include <string.h> 2. The first function followed by char array input is selected% s and 3 is now behind without conversion on characters and numbers by ' 0 '4. there is the sum from the last one, the last one is the start of the array

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
//题目2:输入两个非常大的整数(完全超出了int、long的表示范围),
//这个整数的长度可能超过100位,计算并输出这两个数相加的结果。( HDU acm 1002用string处理比较好)
//我的思路就是,创建三个字符串数组两个用来输入,一个用来最后的输出,
char str1[1000];
char str2[1000];
char str3[1000];
int main()
{
    //先初始化一下
    memset(str1,0,sizeof(str1));
    memset(str2,0,sizeof(str2));
    memset(str3,0,sizeof(str3));
    //输入对应的两个数
    scanf("%s%s",str1,str2);//这里需要注意一下
    int len1=strlen(str1)-1;
    int len2=strlen(str2)-1;//这两个的长度都是需要知道的
    int i=len1;
    int j=len2;
    int k=0;//k是求和之后的存储数组中的值
    int num;//用来存中间值
    int flag=0;//用来存中间的进位
    while(i>=0&&j>=0){//在这两个长度都满足的情况之下
        //开始进行从个位开始加
        num=(str1[i--]-'0')+(str2[j--]-'0');//这里值得注意的是,字符串通过-'0'变成数字(因为数字字符串就是'0’这个位置加上对应数字向前进几位
        num+=flag;
        flag=(num/10);//这就相当于下一个了
        num%=10;
        str3[k++]=num+'0';
    }
    //然后可能会存在两个数值的长度不一样,所以会让有一个更进一步
    while(i>=0){
        num=str1[i--]-'0';
        num+=flag;
        flag=(num/10);
        num%=10;
        str3[k++]=num+'0';
    }
     while(i>=0){
        num=str2[i--]-'0';
        num+=flag;
        flag=(num/10);
        num%=10;
        str3[k++]=num+'0';
    }
    //这里还有一点,还要看最后一个进位
    if(flag==1)
        str3[k++]=1+'0';
    //然后开始输出
    for(j=k-1;j>=0;j--)
        printf("%c",str3[j]);


}



2007
Topic 1: palindrome string "is a positive reading and verlan same string, such as" level "or" noon "and so the string is a palindrome Please write a program to determine whether a string is read." palindrome. "

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
//2007年
//题目1:回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。
char str[100];
int main()
{
    //输入字符
     scanf("%s",str);
     int len=strlen(str);
     int i,j;
     int flag=0;
     for(i=0,j=len-1;i<=j;j--,i++){//这个i和j的关系需要注意一下
        if(str[i]!=str[j])
            flag=1;
     }

     if(flag==1)
        printf("NO");
     else
        printf("YES");


}

Problem 2: The number of inputs n, in ascending sort and output.

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
//题目2:输入n个数,按从小到大进行排序并输出。
//就直接sort排序吧
char str[100];
int main()
{
    int a[1000];
    int n;//这个是输入的数
    int i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    //再直接用sort的默认排序
    sort(a,a+n);
    for(i=0;i<n;i++)
        printf("%d ",a[i]);

}

Problem 1: Enter a long integer number, with the odd bits of a new output from the low number.

It is worth noting that a long integer is Ld

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include <math.h>
using namespace std;
//题目1:输入一个长整型的数,从低位起取出奇数位组成一个新的数输出。
int main()
{
    long int num,sum,temp;//这是要输入的值,最终值,中间值
    scanf("%ld",&num);
    sum=0;
    int k=0;//这个是10的次方
    //是需要用到/和%
    while(num!=0){
        //先取第一位
        temp=num%10;
        sum+=(temp*pow(10,k));
        k++;
        num/=100;//因为直接跳过下一个
    }
    printf("%ld",sum);
}

Problem 2: n input string, they are alphabetically in ascending order and outputs.

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#define MAX 100
using namespace std;
bool cmp(string x,string y)//strcmp(x,y)若x==y,则返回零;若x<y,则返回负数;若x>y,则返回正数。
{
	if(strcmp(x.c_str(),y.c_str())==1)
		return false;
	return true;
}
int main()
{
	int n;
	string str[MAX];
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
			scanf("%s",str[i].c_str());
		sort(str,str+n,cmp);
		for(int i=0;i<n;i++)
            printf("%s ",str[i].c_str());
	}
}


2009: Title 1: Enter two positive integers, find the greatest common divisor of two numbers.

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
//2009年:
//题目1:输入两个正整数,求出这两个数的最大公约数。
int main()
{
    //就是之前学的辗转相除法
    int a,b;//两个需要输入的正整数
    int temp;
    scanf("%d%d",&a,&b);
    //需要判断这两个的大小
    if(a>b){
        temp=a;
        a=b;
        b=temp;
    }
    while(b!=0){
       temp=b%a;
       a=b;
       b=temp;
    }
    printf("%d",a);
}

Problem 2: Number of Narcissus "refers to a three-digit, digits of which is equal to the cube of its own, for example: 153 = 13 + 53 + 3 ^ 3, enter an integer, it is determined whether the number of daffodils

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
//题目2:水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+3^3,输入一个整数,判断它是否是水仙花数。
//写一个方法求立方
int lifang(int x){
    return x*x*x;
}
int main()
{
    int n,m;//写一个三位数
    int sum=0;
    int temp;
    scanf("%d",&n);
    m=n;
    while(n){//依次求个十百位
        temp=n%10;
        sum+=lifang(temp);
        n/=10;
    }
    if(sum==m)
        printf("YES");
    else
        printf("NO");

}

After the defined number:: Title 3 If a positive integer greater than 1 and equal to the sum of all the factors of its own, called the number is the number of complete, such as 6,28 Ends are: 6 = 1 + 2 + 3; 28 = 1 + 2 + 4 + 7 + 14. Enter an integer, it is determined whether the number of finished.

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
//题目3:完数的定义:
//如果一个大于1的正整数的所有因子之和等于它的本身,则称这个数是完数,比如6,28都是完数:6=1+2+3;28=1+2+4+7+14。输入一个整数,判断它是否是完数。
//写一个方法判断是否为因子
bool yingzi(int x,int y){
    if(x%y==0)
        return true;
    return false;
}
int main()
{
    int n;//输入整数
    scanf("%d",&n);
    int i;
    int sum=0;
    for(i=1;i<n;i++){
        if(yingzi(n,i))
            sum+=i;
    }
    if(sum==n)
        printf("YES");
    else
        printf("NO");

}
Published 72 original articles · won praise 5 · Views 2807

Guess you like

Origin blog.csdn.net/qq_41115379/article/details/104941589