有什么方法可以给变量赋值,使得赋值是另一个变量的前两位数字?

时间:2019-08-31 07:31:29

标签: c

我目前正在大学里做一个项目,我想出了逻辑,如果可能的话,可以帮助我更快地完成项目。 这是我想做的 假设

int a=2302; //user input(Now is there any way to do the following?)
int b=23 //First two-digit of a
int c=02; //Last two-digit of a

2 个答案:

答案 0 :(得分:0)

int b = a / 100;
int c = a - b * 100;

答案 1 :(得分:0)

假设value是至少10的int,则:

char fdigs[3], ldigs[3];
int first2 = value / (int)(pow(10.0, (int)(log10(value) + 1.0e-9 - 1.0)) + 0.5);
int last2 = value % 100;
sprintf(fdigs, "%02d", first2);
sprintf(ldigs, "%02d", last2);

将给出两个wee字符串中的数字(如果需要,包括前导零)!

测试代码:

#include <stdio.h>
#include <math.h>

int main()
{
    int value;
    int first2, last2;
    char fdigs[3], ldigs[3];
    printf("\nEnter number: ");
    scanf("%d", &value);
    while (value >= 10) {
        first2 = value / (int)(pow(10.0, (int)(log10(value) + 1.0e-9 - 1.0)) + 0.5);
        last2 = value % 100;
        sprintf(fdigs, "%02d", first2);
        sprintf(ldigs, "%02d", last2);
        printf("First Two: %s; Last Two: %s", fdigs, ldigs);
        printf("\n\nEnter another number: ");
        scanf("%d", &value);
    }
    printf("\n");
    return 0;
}
相关问题