我目前正在大学里做一个项目,我想出了逻辑,如果可能的话,可以帮助我更快地完成项目。 这是我想做的 假设
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
答案 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;
}